我对Java 8 Time API中的日期解析感到有点气馁.
以前我可以轻松写:
String date = "04.2013";
DateFormat df = new SimpleDateFormat("MM.yyyy");
Date d = df.parse(date);
Run Code Online (Sandbox Code Playgroud)
但是现在如果我使用LocalDate并像这样做:
String date = "04.2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)
我收到一个例外:
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)
Run Code Online (Sandbox Code Playgroud)
如果我将字符串格式更改为"yyyy-MM-dd",即使没有格式化程序,一切也能正常工作:
String date = "2013-04-12";
LocalDate …Run Code Online (Sandbox Code Playgroud) 我有一段代码工作正常,现在不知何故不起作用.
我正在读取csv文件,并在读取格式的时间字段时出错4:38.
我抛出异常的代码是:
LocalTime.parse("4:38", DateTimeFormatter.ofPattern("HH:mm"))
Run Code Online (Sandbox Code Playgroud)
我也试过"H:mm"或者"H:m"为模式但它抛出同样的异常:Text '4:38' could not be parsed at index 0.知道为什么它会在小时数引发异常吗?
我使用的是Java 8.
我对 Java 8 还很陌生,所以这个问题可能有一个明显的答案,但在阅读了其他类似的 SO 问题后,我找不到任何导致我的问题的相似之处。
这是我的班级:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class LocalDateTimeIssue {
public static void main(String[] args) {
String dateTimeString = "18-04-2019 12:14:46";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss", Locale.US);
LocalDateTime ldt = LocalDateTime.parse(dateTimeString , dtf);
System.out.println(ldt.getSecond());
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '18-04-2019 12:14:46' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492) …Run Code Online (Sandbox Code Playgroud)