tl;dr
您所需的格式恰好在 ISO 8601 标准中定义。这java.time类在解析/生成文本时默认使用标准格式。
LocalDate.now().toString()
2019-01-23
本地化到在美国的英语 http://www.localeplanet.com/java/en-US/:
LocalDate
.now(
ZoneId.of( "America/Chicago" )
)
.format(
DateTimeFormatter.ofLocalizedDate(
FormatStyle.FULL // or LONG or MEDIUM or SHORT
)
.withLocale(
new Locale( "en" , "US" ) // English language, with cultural norms of United States.
)
)
For 突尼斯的阿拉伯语 http://www.localeplanet.com/java/ar-TN/index.html, use:
new Locale( "ar" , "TN" )
避免遗留日期时间类
您正在使用与最早版本的 Java 捆绑在一起的糟糕的日期时间类。他们完全被java.time采用 JSR 310 的类。对于早期的 Android,请参阅下面底部的项目符号。
Locale
Android 没有 Locale.Arabic 格式
众多语言环境中只有少数具有命名常量。学习指定一个Locale
由标准语言代码和国家(文化)代码,如Locale https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html班级。欲了解更多信息,请参阅这个相关问题 https://stackoverflow.com/q/20844525/642706.
例如,对于符合沙特阿拉伯文化规范的阿拉伯语 http://www.localeplanet.com/java/ar-SA/index.html, use:
Locale locale = new Locale( "ar" , "SA" ) ; // Arabic language. Saudi Arabia cultural norms.
我们可以使用该语言环境来本地化代表某个时刻的文本。
ZoneId z = ZoneId.of( "Pacific/Auckland" ); // By the way, time zone has *nothing* to do with locale, orthogonal issues.
ZonedDateTime zdt = ZonedDateTime.now( z );
Locale locale = new Locale( "ar" , "SA" );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );
运行时。
2019 年 18 月 12:14:00 于 2019 年 12 月 18 日
要查看系统上可用的所有区域设置定义,请运行此代码。
for ( Locale locale : Locale.getAvailableLocales() ) {
System.out.println( locale.toString() + " Name: " + locale.getDisplayName( Locale.US ) );
}
今天的英语日期
The LocalDate https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html类表示仅日期值,没有日期和时间时区 https://en.wikipedia.org/wiki/Time_zone or 与 UTC 的偏移量 https://en.wikipedia.org/wiki/UTC_offset.
时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,午夜过后几分钟法国巴黎 https://en.wikipedia.org/wiki/Europe/Paris又是新的一天,但仍是“昨天”魁北克省 蒙特利尔 https://en.wikipedia.org/wiki/America/Montreal.
如果未指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能随时改变 https://docs.oracle.com/javase/10/docs/api/java/util/TimeZone.html#getDefault()在运行时(!),所以你的结果可能会有所不同。最好明确指定您想要/预期的时区作为参数。
指定一个正确的时区名称 https://en.wikipedia.org/wiki/List_of_tz_zones_by_name格式为Continent/Region
, 例如America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
。切勿使用 2-4 个字母的缩写,例如EST
or IST
像他们那样not真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
如果您想使用 JVM 当前的默认时区,请询问它并作为参数传递。如果省略,代码读起来会变得不明确,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样没有意识到这个问题。
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
或者指定一个日期。您可以通过数字设置月份,1 月至 12 月的合理编号为 1-12。
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
或者,更好的是,使用Month https://docs.oracle.com/javase/10/docs/api/java/time/Month.html预定义的枚举对象,一年中的每个月都有一个。提示:使用这些Month
整个代码库中的对象而不仅仅是整数,以使您的代码更加自记录,确保有效值,并提供类型安全 https://en.wikipedia.org/wiki/Type_safety。同上Year https://docs.oracle.com/javase/8/docs/api/java/time/Year.html & YearMonth https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
要生成本地化为英语的文本,请使用DateTimeFormatter.ofLocalizedDate
.
FormatStyle formatStyle = FormatStyle.FULL ;
Locale locale = new Locale( "en" , "US" ) ; // English language, United States cultural norms.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( formatStyle ).withLocale( locale );
String output = localDate.format( f ) ;
About java.time
The java.time http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html框架内置于 Java 8 及更高版本中。这些课程取代了麻烦的旧课程legacy https://en.wikipedia.org/wiki/Legacy_system日期时间类,例如java.util.Date https://docs.oracle.com/javase/10/docs/api/java/util/Date.html, Calendar https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html, & SimpleDateFormat http://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html.
The 乔达时间 http://www.joda.org/joda-time/项目,现在在维护模式 https://en.wikipedia.org/wiki/Maintenance_mode,建议迁移到java.time http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html类。
要了解更多信息,请参阅甲骨文教程 http://docs.oracle.com/javase/tutorial/datetime/TOC.html。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310 https://jcp.org/en/jsr/detail?id=310.
您可以交换java.time对象直接与您的数据库。用一个JDBC驱动程序 https://en.wikipedia.org/wiki/JDBC_driver符合JDBC 4.2 http://openjdk.java.net/jeps/170或稍后。不需要字符串,不需要java.sql.*
类。
从哪里获取 java.time 类?
-
Java SE 8 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8, Java SE 9 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9, Java SE 10 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_10, Java SE 11 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_11, and later - Part of the standard Java API with a bundled implementation.
-
Java SE 6 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6 and Java SE 7 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7
- 大部分的java.time功能向后移植到 Java 6 和 7三十后端口 http://www.threeten.org/threetenbp/.
-
Android https://en.wikipedia.org/wiki/Android_(operating_system)
- 更高版本的 Android 捆绑实现java.time类。
- 对于早期的 Android (三十ABP项目适应三十后端口 http://www.threeten.org/threetenbp/(上文提到的)。看如何使用 ThreeTenABP... http://stackoverflow.com/q/38922754/642706.
The 三十额外 http://www.threeten.org/threeten-extra/项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如Interval http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/Interval.html, YearWeek http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearWeek.html, YearQuarter http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearQuarter.html, and more http://www.threeten.org/threeten-extra/apidocs/index.html.