gpt4 book ai didi

java - JS/Java 任何城市的夏令时时间

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:01 25 4
gpt4 key购买 nike

我想要一个可以按以下方式工作的代码。
显示需要自动处理夏令时的8-10个城市的时间。

For example : Melbourne observes DST, a API using which I can fetch the current time based on location without any hardcode done for DST calculation.

目前正在运行的代码基于硬编码值。它选择系统时间转换为 GMT,然后根据位置添加/减去偏移量。

然后根据日期计算夏令时并适当添加。

但是我想使用一些 API 来删除硬编码值,我正在尝试使用下面的代码。

The below code giving time for IST/GMT, but when I put AEST/AEDT it doesnt give the correct time.


public class DateFormatter {

public static String formatDateToString(Date date, String format,
String timeZone) {
// null check
if (date == null) return null;
// create SimpleDateFormat object with input format
SimpleDateFormat sdf = new SimpleDateFormat(format);
// default system timezone if passed null or empty
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
// set timezone to SimpleDateFormat
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
// return Date in required format with timezone as String
return sdf.format(date);
}

public static void main(String[] args) {
//Test formatDateToString method
Date date = new Date();
System.out.println("Default Date:"+date.toString());
System.out.println("System Date: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", null));
System.out.println("System Date in PST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "PST"));
System.out.println("System Date in IST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "IST"));
System.out.println("System Date in GMT: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "GMT"));
System.out.println("System Date in CT: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "BDT"));
}

}

最佳答案

import java.time.LocalTime;
import java.time.ZoneId;

public class Main {
public static void main(String... args) {
ZoneId zone2 = ZoneId.of("America/Santiago");
LocalTime now2 = LocalTime.now(zone2);
System.out.println(now2);
}
}

Use the below link to know the timezones

http://joda-time.sourceforge.net/timezones.html

关于java - JS/Java 任何城市的夏令时时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29138232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com