-5 java datetime date datetime-format date-formatting
好吧,我试图寻找很多问题,但找不到相关的东西。我有一个包含以下数据的字符串:
String sDate = "2018-01-17 00:00:00";
Run Code Online (Sandbox Code Playgroud)
这来自一个应用程序,我需要将其转换为以下日期格式
17-01-2018
我浏览了这个链接,但无法联系。
有人可以帮忙吗..?
如果您使用的是 Java 8,则可以使用java.time库和:
String sDate = "2018-01-17 00:00:00";
//Step one : convert the String to LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(sDate, formatter);
//Step two : format the result date to dd-MM-yyyy
String result = date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
Run Code Online (Sandbox Code Playgroud)
输出
17-01-2018
Run Code Online (Sandbox Code Playgroud)
另一个过度工程解决方案(它仅适用于您的情况)您可以从默认格式中受益LocalDateTime:
String result = LocalDateTime.parse(sDate.replace(" ", "T"))
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2488 次 |
| 最近记录: |