我有一个字符串“ 2018-07-24T01:30:27”。我想将此解析为具有EST时区的ZonedDateTime,而不更改时间。
我有代码...
String foo = "2018-07-24T01:30:27";
ZoneId zone = ZoneId.of("America/New_York");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return simpleDateFormat.parse(foo).toInstant().atZone(zone);
返回值为
2018-07-24T02:30:27-04:00[America/New_York]
我也尝试过创建此类,但没有帮助
@Configuration
public class TimeZoneConfig {
@PostConstruct
public void init() {
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
System.out.println("Date in UTC: " + new Date().toString());
}
}
请给我一些建议吗?
由于您要ZonedDateTime
考虑使用DateTimeFormatter
from java.time
:
String foo = "2018-07-24T01:30:27";
ZoneId zone = ZoneId.of("America/New_York");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
TemporalAccessor temporalAccessor = dateTimeFormatter.withZone(zone).parse(foo);
return ZonedDateTime.from(temporalAccessor);
返回的ZonedDateTime
将是:
2018-07-24T01:30:27-04:00[America/New_York]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句