如何在JAVA中将任何TimeZone dateTime转换为UTC日期 [英] How to convert any TimeZone dateTime to UTC Date in JAVA
问题描述
我必须使用用户收到的日期时间,并将该时间从给定时间转换为UTC.目前,所有解决方案都在起作用,但不是我需要的.首先,它将转换为时区,但我不想最初进行转换.我必须在我的日期上设置该时区,然后将该时间转换为UTC.现在,每个解决方案都表明我必须传递日期和时间,然后才能使用提供的时区更新日期和时间,然后转换为UTC.这不是我的问题.
I have to use the date time which I receive by user, and convert that time to UTC from the given time. Right now, all the solutions are working, but not as I needed. First, it converts to the time zone, but I don't want to initially convert. I have to set that timezone on my date and then convert that time to UTC. Right now, each and every solution shows that I have to pass date and time, and then I am getting updated date and time with the time zone which I provide, and then converts to the UTC. This was not my question.
我的用户将向我发送任何时区的日期时间,并且我的服务器将在UTC时区运行.我将从前端获取区域ID.我想将该日期时间转换为UTC并用我的其他条件进行验证.我可能会在一个请求中获得UTC-7时间,然后在下一个请求后获得UTC + 5:30.因此,所有时间都应转换为UTC,而我无法在日期上设置时区.
My user will send me date time of any timezone, and my server will be running on UTC time zone. I will get the zone id from the front end. I want to convert that date time to UTC and verify with my other conditions. It might be possible that I will get UTC-7 time in one request, and then after in next request I will get UTC+5:30. So, all the time should be converted to the UTC and I am not able to set timezone on date.
我想将特定时区设置为日期,但我正在获取该时区的日期和时间,但是我无法设置确切的时区.在这里,我要添加我的代码,请告诉我哪里错了:
I wanted to set the specific timezone to date, and I am getting the date and time of that timezone, but I'm not able to set the exact timezone. Here, I am adding my code please tell me where I am wrong:
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,我想对其进行更新.
Here you can see that my DateTime is converted, but time zone is still UTC and I wanted to update that.
推荐答案
在这里,我将使用Java 8日期/时间API编写它.
Here, is how I would write it using Java 8 Date/Time APIs.
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-较短的版本
Using Just Java 8 Date/Time APIs - Shorter Version
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);
}
这篇关于如何在JAVA中将任何TimeZone dateTime转换为UTC日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!