Explain the DateFormat and SimpleDateFormat classes of the formatted date in Java _java

Source: Internet
Author: User
Tags dateformat datetime numeric locale object object string format time and date stringbuffer


DateFormat
1. DateFormat Introduction
The role of DateFormat is to format and parse the date/time. In fact, it is the date formatting tool that helps us format date and then converts date into the string string we want to use
However, the DateFormat format date has limited functionality and is not simpledateformat powerful, but DateFormat is SimpleDateFormat's parent class. Therefore, we first have a general understanding of DateFormat, and then learn SimpleDateFormat.
The role of DateFormat is to format date. It supports formatting styles including full, LONG, MEDIUM and short altogether 4 kinds:
(Dateformat.short)
Full of numbers, as 12.13.52 or 3:30pm
(Dateformat.medium)
Longer, such as the 12, 1952
(Dateformat.long)
Longer, such as January 12, 1952 or 3:30:32pm
(Dateformat.full)
is fully specified, such as Tuesday, April 12, 1952 AD, or 3:30:42pm PST.
The definition of DateFormat is as follows
Public abstract class NumberFormat extends Format {}
function interface of DateFormat
Default constructor:
DateFormat ()



Non-constructor:


Object          clone()
boolean         equals(Object object)
abstract StringBuffer  format(Date date, StringBuffer buffer, FieldPosition field)
final StringBuffer    format(Object object, StringBuffer buffer, FieldPosition field)
final String       format(Date date)
static Locale[]     getAvailableLocales()
Calendar         getCalendar()
final static DateFormat   getInstance()
final static DateFormat   getDateInstance()
final static DateFormat   getDateInstance(int style)
final static DateFormat   getDateInstance(int style, Locale locale)
final static DateFormat   getTimeInstance()
final static DateFormat   getTimeInstance(int style)
final static DateFormat   getTimeInstance(int style, Locale locale)
final static DateFormat   getDateTimeInstance()
final static DateFormat   getDateTimeInstance(int dateStyle, int timeStyle)
final static DateFormat   getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
NumberFormat   getNumberFormat()
TimeZone     getTimeZone()
int       hashCode()
boolean     isLenient()
Date       parse(String string)
abstract Date  parse(String string, ParsePosition position)
Object      parseObject(String string, ParsePosition position)
void       setCalendar(Calendar cal)
void       setLenient(boolean value)
void       setNumberFormat(NumberFormat format)
void       setTimeZone(TimeZone timezone)


Note: DateFormat is an abstract class.
When we get the DateFormat instance through the DateFormat getinstance (), Getdateinstance () and Getdatetimeinstance (), the SimpleDateFormat object is actually returned.
The following function is actually the returned SimpleDateFormat object.


Final static DateFormat getinstance () final static
DateFormat gettimeinstance ()
final static DateFormat gettimeinstance (int style)
final static dateformat gettimeinstance (int style, Locale Locale)
final static DateFormat getdateinstance ()
final static dateformat getdateinstance (int style)
final static DateFormat getdateinstance (int style, Locale Locale)
final static DateFormat getdatetimeinstance ()
final static DateFormat getdatetimeinstance (int datestyle, int timestyle)
final static dateformat getdatetimeinstance (int Datestyle, int timestyle, Locale Locale)


These functions are defined in Simpledateformat.java as follows:


public static final int FULL = 0;
public static final int LONG = 1;
public static final int MEDIUM = 2;
public static final int SHORT = 3;
public static final int DEFAULT = MEDIUM;

public final static DateFormat getInstance () {
  return getDateTimeInstance (SHORT, SHORT);
}

public final static DateFormat getTimeInstance ()
{
  return get (DEFAULT, 0, 1, Locale.getDefault ());
}

public final static DateFormat getTimeInstance (int style)
{
  return get (style, 0, 1, Locale.getDefault ());
}

public final static DateFormat getTimeInstance (int style,
                       Locale aLocale)
{
  return get (style, 0, 1, aLocale);
}

