英文:
How to convert a UTC date string in "2020-08-07T16:07:13.337248Z` to `MM-dd-yyyy hh:mm`` in est in Java
问题
我有一个字符串,其中包含以UTC表示的日期时间:“2020-08-07T16:07:13.337248Z”,我想在Java中将其转换为EST格式为“MM-dd-yyyy hh:mm”。有人可以帮助我吗?
英文:
I have a string which is the date time represented in UTC "2020-08-07T16:07:13.337248Z" , I would like to convert this into EST in format "MM-dd-yyyy hh:mm" in Java . Can anyone help me ?
答案1
得分: 1
- 尝试这个。
- 使用DateTimeFormatter的ISO_DATE_TIME格式转换为LocalDateTime。
- 然后再转换为所需格式。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
String timeStr = "2020-08-07T16:07:13.337248Z";
String format = "MM-dd-yyyy hh:mm a";
ZonedDateTime zdt = ZonedDateTime
.parse(timeStr, DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("GMT-5")));
如果您想要考虑"夏令时",请执行以下操作:
ZonedDateTime zdt = ZonedDateTime.parse(timeStr,
DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("EST5EDT")));
System.out.println(ldt.format(DateTimeFormatter.ofPattern(format)));
打印结果:
08-07-2020 11:07 AM
我在格式中添加了a
用于上午
或下午
。如果您想在输出中看到EST
,则在a
之后输入'EST'
。
英文:
Try this.
- Converts to LocalDateTime using the DateTimeFormatter.ISO_DATE_TIME format.
- Then converts back to desired format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
String timeStr = "2020-08-07T16:07:13.337248Z";
String format = "MM-dd-yyyy hh:mm a";
ZonedDateTime zdt = ZonedDateTime
.parse(timeStr,DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("GMT-5")));
If you want to allow for Daylight Savings Time
then do the following:
ZonedDateTime zdt = ZonedDateTime.parse(timeStr,
DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("EST5EDT")));
System.out.println(ldt.format(DateTimeFormatter.ofPattern(format)));
Prints
08-07-2020 11:07 AM
I added the a
in the format for am
or pm
. If you want to see EST
in the output, then put in 'EST'
after the a
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论