将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
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.
DateTimeFormatter.ofLocalizedDateTime
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)
.DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT)
with DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.