java - 根据语言环境(国家/地区)获取当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5593715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java - Getting current date and time based on the locale (country)
提问by Sriram
I would like to fetch the current date / time based on the locale. If I pass locale object, I need to get the relevant date / time of the country.
我想根据语言环境获取当前日期/时间。如果我传递语言环境对象,我需要获取该国家/地区的相关日期/时间。
回答by dehasi
Since Java 8 you have LocalDateTime
and ZonedDateTime
从 Java 8 开始,您拥有LocalDateTime
和ZonedDateTime
So you can do the same as @Babar said:
所以你可以像@Babar 所说的那样做:
Get a zone id
获取区域 ID
ZoneId zoneId = ZoneId.of("Europe/Brussels");
ZoneId zoneId = ZoneId.of("Europe/Brussels");
And then get date and time
然后获取日期和时间
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
回答by zawhtut
public static void main(String[] arg){
String[] timeZoneIDList = TimeZone.getAvailableIDs();
//List of Time Zones
for(String timeZoneID:timeZoneIDList){
System.out.println(timeZoneID);
}
//What time is it in Fiji
Calendar fijiCalendar = Calendar.getInstance(TimeZone.getTimeZone("Pacific/Fiji"));
System.out.println("Time in Fiji->"+fijiCalendar.get(Calendar.HOUR)+":"+fijiCalendar.get(Calendar.MINUTE));
}
回答by Babar
If you use Joda Timethen following should work. (Don't have the compiler at hand)
如果您使用Joda Time,那么以下应该可以工作。(手头没有编译器)
DateTimeZone tz = DateTimeZone.forID("America/Winnipeg");
DateTime dt = new DateTime(tz);
You can get the Timezone Ids here. Please check the link given by dantuch too.
您可以在此处获取时区 ID 。请检查 dantuch 提供的链接。