解析 java 8 中的时钟时间
Parse clock time in java 8
我想知道是否可以在 Java 8 中解析时钟时间 hour:minute:second?
例如
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
我试过了,但遇到了这个异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
LocalTime.parse
因为你只有一次使用 LocalTime
class。无需根据您的情况定义格式化模式。
String str = "12:22:10";
LocalTime time = LocalTime.parse(str);
查看代码 run live at IdeOne.com。
有关详细信息,请参阅 Oracle Tutorial。
LocalTime.parse("04:17");
我想知道是否可以在 Java 8 中解析时钟时间 hour:minute:second?
例如
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
我试过了,但遇到了这个异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
LocalTime.parse
因为你只有一次使用 LocalTime
class。无需根据您的情况定义格式化模式。
String str = "12:22:10";
LocalTime time = LocalTime.parse(str);
查看代码 run live at IdeOne.com。
有关详细信息,请参阅 Oracle Tutorial。
LocalTime.parse("04:17");