问题描述
以前我可以很容易地写道:
String date =04.2013;
DateFormat df = new SimpleDateFormat(MM.yyyy);
日期d = df.parse(date);
但是现在如果我使用 LocalDate ,就这样做: p>
字符串日期=04.2013;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(MM.yyyy);
LocalDate ld = LocalDate.parse(date,formatter);
我收到例外:
java.time.format.DateTimeParseException:无法在索引0处解析文本'04'
java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
java.time.LocalDate.parse(LocalDate.java:400)
java.time.LocalDate.parse(LocalDate.java :385)
com.luxoft.ath.controllers.JsonController.region(JsonController.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl。 invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invoca bleHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method。 annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web。 servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet。 DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet。 java:852)
javax.servlet.http.HttpServlet.service(Http Servlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
如果我将字符串格式更改为yyyy-MM-dd,一切都可以完美工作,即使没有格式化程序: / p>
String date =2013-04-12;
LocalDate ld = LocalDate.parse(date);
所以我的问题是:如何使用Java 8 Time API以自定义格式解析日期? >
这是有道理的:你的输入并不是一个真正的日期,因为它没有一天的信息。您应该将其解析为 YearMonth
,如果您不在乎当天,请使用该结果。
String date =04.2013;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(MM.yyyy);
YearMonth ym = YearMonth.parse(date,formatter);
如果您需要申请特定的一天,您可以获得一个 LocalDate
从 YearMonth
例如:
LocalDate ld = ym.atDay(1);
//或
LocalDate ld = ym.atEndOfMonth();
您还可以使用 TemporalAdjuster
例如,在本月的最后一天*:
LocalDate ld = ym.atDay(1).with(lastDayOfMonth() );
I'm a bit discouraged with parsing dates in Java 8 Time API.
Previously I could easily write:
String date = "04.2013";
DateFormat df = new SimpleDateFormat("MM.yyyy");
Date d = df.parse(date);
But now if I use LocalDate and do it like this:
String date = "04.2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
I receive an exception:
java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0
java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
java.time.LocalDate.parse(LocalDate.java:400)
java.time.LocalDate.parse(LocalDate.java:385)
com.luxoft.ath.controllers.JsonController.region(JsonController.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
If I change string format to "yyyy-MM-dd" everything work perfectly, even without formatter:
String date = "2013-04-12";
LocalDate ld = LocalDate.parse(date);
So my question is: how to parse date in custom format using Java 8 Time API?
It makes sense: your input is not really a date because it does not have a day information. You should parse it as a YearMonth
and use that result if you don't care about the day.
String date = "04.2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy");
YearMonth ym = YearMonth.parse(date, formatter);
If you do need to apply a specific day, you can obtain a LocalDate
from a YearMonth
for example:
LocalDate ld = ym.atDay(1);
//or
LocalDate ld = ym.atEndOfMonth();
You can also use a TemporalAdjuster
, for example, for the last day of the month*:
LocalDate ld = ym.atDay(1).with(lastDayOfMonth());
这篇关于Java 8 Time API:如何解析“MM.yyyy”格式的字符串到LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!