How to get date or datetime from string in java

java-datetime (2 Part Series)

1 How to get date or datetime from string in java
2 How to get mockable current time in java enterprise

To get the date or dateime from String since Java 8 you can use the parse method of both LocalDate and LocalDateTime which takes two parameters: the string and a DateFormatter which you can specify as in the following example:

LocalDate date = LocalDate.parse("1969-12-31", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDateTime dateTime = LocalDateTime.parse("1983-01-08 20:54", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));

Enter fullscreen mode Exit fullscreen mode


Shared with ️ from Codever.   use the copy to mine functionality to add it to your personal snippets collection.

原文链接:How to get date or datetime from string in java

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容