public final static DateFormat getDateInstance ()
{
  return get (0, DEFAULT, 2, Locale.getDefault ());
}

public final static DateFormat getDateInstance (int style)
{
  return get (0, style, 2, Locale.getDefault ());
}

public final static DateFormat getDateInstance (int style,
                       Locale aLocale)
{
  return get (0, style, 2, aLocale);
}

public final static DateFormat getDateTimeInstance ()
{
  return get (DEFAULT, DEFAULT, 3, Locale.getDefault ());
}

public final static DateFormat getDateTimeInstance (int dateStyle,
                          int timeStyle)
{
  return get (timeStyle, dateStyle, 3, Locale.getDefault ());
}

public final static DateFormat
  getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale)
{
  return get (timeStyle, dateStyle, 3, aLocale);
}

/ **
 * Obtaining a DateFormat instance is actually returning a SimpleDateFormat object.
 *
 * timeStyle-value can be "FULL" or "LONG" or "MEDIUM" or "SHORT"
 * dateStyle-value can be "FULL" or "LONG" or "MEDIUM" or "SHORT"
 * flags-The value can be "1" or "2" or "3".
 * 1 means get "time style"
 * 2 means get "date style"
 * 3 means get "time and date style"
 * loc-locale object, representing "local"
 * /
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;
  }
  try {
    // Check whether a provider can provide an implementation that's closer
    // to the requested locale than what the Java runtime itself can provide.
    LocaleServiceProviderPool pool =
      LocaleServiceProviderPool.getPool (DateFormatProvider.class);
    if (pool.hasProviders ()) {
      DateFormat providersInstance = pool.getLocalizedObject (
                        DateFormatGetter.INSTANCE,
                        loc,
                        timeStyle,
                        dateStyle,
                        flags);
      if (providersInstance! = null) {
        return providersInstance;
      }
    }

    return new SimpleDateFormat (timeStyle, dateStyle, loc);
  } catch (MissingResourceException e) {
    return new SimpleDateFormat ("M / d / yy h: mm a");
  }
}


With the above code, we can further realize that the role of DateFormat is to format date, and help us convert date to the string string we need. DateFormat provides very limited functionality and can only support 4 formats such as full, LONG, MEDIUM, and short. Also, when we get the DateFormat instance, we are actually returning the SimpleDateFormat object.



2. DateFormat Example
below, we learn how to use DateFormat's common APIs through examples.
The source code is as follows (Dateformattest.java):



import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.FieldPosition;

/ **
 * DateFormat API test program
 * /
public class DateFormatTest {

  public static void main (String [] args) {

    // Only show "time": call getTimeInstance () function
    testGetTimeInstance ();

    // Only show "date": call getDateInstance () function
    testGetDateInstance ();

    // Display "date" + "time": call getDateTimeInstance () function
    testGetDateTimeInstance ();

    // test the format () function
    testFormat ();
  }

  / **
   * Test DateFormat's getTimeInstance () function
   * It has 3 overloads:
   * (01) getTimeInstance ()
   * (02) getTimeInstance (int style)
   * (03) getTimeInstance (int style, Locale locale)
   *
   * @author skywang
   * /
  private static void testGetTimeInstance () {
    Date date = new Date ();

    // Locale locale = new Locale ("fr", "FR");
    Locale locale = new Locale ("zh", "CN");

    // equivalent to DateFormat.getTimeInstance (DateFormat.MEDIUM);
    DateFormat short0 = DateFormat.getTimeInstance ();

    // The parameter is: "Display style of time"
    DateFormat short1 = DateFormat.getTimeInstance (DateFormat.SHORT);
    DateFormat medium1 = DateFormat.getTimeInstance (DateFormat.MEDIUM);
    DateFormat long1 = DateFormat.getTimeInstance (DateFormat.LONG);
    DateFormat full1 = DateFormat.getTimeInstance (DateFormat.FULL);

    // The parameters are: "display style of time" and "region"
    DateFormat short2 = DateFormat.getTimeInstance (DateFormat.SHORT, locale);
    DateFormat medium2 = DateFormat.getTimeInstance (DateFormat.MEDIUM, locale);
    DateFormat long2 = DateFormat.getTimeInstance (DateFormat.LONG, locale);
    DateFormat full2 = DateFormat.getTimeInstance (DateFormat.FULL, locale);

    System.out.println ("\ n ---- getTimeInstance ---- \ n"
        + "(1.0) Empty Param:" + short0.format (date) + "\ n"
        + "(2.1) One Param (s):" + short1.format (date) + "\ n"
        + "(2.2) One Param (m):" + medium1.format (date) + "\ n"
        + "(2.3) One Param (l):" + long1.format (date) + "\ n"
        + "(2.4) One Param (f):" + full1.format (date) + "\ n"
        + "(3.1) One Param (s, l):" + short2.format (date) + "\ n"
        + "(3.2) One Param (m, l):" + medium2.format (date) + "\ n"
        + "(3.3) One Param (l, l):" + long2.format (date) + "\ n"
        + "(3.4) One Param (f, l):" + full2.format (date) + "\ n"
        );
  }

