怎么把localdatetime转换成GMT?

时间:2020-05-19 04:03:37

标签: java datetime

我有这样的字符串日期

2020-05-19 10:46:09 this is Asia/Jakarta time

我想转换为GMT,在这里输入我的代码

String created = New_tradingHistory_trade.getCreated_at();
                    Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(created);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                    String CreatedGMT = sdf.format(date1);
                    System.out.print(CreatedGMT);

我总是喜欢什么 2020-05-19 10:46:09 2020-05-19 10:46:09我的问题是如何将我的日期转换为GMT?

1 个答案:

答案 0 :(得分:3)

您应该使用java-8 date time api,并停止使用旧版DateSimpleDateFormat

  1. 您输入的字符串位于本地日期时间
  2. 因此使用LocalDateTime将其解析为DateTimeFormatter
  3. 然后将区域Asia/Jakarta上的localDateTime转换为ZoneDateTime
  4. 最后将zoneDateTime转换为等于GTM的{​​{1}}日期时间

UTC