ere*_*eOn 32 java formatting android locale date
我是Java和Android开发的新手,所以这可能是一个愚蠢的问题,但我一直在寻找几天,但找不到解决方案:
我尝试输出一个java.util.Date取决于用户的语言环境.
在StackOverflow上搜索引导我:
java.util.Date date = new Date();
String dateString = DateFormat.getDateFormat(getApplicationContext()).format(date);
Run Code Online (Sandbox Code Playgroud)
这输出:
20/02/2011
Run Code Online (Sandbox Code Playgroud)
在我的法语本地化电话上.几乎没问题.
我如何也输出小时,分钟和秒部分的Date,使用用户的语言环境?我一直在寻找Android文档,但找不到任何东西.
非常感谢.
Tia*_*ago 33
使用 android.text.format.DateFormat.getTimeFormat()
参考:http://developer.android.com/reference/android/text/format/DateFormat.html
Bas*_*que 12
ZonedDateTime // Represent a moment as seen in the wall-clock time used by the people of a particular region (a time zone).
.now( ZoneId.of( "Asia/Kolkata" ) ) // Capture the current moment as seen in the specified time zone. Returns a `ZonedDateTime` object.
.format( // Generate text representing the value of this `ZonedDateTime` object.
DateTimeFormatter // Class controlling the generation of text representing the value of a date-time object.
.ofLocalizedDateTime ( FormatStyle.FULL ) // Automatically localize the string representing this date-time value.
.withLocale ( Locale.FRENCH ) // Specify the human language and cultural norms used in localizing.
) // Return a `String` object.
Run Code Online (Sandbox Code Playgroud)
问题中的代码使用了麻烦的旧日期时间类,现在已经遗留下来,取而代之的是Java 8及更高版本中内置的java.time类.
区域设置和时区彼此无关.Locale确定生成String以表示日期时间值时使用的人类语言和文化规范.时区确定用于表示时间线上的时刻的特定区域的挂钟时间.
该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多9个(9)小数的位数).
Instant instant = Instant.now();
Run Code Online (Sandbox Code Playgroud)
2016-10-12T07:21:00.264Z
应用时区来获得ZonedDateTime.我随意选择使用印度时区显示这一刻.同一时刻,时间轴上的同一点.
ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = instant.atZone( z );
Run Code Online (Sandbox Code Playgroud)
2016-10-12T12:51:00.264 + 05:30 [亚/加尔各答]
使用QuébecCanada的区域设置生成字符串.让java.time自动本地化字符串.
Locale l = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( l );
String output = zdt.format ( f ); // Indian time zone with Québécois presentation/translation.
Run Code Online (Sandbox Code Playgroud)
mercredi 2016年10月12日12小时51 IST
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,.Calendar,和java.text.SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
java.text.DateFormat 拥有您可能需要的所有功能:
DateFormat.getDateInstance()
DateFormat.getTimeInstance()
但你需要的是:
DateFormat.getDateTimeInstance()
您还可以指定日期/时间部分的长度和区域设置.最终结果将是:
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, Locale.FRENCH).format(Date);
资料来源:http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
getBestDateTimePattern()DateFormat.getBestDateTimePattern()从android.text.format包根据用户设置的区域设置创建最佳的日期和时间。
例如:骨架EEEE, MMM d, YYYY, jj:mm返回本地化日期和时间,如下所示。
Locale English (India): Monday 9 Sep 2019, 9:33 PM\nLocale English (United States): Monday, Sep 9, 2019, 9:33 PM\nLocale espa\xc3\xb1ol (Estados Unidos): lunes, 9 de sep. de 2019 9:33 PM\nLocale espa\xc3\xb1ol (M\xc3\xa9xico): lunes, 9 de sep de 2019 21:33\nLocale fran\xc3\xa7ais (France): lundi 9 sept. 2019 \xc3\xa0 21:33\nLocale portugu\xc3\xaas (Brasil): segunda-feira, 9 de set de 2019 21:33\nRun Code Online (Sandbox Code Playgroud)\n对于不同的区域设置等等。它还遵循区域设置的 12 小时或 24 小时时间格式。
\n要自定义您自己的骨架,请参阅unicode.org 上的UTS #35模式。
\n这是 Kotlin 中经过测试的示例代码:
\nval skeleton = DateFormat.getBestDateTimePattern(Locale.getDefault(), "EEEE, MMM d, YYYY, jj:mm")\nval formatter = SimpleDateFormat(skeleton, Locale.getDefault()).apply {\n timeZone = TimeZone.getDefault()\n applyLocalizedPattern(skeleton)\n}\nval dateTimeText = formatter.format(calendar.time)\nLog.d("YourClass", "Locale ${Locale.getDefault().displayName}: $dateTimeText")\nRun Code Online (Sandbox Code Playgroud)\n除了Calendar类之外,您还可以使用ZonedDateTime. 只需将其转换为Date对象并将其传递给format(). 例如:Date(zonedDateTime.toInstant().toEpochMilli())
希望有帮助。
\n| 归档时间: |
|
| 查看次数: |
28589 次 |
| 最近记录: |