How to convert a UTC date string in "2020-08-07T16:07:13.337248Z` to `MM-dd-yyyy hh:mm“ in est in Java

huangapple go评论129阅读模式
英文:

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

  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. String timeStr = "2020-08-07T16:07:13.337248Z";
  4. String format = "MM-dd-yyyy hh:mm a";
  5. ZonedDateTime zdt = ZonedDateTime
  6. .parse(timeStr, DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("GMT-5")));

如果您想要考虑"夏令时",请执行以下操作:

  1. ZonedDateTime zdt = ZonedDateTime.parse(timeStr,
  2. DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("EST5EDT")));
  3. System.out.println(ldt.format(DateTimeFormatter.ofPattern(format)));

打印结果:

  1. 08-07-2020 11:07 AM

我在格式中添加了a用于上午下午。如果您想在输出中看到EST,则在a之后输入'EST'

英文:

Try this.

  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. String timeStr = "2020-08-07T16:07:13.337248Z";
  4. String format = "MM-dd-yyyy hh:mm a";
  5. ZonedDateTime zdt = ZonedDateTime
  6. .parse(timeStr,DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("GMT-5")));

If you want to allow for Daylight Savings Time then do the following:

  1. ZonedDateTime zdt = ZonedDateTime.parse(timeStr,
  2. DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("EST5EDT")));
  3. System.out.println(ldt.format(DateTimeFormatter.ofPattern(format)));

Prints

  1. 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.

huangapple
  • 本文由 发表于 2020年8月11日 06:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63348775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定