概述
{@code DateFormat} is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner.
The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for formatting (i.e., date → text), parsing (text → date), and normalization.
The date is represented as a <code>Date</code> object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
DateFormat 用于格式化Date/time & 解析text到Date;
DateFormat的子类SimpleDateFormat,允许将Date转换为text,解析text为Date;
date表示 Date对象 或 自1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数;
{@code DateFormat} provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles.
The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}.
DateFormat提供了许多格式化样式:FULL、LONG、MEDIUM、SHORT;
{@code DateFormat} helps you to format and parse dates for any locale.
Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.
DateFormat可以 格式化&解析 任何环境的 date;
DateFormat 独立于 阴历/阳历 的月份、星期等;
To format a date for the current Locale, use one of the static factory methods: String myString = DateFormat.getDateInstance().format(myDate);
可以使用static工厂方法 DateFormat.getDateInstance().format(myDate) 格式化当前区域的date;
To format a date for a different Locale, specify it in the call to {@link #getDateInstance(int, Locale) getDateInstance()}.
eg:DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
要为不同的区域设置的date 格式化,需要在调用时指定getDateInstance(int, Locale);
Date date = new Date(); String format2 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA).format(date); System.out.println(format2); // 2023年12月20日 星期三 String format3 = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA).format(date); System.out.println(format3); // 2023年12月20日 String format4 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA).format(date); System.out.println(format4); // 2023-12-20 String format5 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA).format(date); System.out.println(format5); // 23-12-20
You can use a DateFormat to parse also.
Date myDate = df.parse(myString);
Date parse = DateFormat.getDateTimeInstance().parse("2023-12-23 12:23:34"); System.out.println(parse); // Sat Dec 23 12:23:34 CST 2023
Use {@code getDateInstance} to get the normal date format for that country.
There are other static factory methods available.
Use {@code getTimeInstance} to get the time format for that country.
Use {@code getDateTimeInstance} to get a date and time format.
getDateInstance 获取 当前地区的正常 date format;
getTimeInstance 获取 time format;
getDateTimeInstance 获取 date And time format;
Date date1 = new Date(); String format6 = DateFormat.getDateInstance().format(date1); System.out.println(format6); //2023-12-20 String format7 = DateFormat.getTimeInstance().format(date1); System.out.println(format7); // 15:29:04 String format8 = DateFormat.getDateTimeInstance().format(date1); System.out.println(format8); // 2023-12-20 15:29:04
You can pass in different options to these factory methods to control the length of the result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}.
The exact result depends on the locale, but generally:
{@link #SHORT} is completely numeric, such as {@code 12.13.52} or {@code 3:30pm}
{@link #MEDIUM} is longer, such as {@code Jan 12, 1952}
{@link #LONG} is longer, such as {@code January 12, 1952} or {@code 3:30:32pm}
{@link #FULL} is pretty completely specified, such as {@code Tuesday, April 12, 1952 AD or 3:30:42pm PST}.
可以在 static factory methods的参数设置 格式样式:SHORT、MEDIUM、LONG、FULL;
You can also set the time zone on the format if you wish.
If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the {@code DateFormat} you get from the factory methods to a {@link SimpleDateFormat}.
This will work for the majority of countries; just remember to put it in a {@code try} block in case you encounter an unusual one.
如果你想,还可以设置时区;
可以使用SimpleDateFormat,建议放入try中,以防异常情况;
Date formats are not synchronized.
It is recommended to create separate format instances for each thread.
If multiple threads access a format concurrently, it must be synchronized externally.
DateFormat是线程非同步的;
public abstract class DateFormat extends Format { /** * Constant for full style pattern. */ public static final int FULL = 0; /** * Constant for long style pattern. */ public static final int LONG = 1; /** * Constant for medium style pattern. */ public static final int MEDIUM = 2; /** * Constant for short style pattern. */ public static final int SHORT = 3; /** * Constant for default style pattern. Its value is MEDIUM. */ public static final int DEFAULT = MEDIUM; public final static DateFormat getDateInstance() { return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT)); } public final static DateFormat getTimeInstance() { return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT)); } public final static DateFormat getDateTimeInstance() { return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT)); } /** * Creates a DateFormat with the given time and/or date style in the given locale. * @param timeStyle a value from 0 to 3 indicating the time format, ignored if flags is 2 * @param dateStyle a value from 0 to 3 indicating the time format, ignored if flags is 1 * @param flags either 1 for a time format, 2 for a date format, or 3 for a date/time format * @param loc the locale for the format */ private static DateFormat get(int timeStyle, int dateStyle, int flags, Locale loc) { if ((flags & 1) != 0) { if (timeStyle < 0 || timeStyle > 3) { throw new IllegalArgumentException("Illegal time style " + timeStyle); } } else { timeStyle = -1; } if ((flags & 2) != 0) { if (dateStyle < 0 || dateStyle > 3) { throw new IllegalArgumentException("Illegal date style " + dateStyle); } } else { dateStyle = -1; } LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc); DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc); if (dateFormat == null) { dateFormat = get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc); } return dateFormat; } public final String format(Date date) { return format(date, new StringBuffer(), DontCareFieldPosition.INSTANCE).toString(); } public Date parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Date result = parse(source, pos); if (pos.index == 0) throw new ParseException("Unparseable date: \"" + source + "\"" , pos.errorIndex); return result; } }
标签:format,Locale,DateFormat,Date,time,date From: https://www.cnblogs.com/anpeiyong/p/17916661.html