DateFormat

发布时间 2023-12-20 15:53:06作者: anpeiyong

概述

{@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是线程非同步的;