将DataTime转换为本地日期格式

编程入门 行业动态 更新时间:2024-10-24 19:17:28
本文介绍了将DataTime转换为本地日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将DateTime转换为本地日期格式?

How to convert DateTime to Local Date Format?

示例:日期:2014年1月25日12:00:00 AM此日期是美国格式,但在我的机器上,我使用TR格式25/1/2014,并且还假定另一台计算机使用另一种格式.示例:2014/1/25

Example: date: 1/25/2014 12:00:00 AM This date is US format but in my machine I use TR format 25/1/2014 and also assume that another machine use another format Example: 2014/1/25

如何以编程方式将此日期转换为本地日期格式?

How can I convert this date to local date format programmaticaly?

我正在使用Java版本1.7,并且我想使用java.util.Calendar

I am using java version 1.7 and i want to use java.util.Calendar

谢谢.

推荐答案

DateTimeFormatter.ofLocalizedDateTime

使用它来获取ISO年表中特定于语言环境的日期格式.

DateTimeFormatter.ofLocalizedDateTime

Use it to obtain a locale-specific date format for the ISO chronology.

演示:

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Locale; public class Main { public static void main(String[] args) { DateTimeFormatter dtfLocalized = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT); // Test LocalDateTime date = LocalDateTime.now(); System.out.println(dtfLocalized.withLocale(Locale.US).format(date)); System.out.println(dtfLocalized.withLocale(Locale.UK).format(date)); System.out.println(dtfLocalized.withLocale(Locale.CHINESE).format(date)); System.out.println(dtfLocalized.withLocale(Locale.GERMAN).format(date)); System.out.println(dtfLocalized.withLocale(Locale.forLanguageTag("tr")).format(date)); System.out.println(dtfLocalized.withLocale(Locale.getDefault()).format(date)); } }

输出:

5/8/21, 6:54 PM 08/05/2021, 18:54 2021/5/8 下午6:54 08.05.21, 18:54 8.05.2021 18:54 08/05/2021, 18:54

注意:如果只想格式化日期部分,请将 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT)替换为 DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).

Note: If you want to format just the date part, replace DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT) with DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).

详细了解现代日期时间 Trail中的API * :日期时间 .

Learn more about the the modern date-time API* from Trail: Date Time.

*出于任何原因,如果您必须坚持使用Java 6或Java 7,则可以使用 ThreeTen-Backport ,它将大多数 java.time 功能反向移植到Java 6&7.如果您正在为Android项目工作,并且您的Android API级别仍不符合Java-8,请检查可以通过desugaring 和如何在Android Project中使用ThreeTenABP .

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

更多推荐

将DataTime转换为本地日期格式

本文发布于:2023-10-23 00:05:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   日期   格式   DataTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!