我有一个采用此格式的日期字符串 2019-01-01T18:46:19
,我想将其转换为此格式 06:46 pm AEST 01/01/19
使用缩写的澳大利亚时区 例如。我怎样才能在Java中做到这一点?
最佳答案
您可以使用较新的 java.time
API 来执行此任务:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZOneId;
import java.time.format.DateTimeFormatter;
ZoneId zone = ZoneId.of("Australia/Sydney");
LocalDateTime ldt = LocalDateTime.parse("2019-01-01T18:46:19") ;
ZonedDateTime zdt = ZonedDateTime.of(ldt, zone);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm a z dd/MM/yy");
System.out.println(dtf.format(zdt));
输出:
06:46 PM AEDT 01/01/19
注意:您问题中的格式对于您想要日/月/年还是美国月/日/年还是不明确,但我假设澳大利亚跟随英国更喜欢日/月/年。
关于java - 如何在Java中将日期格式化为带有缩写时区的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50770585/