  / **
   * Test DateFormat's getDateTimeInstance () function
   * It has 3 overloads:
   * (01) getDateInstance ()
   * (02) getDateInstance (int style)
   * (03) getDateInstance (int style, Locale locale)
   * /
  public static void testGetDateTimeInstance () {
    Date date = new Date ();

    Locale locale = new Locale ("zh", "CN");

    // equivalent to DateFormat.getDateTimeInstance (DateFormat.MEDIUM);
    DateFormat short0 = DateFormat.getDateTimeInstance ();

    DateFormat short1 = DateFormat.getDateTimeInstance (DateFormat.SHORT, DateFormat.SHORT);
    DateFormat medium1 = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM);
    DateFormat long1 = DateFormat.getDateTimeInstance (DateFormat.LONG, DateFormat.LONG);
    DateFormat full1 = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL);

    DateFormat short2 = DateFormat.getDateTimeInstance (DateFormat.SHORT, DateFormat.SHORT, locale);
    DateFormat medium2 = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
    DateFormat long2 = DateFormat.getDateTimeInstance (DateFormat.LONG, DateFormat.LONG, locale);
    DateFormat full2 = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL, locale);

    System.out.println ("\ n ---- getDateTimeInstance ---- \ n"
        + "(1.0) Empty Param:" + short0.format (date) + "\ n"
        + "(2.1) One Param (s):" + short1.format (date) + "\ n"
        + "(2.2) One Param (m):" + medium1.format (date) + "\ n"
        + "(2.3) One Param (l):" + long1.format (date) + "\ n"
        + "(2.4) One Param (f):" + full1.format (date) + "\ n"
        + "(3.1) One Param (s, l):" + short2.format (date) + "\ n"
        + "(3.2) One Param (m, l):" + medium2.format (date) + "\ n"
        + "(3.3) One Param (l, l):" + long2.format (date) + "\ n"
        + "(3.4) One Param (f, l):" + full2.format (date) + "\ n"
        );
  }

  / **
   * Test the DateFormat getDateInstance () function
   * It has 3 overloads:
   * (01) getDateTimeInstance ()
   * (02) getDateTimeInstance (int dateStyle, int timeStyle)
   * (03) getDateTimeInstance (int dateStyle, int timeStyle, Locale locale)
   * /
  public static void testGetDateInstance () {
    Date date = new Date ();

    // Locale locale = new Locale ("en", "US");
    Locale locale = new Locale ("zh", "CN");

    // equivalent to DateFormat.getDateInstance (DateFormat.MEDIUM);
    DateFormat short0 = DateFormat.getDateInstance ();

    DateFormat short1 = DateFormat.getDateInstance (DateFormat.SHORT);
    DateFormat medium1 = DateFormat.getDateInstance (DateFormat.MEDIUM);
    DateFormat long1 = DateFormat.getDateInstance (DateFormat.LONG);
    DateFormat full1 = DateFormat.getDateInstance (DateFormat.FULL);

    DateFormat short2 = DateFormat.getDateInstance (DateFormat.SHORT, locale);
DateFormat medium2 = DateFormat.getDateInstance (DateFormat.MEDIUM, locale);
    DateFormat long2 = DateFormat.getDateInstance (DateFormat.LONG, locale);
    DateFormat full2 = DateFormat.getDateInstance (DateFormat.FULL, locale);

    System.out.println ("\ n ---- getDateInstance ---- \ n"
        + "(1.0) Empty Param:" + short0.format (date) + "\ n"
        + "(2.1) One Param (s):" + short1.format (date) + "\ n"
        + "(2.2) One Param (m):" + medium1.format (date) + "\ n"
        + "(2.3) One Param (l):" + long1.format (date) + "\ n"
        + "(2.4) One Param (f):" + full1.format (date) + "\ n"
        + "(3.1) One Param (s, l):" + short2.format (date) + "\ n"
        + "(3.2) One Param (m, l):" + medium2.format (date) + "\ n"
        + "(3.3) One Param (l, l):" + long2.format (date) + "\ n"
        + "(3.4) One Param (f, l):" + full2.format (date) + "\ n"
        );

  }

  / **
   * Test DateFormat's format () function
   * /
  public static void testFormat () {
    Date date = new Date ();
    StringBuffer sb = new StringBuffer ();
    FieldPosition field = new FieldPosition (DateFormat.YEAR_FIELD);
    DateFormat format = DateFormat.getDateTimeInstance ();

    sb = format.format (date, sb, field);
    System.out.println ("\ ntestFormat");
    System.out.printf ("sb =% s \ n", sb);
  }
}



