Java date format (DateFormat SimpleDateFormat class and category)

Converting the format date indicates the date / time format predefined date / time format. For example, the date "Fri May 18 15:46:24 CST2016" format to "2016-5-18 15:46:24 Friday" format.

In  Java  , the class can be used DateFormat and SimpleDateFormat classes to format dates, described in detail below use these classes to format dates.

DateFormat class

DateFormat date / time formatting subclasses of the abstract class, which formats and parses dates or time in a language-independent manner. Date / time formatting subclasses (such as the SimpleDateFormat) allowed format (i.e. date → text), parsing (→ date text) and normalized date.

Can not be used when creating new objects DateFormat keywords, but should use the static method getDateInstance, DateFormat class (), the following sample code:

 

DateFormat df = DateFormat.getDatelnstance();

After creating a DateFormat object, you can call the method of the object to format the date / time. DateFormat class conventional method shown in Table 1.

Table 1 DateFormat class conventional method
method description
String format(Date date) Date Format The date / time string
Calendar getCalendar() Gets a calendar with this date / time format associated
static DateFormat getDateInstance() Gets the default formatting style and has a default locale date format
static DateFormat getDateInstance(int style) Gets the date style format with the specified format and default locale
static DateFormat getDateInstance(int style,
Locale locale)
Gets the date format with the specified format and style specified locale
static DateFormat getDateTimeInstance() Gets the default formatting style with a date and the default locale / time
format
static DateFormat getDateTimeInstance(int
dateStyle,int timeStyle)
Get with the specified date / time formatting styles for the default locale and
date / time format
static DateFormat getDateTimeInstance(int
dateStyle,int timeStyle,Locale locale)
Get with the specified date / time formatting styles and specify the locale of
the date / time format
static DateFormat getTimeInstance() Gets the default formatting style and has a default locale time format
static DateFormat getTimeInstance(int style) Gets the format with the specified format and style for the default locale
static DateFormat getTimeInstance(int style,
Locale locale)
Gets the format with the specified format and style specified locale
void setCalendar(Calendar newCalendar) Set the calendar this format
Date parse(String source) Parse the string into the given date / time


The main style format set by DateFormat constants. Different constants passed to the method shown in Table 1 in order to control the length of the result. DateFormat class constants are as follows.

  • SHORT: fully digital, such as 12.5.10 or 5:30 pm.
  • MEDIUM: longer, such as May 10,2016.
  • LONG: longer, or as May 12,2016 11:15:32 am.
  • FULL: is fully specified, such as Tuesday, May 10,2012 AD or 11: l5: 42am CST.


Example DateFormat class using said format / time is as follows:

// get a different date formatting style and Chinese environmental 
DateFormat DF1 = DateFormat.getDateInstance (DateFormat.SHORT, Locale.CHINA);
DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
DF4 DateFormat = DateFormat.getDateInstance (DateFormat.LONG, Locale.CHINA);
 // get time formatting styles for different environments and China 
DateFormat DF5 = DateFormat.getTimeInstance (DateFormat.SHORT, Locale.CHINA);
DateFormat df6 = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA);
DateFormat df7 = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);
DF8 DateFormat = DateFormat.getTimeInstance (DateFormat.LONG, Locale.CHINA);
 // different formatting style of date is the date format string 
String date1 = df1.format ( new new a Date ());
String date2 = df2.format(new Date());
String date3 = df3.format(new Date());
Date4 String = df4.format ( new new a Date ());
 // different formatting style of time as the time format string 
String TIME1 = df5.format ( new new a Date ());
String time2 = df6.format(new Date());
String time3 = df7.format(new Date());
String time4 = df8.format(new Date());
// 输出日期
System.out.println("SHORT:" + date1 + " " + time1);
System.out.println("FULL:" + date2 + " " + time2);
System.out.println("MEDIUM:" + date3 + " " + time3);
System.out.println("LONG:" + date4 + " " + time4);

The operation of the code, the following output results:

SHORT: 18-10-15 9:30 am
FULL: 2018 Nian 10 Monday, 15 October at 09 am CST 30 minutes and 43 seconds
MEDIUM,: 2018-10-15 9:30:43 
LONG: 2018 Nian 10 15 morning 09 hours 30 minutes 43 seconds

This example describes the format DateFormat class constants used in conjunction with the method may be performed by using different styles of classes DateFomat date.

SimpleDateFormat 类

If DateFormat class formatted date / time, and can not meet the requirements, it is necessary to use a subclass --SimpleDateFormat DateFormat class.

SimpleDateFormat is a concrete class locale-related manner formatting and parsing dates, which allows for formatting (date → text), parsing (text → date) and normalization. SimpleDateFormat mode may be selected so that any user-defined date / time format.

SimpleDateFormat class has the following 3 main constructor.

  • SimpleDateFormat (): using the default format and default locale structure SimpleDateFormat.
  • SimpleDateFormat (String pattern): with the specified format and default locale structure SimpleDateF ormat.
  • SimpleDateFormat (String pattern, Locale locale): SimpleDateF ormat with the locale configuration specified format and specify.


SimpleDateFormat custom format commonly used letters and as given in Table 2 below.

Letters and their meanings in Table 2 of Example date / time format
letter meaning Examples
Y years. Yy is generally a two year, yyyy represents a four year Use yy is in play, such as the 11;
use yyyy represents the year, such as 2011
M month. MM is the month for general use, if you use MMM, it will
display the month in different languages according to the locale
Use MM is the month, such as 05;
using MMM for the month, in Locale.CHINA
under locale, such as "October"; in Locale.US
under locale, such as Oct
d The number of days of the month. Usually the number of days with dd Dd represented using the number of days, such as 10
D The number of days in the year. He said on that day is the first few days of the year, is represented by D D represents the number of days in the year of use, such as 295
E Day of the week. Represented by E based on different locale, the display is not
the week with a few language
Use E represents a day of the week, in Locale.CHINA language
under the locale, such as "Thursday"; in Locale.US language
under the locale, such as Thu
H The number of hours of the day (0 to 23). Usually HH represents hours HH is the number of hours of use, such as 18
h The number of hours of the day (1 to 12). Hh represents the number of hours of general use Hh is the number of hours of use, such as 10 (note 10 there
may be 10 points, 22 points may be)
m The number of minutes. Usually the number of minutes used mm Number of minutes used, expressed in mm, such as 29
s Seconds. Generally used ss the number of seconds Use ss is the number of seconds, such as 38
S The number of milliseconds. It represents the number of milliseconds generally used SSS SSS represented using the number of milliseconds, such as 156

Example 1

Write Java programs, using SimpleDateFormat class to format and print the current date, the date format is "xxxx xx xx xxx day week xx minutes xx seconds point", specific codes are as follows:

import java.text.SimpleDateFormat;
import java.util.Date;
public class Test13 {
    public static void main(String[] args) {
        Date now = new Date(); // 创建一个Date对象,获取当前时间
        // 指定格式化格式
        SimpleDateFormat f = new SimpleDateFormat("今天是 " + "yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
        System.out.println(f.format(now)); // 将当前时间袼式化为指定的格式
    }
}

该程序的运行结果如下:

今天是 2018 年 10 月 15 日 星期一 09 点 26 分 23 秒

 

Guess you like

Origin www.cnblogs.com/shenyixin/p/12424926.html