Java 如何制作自定义日期格式,该格式将根据区域设置(US或GB)更改MMdd顺序
我们知道我们和GB有不同的日期顺序。US是MM DD YYYY,但GB是DD MM YYYY 使用SimpleDataFormat()类,我们可以定义我们的模式,例如“MM.dd.yyyy HH:MM:ss.SSS”,即。 新的简化格式(“MM.dd.yyyy HH:MM:ss.SSS”,myLocale);但这种模式是固定的。月份和日期的顺序不会随US语言环境和GB语言环境而改变 有没有办法定义自定义格式,并且区域设置也会影响月份和日期的显示顺序 使用它可以获取区域设置和时间顺序的日期和时间样式的格式模式 演示:Java 如何制作自定义日期格式,该格式将根据区域设置(US或GB)更改MMdd顺序,java,date,internationalization,locale,datetime-format,Java,Date,Internationalization,Locale,Datetime Format,我们知道我们和GB有不同的日期顺序。US是MM DD YYYY,但GB是DD MM YYYY 使用SimpleDataFormat()类,我们可以定义我们的模式,例如“MM.dd.yyyy HH:MM:ss.SSS”,即。 新的简化格式(“MM.dd.yyyy HH:MM:ss.SSS”,myLocale);但这种模式是固定的。月份和日期的顺序不会随US语言环境和GB语言环境而改变 有没有办法定义自定义格式,并且区域设置也会影响月份和日期的显示顺序 使用它可以获取区域设置和时间顺序的日期和时间样
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.UK), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.US), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.UK))));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.US))));
}
static String getFullPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.FULL, FormatStyle.FULL,
IsoChronology.INSTANCE, locale);
}
static String getShortPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT,
IsoChronology.INSTANCE, locale);
}
}
Monday, 1 February 2021 at 21:05:34 Greenwich Mean Time
Monday, February 1, 2021 at 9:05:34 PM Greenwich Mean Time
01/02/2021, 21:05
2/1/21, 9:05 pm
输出:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.UK), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.US), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.UK))));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.US))));
}
static String getFullPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.FULL, FormatStyle.FULL,
IsoChronology.INSTANCE, locale);
}
static String getShortPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT,
IsoChronology.INSTANCE, locale);
}
}
Monday, 1 February 2021 at 21:05:34 Greenwich Mean Time
Monday, February 1, 2021 at 9:05:34 PM Greenwich Mean Time
01/02/2021, 21:05
2/1/21, 9:05 pm
从了解有关现代日期时间API的更多信息
注意:java.util的日期时间API及其格式化APISimpleDateFormat
已经过时且容易出错。建议完全停止使用,并切换到
- 无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7
- 如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.UK), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.US), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.UK))));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.US))));
}
static String getFullPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.FULL, FormatStyle.FULL,
IsoChronology.INSTANCE, locale);
}
static String getShortPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT,
IsoChronology.INSTANCE, locale);
}
}
Monday, 1 February 2021 at 21:05:34 Greenwich Mean Time
Monday, February 1, 2021 at 9:05:34 PM Greenwich Mean Time
01/02/2021, 21:05
2/1/21, 9:05 pm
输出:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.UK), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getFullPattern(Locale.US), Locale.ENGLISH)));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.UK))));
System.out.println(now.format(DateTimeFormatter.ofPattern(getShortPattern(Locale.US))));
}
static String getFullPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.FULL, FormatStyle.FULL,
IsoChronology.INSTANCE, locale);
}
static String getShortPattern(Locale locale) {
return DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT,
IsoChronology.INSTANCE, locale);
}
}
Monday, 1 February 2021 at 21:05:34 Greenwich Mean Time
Monday, February 1, 2021 at 9:05:34 PM Greenwich Mean Time
01/02/2021, 21:05
2/1/21, 9:05 pm
从了解有关现代日期时间API的更多信息
注意:java.util的日期时间API及其格式化APISimpleDateFormat
已经过时且容易出错。建议完全停止使用,并切换到
- 无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7
- 如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认
Date
、Calendar
和SimpleDateFormat
。仅使用JSR 310中定义的java.time类。