小编Ben*_*min的帖子

使用java 8 DateTime API解析日期和AM/PM

我正在尝试使用java 8 DateTime API解析日期时间字符串.

要解析的字符串包含日期,月份,年份和AM/PM标记,例如17/02/2015 PM.我使用以下模式:dd/MM/yyyy aa我希望解析的LocalDateTime时间部分设置为12:00:0000:00:00取决于AM/PM标记.

使用以前的java.text.SimpleDateFormatAPI,解析使用此模式按预期工作.

但是当我使用java 8 DateTime API并运行以下代码时:

LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa"));
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1765)
at java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1604)
at java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1572)
at java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:534)
Run Code Online (Sandbox Code Playgroud)

如果我将模式切换到dd/MM/yyyy a那时我得到了以下异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text '17/02/2015 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492) …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-time

11
推荐指数
1
解决办法
5025
查看次数

如何使用spring4 @RestController返回一个jsp页面?

当我像这样使用spring3 @Controller时:

@RequestMapping("/userCenter")

@Controller
public class LoginCtrl {
    @RequestMapping("/loginPage")
    public String login(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception { 
        return "userCenter/loginPage";
    }
}
Run Code Online (Sandbox Code Playgroud)

没关系,我在浏览器中获取了loginPage.jsp正确的内容.

但是当我将@Controller更改为@RestController时

localhost:8080 // userCenter/loginPage返回一个字符串为"userCenter/loginPage"的页面

那么,我如何使用@RestController来获取像@Controller这样的jsp页面?

spring spring-mvc

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

java-8 ×1

java-time ×1

spring ×1

spring-mvc ×1