我有以下字符串:
Mon Sep 14 15:24:40 UTC 2009我需要将其格式化为如下字符串:
14/9/2009我如何在Java中做到这一点?
发布于 2010-11-14 19:51:02
使用SimpleDateFormat (单击javadoc链接查看模式)将一种模式中的字符串解析为值得填充的Date,并使用另一种模式将解析的Date格式化为另一种模式中的字符串。
String string1 = "Mon Sep 14 15:24:40 UTC 2009";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy").parse(string1);
String string2 = new SimpleDateFormat("d/M/yyyy").format(date);
System.out.println(string2); // 14/9/2009发布于 2010-11-14 19:46:29
您可以使用SimpleDateFormat类将您拥有的字符串转换为日期对象。日期格式可以在构造函数中给出。format方法将字符串转换为date对象。
获取date对象后,您可以按所需的方式对其进行格式化。
发布于 2017-08-01 15:05:50
在java8和更高版本中有一个liner。
String localDateTime= LocalDateTime.parse("Mon Sep 14 15:24:40 UTC 2009", DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss z yyyy")).format(DateTimeFormatter.ofPattern("d/M/yyyy"));https://stackoverflow.com/questions/4177291
复制相似问题