java - 如何在 JAVA 中将任何 TimeZone dateTime 转换为 UTC Date
问题描述
我必须使用用户收到的日期时间,并将该时间从给定时间转换为 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);
}
推荐阅读
- java - Vlcj jna unsatisfied link 错误无法定位本机库
- google-cloud-platform - 如何在标头中定义 API 密钥而不是在查询中
- database - 在 SQL Server 上附加数据库,PowerShell 光标进入下一行
- python - 如何在不添加路径的情况下更改 Tkinter 图标
- django - 修复 . .py 文件
- azure-devops-rest-api - 高效查询 DevOps Services REST API
- reactjs - 如何以 Reactjs 形式返回组件?
- bash - 为什么包含管道命令的 shell 变量会失败,而非管道命令按预期工作?
- javascript - 递归地将父节点的标题存储到根
- javascript - 用户无法选中超过 3 个框的条件