Run Result:


----gettimeinstance----(1.0) Empty param:4:54:22 pm (2.1) One Param (s): 4:54 pm (2.2) One Param (m): 4:54:22 PM (2. 3 One Param (l): 4:54:22 PM CST (2.4) one Param (f): 4:54:22 PM CST (3.1) One Param (s,l): PM 4:54 (3.2) one Param (m,l): 16   : 54:22 (3.3) One Param (l,l): 04:54 P.M. 22 seconds (3.4) one Param (f,l): 04:54 P.M. 22 sec CST----getdateinstance----(1.0) Empty Param : 2.1 One Param (s): 1/23/14 (2.2) One Param (m): 2014, 2014 (2.3) one Param (L): January 23, 2014 (2.4 One Param (f): Thursday, January, 2014 (3.1) one Param (s,l): 14-1-23 (3.2) one Param (m,l): 2014-1-23 (3.3) One Param ( L,L): January 23, 2014 (3.4) one Param (f,l): January 23, 2014 Thursday----getdatetimeinstance----(1.0) Empty Param:jan 23, 2014 4:54 : PM (2.1) One Param (s): 1/23/14 4:54 pm (2.2) One Param (m): A few, 2014 4:54:23 PM (2.3) one Param (L): January 23, 2014 4:54:23 PM CST (2.4) one Param (f): Thursday, January, 2014 4:54:23 PM CST (3.1) One Param (s,l): 14-1-23 4:54 (3 .2) One Param (m,l): 2014-1-23 16:54:23 (3.3) One Param (l,l): January 23, 2014 04:54 P.M. 23 SEC (3.4) one Param (f,l): January 23, 2014 Thursday 04:54 P.M. 23 sec CST Testfor

 Mat Sb=jan, 2014 4:54:23 PM





Ok. At this point, the study of DateFormat ends. Next, we begin to learn SimpleDateFormat, which is what formatting date needs to be focused on.



SimpleDateFormat
1. SimpleDateFormat Introduction
SimpleDateFormat is a tool that formats date and parses date strings. Its most common use is to format date in the specified format, and then we use the string that can be obtained after the date is formatted.
More strictly speaking, SimpleDateFormat is a specific class that formats and parses dates in a way that is relevant to the locale. It allows for formatting (date-> text), parsing (text-> date), and normalization.
The SimpleDateFormat constructor:
Constructors


SimpleDateFormat ()
SimpleDateFormat (string pattern)
SimpleDateFormat (string template, DateFormatSymbols Value)
SimpleDateFormat (String template, Locale Locale)


Non-constructor function


void               Applylocalizedpattern (string template)
void               Applypattern (string template)
Object              Clone (
boolean             equals (object)
stringbuffer           Format (date date, StringBuffer buffer, fieldposition Fieldpos)
attributedcharacteriterator   Formattocharacteriterator (Object object)
Date               Get2digityearstart ()
dateformatsymbols        getdateformatsymbols ()
int               hashcode ()
Date               Parse (string string, parseposition position)
void               Set2digityearstart (date date)
void               Setdateformatsymbols (dateformatsymbols value)
string              tolocalizedpattern ()
string              topattern ( )


SimpleDateFormat Simple demonstration:


Creates a new Date object with the time 2013-09-19
date = new Date (113,8,19); 
Create a new "SimpleDateFormat object" and set the SDF's "format mode"
simpledateformat sdf = new SimpleDateFormat ("Yyyy-mm-dd");
Formats date with SDF and returns a string.
String str = sdf.format (date); 


2. SimpleDateFormat Related Format description
2.1 Date and time patterns
date and time formats are specified by date and time pattern strings. In a date and time pattern string, the unnamed letters ' a ' to ' Z ' and ' a ' to ' Z ' are interpreted as pattern letters, which are used to represent a date or time string element. The text can be enclosed in single quotes (') to avoid interpretation. "" "represents single quotes. All other characters are not interpreted; they are simply copied to the output string when they are formatted, or they match the input string when parsing.
The following pattern letters are defined (all other characters ' a ' to ' Z ' and ' a ' to ' Z ' are preserved):


-->-->
Letters Date or time element Said Example
G Era Marker Text AD
Y Years Year 1996; 96
M Months of the year Month July; June June; 07
W Number of weeks in the year Number 27
W Number of weeks in the month Number 2
D Days in the year Number 189
D Days in the month Number 10
F Weeks in the month Number 2
E Days in the week Text Tuesday; Tue
A AM/PM Mark Text Pm
H Number of hours in a day (0-23) Number 0
K Number of hours in a day (1-24) Number 24
K Number of hours in am/pm (0-11) Number 0
H Number of hours in am/pm (1-12) Number 12
M Number of minutes in the hour Number 30
S Number of seconds in minutes Number 55
S Number of milliseconds Number 978
Z Time General Time Zone Pacific Standard time; PST; gmt-08:00
Z Time RFC 822 Time Zone -0800



The pattern letters are usually repeated, and their number determines their exact representation:


Text: For formatting, if the number of pattern letters is greater than or equal to 4, the full form is used, otherwise the short form or abbreviated form is used when available. For parsing, both forms are acceptable, regardless of the number of pattern letters.


Number: For formatting, the quantity of the pattern letters is the smallest digit, and if the digits are not enough, fill them with 0 to achieve this number. For parsing, the number of pattern letters is ignored unless two contiguous fields must be separated.


Year: If the calendar for the formatter is the Gregorian calendar, the following rules apply.


Month: If the number of pattern letters is 3 or greater than 3, the month is interpreted as text;


For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits, otherwise the year is interpreted to number.


For parsing, if the number of pattern letters is greater than 2, the year is interpreted literally, regardless of the number of digits. So use the pattern "mm/dd/yyyy" to parse "01/11/12" to January 11, 12 A.D.


When parsing the abbreviated year pattern ("Y" or "yy"), the SimpleDateFormat must interpret the abbreviated year relative to a century. This is accomplished by adjusting the date to the 80 and beyond 20 before the SimpleDateFormat instance was created. For example, in "Mm/dd/yy" mode, if the SimpleDateFormat instance was created on January 1, 1997, the string "01/11/12" will be interpreted as January 11, 2012, and the string "05/04/64" will be interpreted as May 4, 1964. When parsing, only strings consisting of exactly two digits (as defined by Character#isdigit (char)) are resolved to the default century. Any other numeric string will be interpreted literally, such as a single digit string, a string of 3 or more digits, or a two-bit numeric string that is not a number (for example, "1"). Therefore, in the same mode, "01/02/3" or "01/02/003" is interpreted as A.D. January 2, 3. Similarly, the "01/02/-3" resolution was January 2, 4 BC.


Otherwise, the calendar system-specific form is applied. For formatting and parsing, if the number of pattern letters is 4 or greater than 4, the calendar-specific long form is used. Otherwise, the calendar-specific short or abbreviated form is used.


SimpleDateFormat also supports localized date and time pattern strings. In these strings, the pattern letters described above can be replaced with other language-related pattern letters. SimpleDateFormat does not handle text localization other than the schema letter, which is handled by the client of the class.


Example


The following example shows how to interpret date and time patterns in the U.S. language environment. The given date and time is 2001-07-04 12:08:56 for the Pacific Time zone in the United States.


-->
Date and time patterns Results
"YYYY. Mm.dd G ' at ' HH:mm:ss z ' 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM D, ' yy" Wed, April 4, ' 01
"H:mm a" 12:08 PM
"HH ' o ' clock ' A, zzzz" O ' clock PM, Pacific Daylight Time
"K:mm A, z" 0:08 PM, PDT
"Yyyyy. MMMMM.DD GGG hh:mm AAA " 02001.july.04 AD 12:08 PM
"EEE, D MMM yyyy HH:mm:ss Z" Wed, 4 June 2001 12:08:56-0700
"Yymmddhhmmssz" 010704120856-0700
"Yyyy-mm-dd ' T ' HH:mm:ss." Sssz " 2001-07-04t12:08:56.235-0700



The date format is not synchronized. It is recommended that you create separate format instances for each thread. If more than one thread accesses a format at the same time, it must be externally synchronized.


3. SimpleDateFormat sample
Below, we learn how to use SimpleDateFormat through an example.


The source code is as follows (Simpledateformattest.java):





import java.util.Date;
import java.util.Locale;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/ **
 * SimpleDateFormat API test program
 *
 * @author skywang
 * @email kuiwu-wang@163.com
 * /
public class SimpleDateFormatTest {

  public static void main (String [] args) {

    // Get date / time through SimpleDateFormat: there are multiple formats
    testSimpleDateFormats ();

    // Get Date / Time via DateFormat
    superTest ();
  }

  / **
   * Get Date / Time via SimpleDateFormat. There are multiple formats to choose from
   * /
  private static void testSimpleDateFormats () {
    String [] formats = new String [] {
      "HH: mm", // 14:22
      "h: mm a", // 2:22 pm
      "HH: mm z", // 14:22 CST
      "HH: mm Z", // 14:22 +0800
      "HH: mm zzzz", // 14:22 China Standard Time
      "HH: mm: ss", // 14:22:30
      "yyyy-MM-dd", // 2013-09-19
      "yyyy-MM-dd HH: mm", // 2013-09-19 14:22
      "yyyy-MM-dd HH: mm: ss", // 2013-09-19 14:22:30
      "yyyy-MM-dd HH: mm: ss zzzz", // 2013-09-19 14:22:30 China Standard Time
      "EEEE yyyy-MM-dd HH: mm: ss zzzz", // Thursday 2013-09-19 14:22:30 China Standard Time
      "yyyy-MM-dd HH: mm: ss.SSSZ", // 2013-09-19 14: 22: 30.000 + 0800
      "yyyy-MM-dd'T'HH: mm: ss.SSSZ", // 2013-09-19T14: 22: 30.000 + 0800
      "yyyy.MM.dd G 'at' HH: mm: ss z", // 2013.09.19 AD at 14:22:30 CST
      "K: mm a", // 2:22 pm, CST
      "EEE, MMM d, '' yy", // Thursday, September 19, '13
      "hh 'o''clock' a, zzzz", // 02 o'clock afternoon, China Standard Time
      "yyyyy.MMMMM.dd GGG hh: mm aaa", // 02013. September. 19 AD 02:22 PM
      "EEE, d MMM yyyy HH: mm: ss Z", // Thursday, 19 September 2013 14:22:30 +0800
      "yyMMddHHmmssZ", // 130919142230 + 0800
      "yyyy-MM-dd'T'HH: mm: ss.SSSZ", // 2013-09-19T14: 22: 30.000 + 0800
      "EEEE 'DATE (' yyyy-MM-dd ')' 'TIME (' HH: mm: ss ')' zzzz", // Thursday 2013-09-19 14:22:30 Standard Time
    };

    // Date date = (new Date (0)); // date is 1970-01-01 07:00:00
    // Date date = Calendar.getInstance (). GetTime (); // date is the current time
    Date date = new Date (113, 8, 19, 14, 22, 30); // date is 2013-09-19 14:22:30
    for (String format: formats) {
      SimpleDateFormat sdf = new SimpleDateFormat (format, Locale.SIMPLIFIED_CHINESE);
      // SimpleDateFormat sdf = new SimpleDateFormat (format);
      System.out.format ("% 30s% s \ n", format, sdf.format (date));
     }
  }

  / **
   * Get Date / Time via DateFormat
   * /
  private static void superTest () {
    // Create a new date object, the time is 2013-09-19 14:22:30
    // (01) year = "'target year'-1900",
    // (02) months. 0 is January, 1 is February, and so on.
    // (03) days. Numbers between 1-31
    Date mDate = new Date (113, 8, 19, 14, 22, 30);
    Locale locale = new Locale ("zh", "CN");

    // 14:22:30
    String time = DateFormat.getTimeInstance (DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE) .format (mDate);
    // 2013-09-19
    String date = DateFormat.getDateInstance (DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE) .format (mDate);
    // 2013-09-19 14:22:30
    String datetime = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE) .format (mDate);

    System.out.printf ("\ ntime =% s \ ndate =% s \ ndatetime =% s \ n", time, date, datetime);
  }
}


Run Result:


HH: mm 14:22
            h: mm a 2:22 pm
            HH: mm z 14:22 CST
            HH: mm Z 14:22 +0800
          HH: mm zzzz 14:22 China Standard Time
           HH: mm: ss 14:22:30
          yyyy-MM-dd 2013-09-19
       yyyy-MM-dd HH: mm 2013-09-19 14:22
      yyyy-MM-dd HH: mm: ss 2013-09-19 14:22:30
   yyyy-MM-dd HH: mm: ss zzzz 2013-09-19 14:22:30 China Standard Time
 EEEE yyyy-MM-dd HH: mm: ss zzzz Thursday 2013-09-19 14:22:30 China Standard Time
   yyyy-MM-dd HH: mm: ss.SSSZ 2013-09-19 14: 22: 30.000 + 0800
  yyyy-MM-dd'T'HH: mm: ss.SSSZ 2013-09-19T14: 22: 30.000 + 0800
 yyyy.MM.dd G 'at' HH: mm: ss z 2013.09.19 AD at 14:22:30 CST
            K: mm a 2:22 pm
       EEE, MMM d, '' yy Thursday, September 19, '13
     hh 'o''clock' a, zzzz 02 o'clock afternoon, China Standard Time
 yyyyy.MMMMM.dd GGG hh: mm aaa 02013. September. 19 AD 02:22 PM
  EEE, d MMM yyyy HH: mm: ss Z Thursday, 19 September 2013 14:22:30 +0800
         yyMMddHHmmssZ 130919142230 + 0800
  yyyy-MM-dd'T'HH: mm: ss.SSSZ 2013-09-19T14: 22: 30.000 + 0800
EEEE 'DATE (' yyyy-MM-dd ')' 'TIME (' HH: mm: ss ')' zzzz Thursday DATE (2013-09-19) TIME (14:22:30) China Standard Time

time = 14: 22: 30
date = 2013-9-19
datetime = 2013-9-19 14:22:30  




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.