微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在JAVA中将任何TimeZone dateTime转换为UTC日期

如何解决如何在JAVA中将任何TimeZone dateTime转换为UTC日期

我必须使用用户收到的日期时间,并将该时间从给定时间转换为UTC。现在,所有解决方案都在起作用,但不是我需要的。首先,它会转换为时区,但我不想最初进行转换。我必须在日期上设置该时区,然后将其转换为UTC。现在,每个解决方案都表明我必须传递日期和时间,然后才能使用提供的时区获取日期和时间的更新,然后转换为UTC。这不是我的问题。

我的用户将向我发送任何时区的日期时间,我的服务器将在UTC时区运行。我将从前端获取区域ID。我想将该日期时间转换为UTC并用我的其他条件进行验证。我可能会在一个请求中获得UTC-7时间,然后在下一个请求中获得UTC + 5:30。因此,所有时间都应转换为UTC,而我无法在日期上设置时区。

我想将特定的时区设置为日期,我正在获取该时区的日期和时间,但是我无法设置确切的时区。在这里,我要添加代码,请告诉我哪里错了:

Calendar laCalendar = Calendar.getInstance();
laCalendar.set(2020,8,14,00,00);
Date losAngelesDate = laCalendar.getTime();
System.out.println("LA Date1===>" + losAngelesDate);

SimpleDateFormat simpleTimeFormatForUSA = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
simpleTimeFormatForUSA.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String americaDateString = simpleTimeFormatForUSA.format(laCalendar.getTime());
SimpleDateFormat dateFormatUSA = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
losAngelesDate = dateFormatUSA.parse(americaDateString);

System.out.println("LA Date2===>" + losAngelesDate);
Date utcDate = losAngelesDate;
Calendar calendar = Calendar.getInstance();
calendar.setTime(utcDate);
TimeZone timeZone = TimeZone.getTimeZone("UTC");
SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
simpleTimeFormat.setTimeZone(timeZone);
String utcTime = simpleTimeFormat.format(calendar.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
utcDate = dateFormat.parse(utcTime);
System.out.println("UTC date====>" + utcDate);

我的输出

LA Date1===>Mon Sep 14 00:00:56 UTC 2020
LA Date2===>Sun Sep 13 17:00:00 UTC 2020
UTC date====>Sun Sep 13 17:00:00 UTC 2020

在这里您可以看到我的DateTime已转换,但是时区仍然是UTC,我想对其进行更新。

解决方法

这是我如何使用Java 8日期/时间API编写它的方法。

    public static void main(String[] args)
    {
        System.out.println("UTC");
        toTimeZone(new Date(),TimeZone.getTimeZone(ZoneId.of("UTC")));
        
        System.out.println("America/Los_Angeles");
        toTimeZone(new Date(),TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles")));
    }

    public static void toTimeZone(Date date,TimeZone timeZone)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Instant epochMillisInstant = Instant.ofEpochMilli(calendar.getTimeInMillis());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(epochMillisInstant,ZoneId.systemDefault());
        ZonedDateTime dateTimeToConvert = ZonedDateTime.of(localDateTime,ZoneId.systemDefault());
        ZonedDateTime dateTimeWithTimeZone = dateTimeToConvert.withZoneSameInstant(timeZone.toZoneId());
        String formattedDateForNewTimeZone = dateTimeWithTimeZone.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
        System.out.println(formattedDateForNewTimeZone);
    }

仅使用Java 8日期/时间API-较短的版本

public static void main(String[] args) {
        System.out.println("UTC");
        toTimeZone(LocalDateTime.now(),"UTC");
        System.out.println("America/Los_Angeles");
        toTimeZone(LocalDateTime.now(),"America/Los_Angeles");
    }

    private static void toTimeZone(LocalDateTime localDateTime,String timeZone) {
        ZonedDateTime localDateTimeToConvert = ZonedDateTime.of(localDateTime,ZoneId.systemDefault());
        ZonedDateTime zonedDateTime = localDateTimeToConvert.withZoneSameInstant(ZoneId.of(timeZone));
        String formattedDateForNewTimeZone = zonedDateTime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
        System.out.println(formattedDateForNewTimeZone);
    }
,

在将“日历”转换为日期之前,请将“时区”设置为您的首选项。

例如:

import java.util.Calendar;
import java.util.TimeZone;

public class Timezone{

    public static void main(String []args){
       Calendar laCalendar = Calendar.getInstance();
       TimeZone timezone = TimeZone.getTimeZone("UTC");
       laCalendar.set(2020,8,14,00,00);
       laCalendar.setTimeZone(timezone);
       java.util.Date losAngelesDate = laCalendar.getTime();
       System.out.println(losAngelesDate.toString());
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。