2

GMT SQL 服务器中的日期时间数据类型:列名 = PostedDateTime 值 = 2019-09-30 17:46:04.600

我正在尝试使用 JAVA 代码将该日期时间值转换为 EST 时间戳所以输出应该是:2019-09-30 13:46:04

任何想法如何转换这个,请包括需要导入的包?

到目前为止,我有这个:

SimpleDateFormat dr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat dr1 = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
String dateString = obj.getStringProperty("S_DISCUSSION_POSTEDDATETIME");
Date date = dr.parse(dateString);           
strRetBuffer.append(obj.getStringProperty("S_DISCUSSION_AUTHOR") + ": " + dr1.format(date) + ": " + obj.getStringProperty("S_DISCUSSION_TOPICNAME")+": " +obj.getStringProperty("S_DISCUSSION_BODY") + "\n\r" );
4

2 回答 2

3

首先将输入字符串解析为LocalDateTime使用DateTimeFormatter

String date = "2019-09-30 17:46:04.600";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime local = LocalDateTime.parse(date, formatter);

然后转换LocalDateTimeZonedDateTime带时区GMT,即UTC+0

ZonedDateTime zone = ZonedDateTime.of(local, ZoneId.of("UTC"));

最后将其转换ZonedDateTimeLocalDateTimewithUS/Eastern和输出格式

String output = zone.withZoneSameInstant(ZoneId.of("US/Eastern")).toLocalDateTime().format(outputFormat);
于 2019-09-30T20:58:56.517 回答
0
        String x = "2019-09-30 17:46:04.600";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = formatter.parse(x);
        System.out.println(date);
        System.out.println(formatter.format(date));
        outputFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        System.out.println(outputFormat.format(date));
于 2019-10-02T12:48:52.620 回答