ans*_*nsh 1 java timezone datetime locale
我的要求是根据国家/地区代码(DE、IT、ES、IE、PT、UK)获取当前日期和时间。有任何可用的Java API吗?
我也尝试设置指定国家/地区的语言环境,但得到相同的输出?
有人能告诉我为什么即使我也在设置语言环境,我也会得到“亚洲/加尔各答”时区吗?
public void timezone() {
final Locale spanishLocale = new Locale("es", "ES");
final Locale ukLocale = new Locale("en", "UK");
final Locale deLocale = new Locale("de", "DE");
final Locale itLocale = new Locale("it", "IT");
final Locale ptLocale = new Locale("pt", "PT");
final Locale ieLocale = new Locale("es", "ES");
final Calendar cal = Calendar.getInstance(spanishLocale);
System.out.println("date and time in spain is " + cal.getTime());
System.out.println(
"date and time in uk is " + Calendar.getInstance(ukLocale).getTime() + " :: " + Calendar.getInstance(ukLocale).getTimeZone());
System.out.println(
"date and time in de is " + Calendar.getInstance(deLocale).getTime() + " :: " + Calendar.getInstance(deLocale).getTimeZone());
System.out.println(
"date and time in it is " + Calendar.getInstance(itLocale).getTime() + " :: " + Calendar.getInstance(itLocale).getTimeZone());
System.out.println(
"date and time in pt is " + Calendar.getInstance(ptLocale).getTime() + " :: " + Calendar.getInstance(ptLocale).getTimeZone());
System.out.println(
"date and time in ie is " + Calendar.getInstance(ieLocale).getTime() + " :: " + Calendar.getInstance(ieLocale).getTimeZone());
}
Run Code Online (Sandbox Code Playgroud)
输出 :
西班牙的日期和时间是 IST 2018 年星期六 4 月 07 日 20:23:52
英国的日期和时间是 IST 2018 年星期六 4 月 7 日 20:23:52 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata", offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
de 中的日期和时间是 Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia /Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
日期和时间是 IST 2018 年星期六 4 月 7 日 20:23:52 :: sun.util.calendar.ZoneInfo[ id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
pt 中的日期和时间是星期六 4 月 7 日 20:23:52 IST 2018 :: sun.util。 calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,最后一条规则=空]
ie 中的日期和时间是 IST 2018 年星期六 4 月 7 日 20:23:52 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,最后一条规则=空]
ZonedDateTime.now( // Capture the current moment as seen by the wall-clock time used by the people of a certain region (a time zone).
ZoneId.of( "Europe/Paris" ) // Specify time zone by official IANA time zone name. https://en.wikipedia.org/wiki/Tz_database
) // Returns a `ZonedDateTime` object.
.withZoneSameInstant( // Adjust from Paris time to Auckland time, just to show that we can. Same moment, different wall-clock time.
ZoneId.of( "Pacific/Auckland" )
) // Returns a new, second `ZonedDateTime` object without changing (“mutating”) the first. Per immutable objects pattern.
.format( // Generate a String representing the value of our `ZonedDateTime`.
DateTimeFormatter.ofLocalizedDateTime( // Let java.time automatically localize.
FormatStyle.FULL // Specify the length/abbreviation of new String.
) // Returns a `DateTimeFormatter` using the JVM’s current default `Locale`. Override this default in next line.
.withLocale( Locale.ITALIAN ) // Locale is unrelated to time zone. Wall-clock time of Auckland, presented in Italian – perfectly reasonable depending on the needs of your user.
) // Returns a String object holding text that represents the value of our `ZonedDateTime` object.
Run Code Online (Sandbox Code Playgroud)
多梅尼卡 2018 年 4 月 8 日 08:48:16 Ora standard della Nuova Zelanda
基于国家代码的当前日期和时间
没有办法。
国家和时区之间没有直接联系。
地理上的大国往往跨越多个时区,因此每个地区的挂钟时间都接近太阳时(意思是“中午”是太阳在头顶上的时候)。今天的印度在其广阔的土地上使用一个时区是不寻常的。
此外,一个国家内通常有使用不同时区的飞地。这通常与加入或拒绝夏令时 (DST) 相关。例如,在美国,亚利桑那州选择退出 DST 的愚蠢行为。然而在亚利桑那州内是纳瓦霍民族的一部分,它确实参与了夏令时。因此,无论是国家还是州/省都无法确定时区。
此外,除了当前的offset-from-UTC之外,还有更多需要考虑的事情。一个时区是过去,现在和未来的偏移量变化的历史使用区域的人。夏令时 (DST) 中的转换是区域中偏移量变化的常见原因之一。确定更早/更晚的时刻需要一个时区来应用这些历史调整。
所以忘记国家。以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的伪区域,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Run Code Online (Sandbox Code Playgroud)
您可以根据用户当前的地理位置或国家/地区代码猜测或提供一个简短列表供用户选择。请参阅此页面上的答案以获取数据文件,其中列出了自 1970 年以来使用的大约 350 个时区及其经度/纬度和国家/地区代码。
摘抄:
ZonedDateTime.now( // Capture the current moment as seen by the wall-clock time used by the people of a certain region (a time zone).
ZoneId.of( "Europe/Paris" ) // Specify time zone by official IANA time zone name. https://en.wikipedia.org/wiki/Tz_database
) // Returns a `ZonedDateTime` object.
.withZoneSameInstant( // Adjust from Paris time to Auckland time, just to show that we can. Same moment, different wall-clock time.
ZoneId.of( "Pacific/Auckland" )
) // Returns a new, second `ZonedDateTime` object without changing (“mutating”) the first. Per immutable objects pattern.
.format( // Generate a String representing the value of our `ZonedDateTime`.
DateTimeFormatter.ofLocalizedDateTime( // Let java.time automatically localize.
FormatStyle.FULL // Specify the length/abbreviation of new String.
) // Returns a `DateTimeFormatter` using the JVM’s current default `Locale`. Override this default in next line.
.withLocale( Locale.ITALIAN ) // Locale is unrelated to time zone. Wall-clock time of Auckland, presented in Italian – perfectly reasonable depending on the needs of your user.
) // Returns a String object holding text that represents the value of our `ZonedDateTime` object.
Run Code Online (Sandbox Code Playgroud)
我也尝试设置指定国家/地区的语言环境,但得到相同的输出?
区域设置有什么做的时区和偏移从-UTC。区域设置确定在生成字符串以表示日期时间值时本地化中使用的人类语言和文化规范。所以语言环境不影响意义。
要本地化,请指定:
FormatStyle 确定字符串的长度或缩写。Locale 确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。例如,考虑一位来自魁北克的工程师在印度参加一个会议。她将希望使用Asia/Kolkata时区来查看会议日程表,以匹配墙上时钟上看到的时间。
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
LocalTime lt =LocalTime.of( 14 , 0 ) ;
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ; // Conference session start.
Run Code Online (Sandbox Code Playgroud)
2018-01-23T14:00+05:30[亚洲/加尔各答]
但我们的工程师更愿意阅读以她的母语法语Locale.CANADA_FRENCH.
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );
Run Code Online (Sandbox Code Playgroud)
狂欢节 2018 年 1 月 23 日 à 14:00:00 印度标准时间
我的要求是根据国家/地区代码(DE、IT、ES、IE、PT、UK)获取当前日期和时间
您可以ZonedDateTime使用不同的时区创建一个新的,但内部具有相同的时刻( Instant)。因此,您将看到不同的挂钟时间,但仍会继续引用时间线上的同一时刻、同一点。
ZoneId zMadrid = ZoneId.of( "Europe/Madrid" ) ;
ZonedDateTime zdtMadrid = zdt.withZoneSameInstant( zMadrid) ; // Same moment, different wall-clock time.
Run Code Online (Sandbox Code Playgroud)
zdtMadrid.toString(): 2018-01-23T09:30+01:00[欧洲/马德里]
也许用日语本地化西班牙区的时刻。
DateTimeFormatter fJapan = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.JAPAN );
String outputJapan = zdtMadrid.format( fJapan ); // Zone & locale are unrelated, orthogonal issues.
System.out.println(outputJapan);
Run Code Online (Sandbox Code Playgroud)
所以我们得到了用日语/文化呈现的马德里时间。
2018?1?23?? 9?30?00? ?????????
一般来说,您应该将 UTC 值用于您的大部分业务逻辑、日志记录、存储和交换。只需Instant从我们的ZonedDateTime. 无论是Instant与ZonedDateTime表示相同的时刻,在时间轴上的相同点,但观察使用不同的挂钟时间。
Instant instant = zdt.toInstant() ; // Capture current moment in UTC.
Run Code Online (Sandbox Code Playgroud)
走向另一个方向:
ZonedDateTime zdt = instant.atZone( "Europe/Helsinki" ) ; // Same simultaneous moment, different wall-clock time.
Run Code Online (Sandbox Code Playgroud)
麻烦的旧日期时间类在几年前被java.time类取代。而不是Calendar使用ZonedDateTime.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Run Code Online (Sandbox Code Playgroud)
在 UTC 中,使用Instant. 该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。
Instant instant = Instant.now() ;
Run Code Online (Sandbox Code Playgroud)
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。Hibernate 5 & JPA 2.2 支持java.time。java.sql.*
从哪里获得 java.time 类?
| 归档时间: |
|
| 查看次数: |
2700 次 |
| 最近记录: |