Format output of date and DateFormat in Java

Source: Internet
Author: User
Tags dateformat days in month local time locale

First, DateFormat

Use Getdateinstance to get the standard date format for that country/region. Additional static factory methods are also available. Use Gettimeinstance to get the time format for that country/region. Use Getdatetimeinstance to get the date and time format. You can pass different options to these factory methods to control the length of the results (from short to MEDIUM to LONG and to full). The exact result depends on the locale, but usually:

    • Short is completely digital, such as 12.13.52 or 3:30pm
    • MEDIUM longer, such as Jan 12, 1952
    • Long longer, such as January 12, 1952 or 3:30:32pm
    • Full is fully specified, such as Tuesday, April 12, 1952 AD, or 3:30:42pm PST.

If you prefer, you can also set the time zone on the format. If you want to exert more control over formatting or parsing (or give the user more control), you can try to cast the DateFormat obtained from the factory method to SimpleDateFormat. This applies to most countries; just remember to put it in a try code block in case you encounter a special format.

Use local time zone
Date date = new Date ();//date format, accurate to day 2017-4-16dateformat df1 = Dateformat.getdateinstance (); System.out.println (Df1.format (date)),//can be accurate to the second 2017-4-16 12:43:37dateformat DF2 = Dateformat.getdatetimeinstance (); System.out.println (Df2.format (date));//Show only 12:43:37dateformat df3 = Dateformat.gettimeinstance (); System.out.println (Df3.format (date));//display date, week, afternoon, time (accurate to seconds)//April 16, 2017 Sunday 12:43 P.M. 37 seconds Cstdateformat Df4 = Dateformat.getdatetimeinstance (Dateformat.full, dateformat.full); System.out.println (Df4.format (date));//display date, last afternoon, time (accurate to seconds)//April 16, 2017 12:43 P.M. 37 seconds DateFormat Df5 = Dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long); System.out.println (Df5.format (date));//display date, last afternoon, time (accurate to seconds)//April 16, 2017 12:43 P.M. 37 seconds DateFormat Df5_1 = Dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long, Locale.china); System.out.println (Df5_1.format (date));//display date, last afternoon, time (accurate to minute) 17-4-16 pm 12:43dateformat df6 = Dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short); SYSTEM.OUT.PRINTLN (DF6.format (date));//display date, time (accurate to seconds) 2017-4-16 12:43:37 DateFormat df7 = dateformat.getdatetimeinstance (dateformat.medium , Dateformat.medium); System.out.println (Df7.format (date));

Second, SimpleDateFormat

Java.text.SimpleDateFormat

The following pattern letters are defined (all other characters ‘A‘ to ‘Z‘ and ‘a‘ to ‘z‘ are preserved):

Letters date or time element represents Example
G Era Marker Text AD
y Years Year 1996;96
M Month of the Year Month July; Jul;07
w Number of weeks in the year Number 27
W Number of weeks in the month Number 2
D Number of days in the year Number 189
d Number of days in month Number 10
F Week of the Month Number 2
E Days of the Week Text Tuesday;Tue
a AM/PM Mark Text PM
H Number of hours in the day (0-23) Number 0
k Number of hours in the 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 hours 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

Instance parameters

Date and Time mode Results
"yyyy.MM.dd G ‘at‘ HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ‘‘yy" Wed, Jul 4, ‘01
"h:mm a" 12:08 PM
"hh ‘o‘‘clock‘ a, zzzz" 12 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 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd‘T‘HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700

Code results

Date d = new Date (),/*//h 1-12 output format: 2017-04-16 01:01:22 */dateformat format1 = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); String s = format1.format (d); System.out.println (s);/* H 0-23 output format: 2017-04-16 13:01:22*/dateformat format2 = new SimpleDateFormat ("Yyyy-mm-dd hh:mm: SS "); s = Format2.format (d); System.out.println (s);/* K 0-11 output format: 2017-04-16 01:01:22 */dateformat format3 = new SimpleDateFormat ("Yyyy-mm-dd kk:mm: SS "); s = Format3.format (d); System.out.println (s);/* k 1-24 output format: 2017-04-16 13:01:22 */dateformat format4 = new SimpleDateFormat ("Yyyy-mm-dd kk:mm: SS "); s = Format4.format (d); System.out.println (s);/* output format: 20170416010122 */dateformat format5 = new SimpleDateFormat ("Yyyymmddhhmmss"); s = Format5.format (d); System.out.println (s);

Common methods

Parse
Public Date Parse (String texts)
Parses the text of a string, generated Date .

This method attempts to parse the text. If the parse succeeds and returns the resolved date. The update pos can be used to indicate the starting point of the next call to this method. If an error occurs, and NULL is returned.

Designated by:
DateFormatin the class parse
Parameters:
text-One of the parts should be parsed String .
 
Return:
Parsed from a string Date . If an error occurs, NULL is returned.
Thrown:
NullPointerException-If text null.
See also:
DateFormat.setLenient(boolean)

Parse
Public Date Parse (String text,                  parseposition POS)
parses the text of a string, generated Date .

This method attempts to parse pos the text that starts at the given index. If the resolution succeeds, the pos index is updated to the index after the last character used (it is not necessary to parse all characters until the end of the string), and returns the parsed date. The update pos can be used to indicate the starting point of the next call to this method. If an error occurs, the index is not changed, pos and pos the error index is set to the character index at which the error occurred and Null is returned.

Designated by:
DateFormat in the parse class
Parameters:
text -One of the parts should be parsed String .
pos -An object that has the index and error index information described above ParsePosition .
Return:
Parsed from a string Date . If an error occurs, NULL is returned.
Thrown:
NullPointerException-If text or pos is null.
See also:
DateFormat.setLenient(boolean)

Format
Public final String format (date date)
formats a date as a day/time string.

Parameters:
date-The time value to be formatted as a time string.
Return:
the formatted time string.

Format output of date and DateFormat in Java

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.