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评论104阅读模式
英文:

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

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.

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.

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:

确定