如何将 GMT 日期时间 SQL 数据类型转换为 JAVA EST 时间戳 JDK 1.7
How to convert GMT datetime SQL data type to JAVA EST timestamp on JDK 1.7
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" );
首先使用DateTimeFormatter
将输入字符串解析为LocalDateTime
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);
然后将 LocalDateTime
转换为 ZonedDateTime
时区 GMT
即 UTC+0
ZonedDateTime zone = ZonedDateTime.of(local, ZoneId.of("UTC"));
最终将 ZonedDateTime
转换为 LocalDateTime
并使用 US/Eastern
和输出格式
String output = zone.withZoneSameInstant(ZoneId.of("US/Eastern")).toLocalDateTime().format(outputFormat);
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));
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" );
首先使用DateTimeFormatter
LocalDateTime
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);
然后将 LocalDateTime
转换为 ZonedDateTime
时区 GMT
即 UTC+0
ZonedDateTime zone = ZonedDateTime.of(local, ZoneId.of("UTC"));
最终将 ZonedDateTime
转换为 LocalDateTime
并使用 US/Eastern
和输出格式
String output = zone.withZoneSameInstant(ZoneId.of("US/Eastern")).toLocalDateTime().format(outputFormat);
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));