Formatting time in Java

Source: Internet
Author: User
Simpledateformat class to solve our time format problem.

Simpledateformat is a type used to format and analyze dates in a language environment-related manner. It allows formatting (date-> text), analysis (text-> date), and normalization. Simpledateformat allows you to select any custom date-Time Format mode.

Date and Time Modes

The date and time formats are specified by the date and time mode strings. Letters that are not enclosed in quotation marks in the date and time format strings'A'To'Z'And'A'To'Z'It is interpreted as a pattern letter used to represent a date or time string element. Single quotes ('."''"Single quotes. All other characters are not interpreted. They are simply copied to the output string during formatting or matched with the input string during analysis.

The following pattern letters are defined (all other characters'A'To'Z'And'A'To'Z'Are retained ):

Letter Date or time element Indicates Example
G Era identifier Text Ad
Y Year Year 1996;96
M Month in Year Month July;Jul;07
W Week in Year Number 27
W The number of weeks in the month Number 2
D Days in years Number 189
D Days in a month Number 10
F Week in the month Number 2
E Days in a week Text Tuesday;Tue
A AM/PM mark Text PM
H Hours in a day (0-23) Number 0
K Hours per day (1-24) Number 24
K Hours in AM/PM (0-11) Number 0
H Hours in AM/PM (1-12) Number 12
M Minutes in an hour Number 30
S Seconds in minutes Number 55
S Milliseconds Number 978
Z Time Zone General Time Zone Pacific Standard Time;Pst;GMT-08: 00
Z Time Zone RFC 822 Time Zone -0800

The following example shows how to explain the date and time modes in the U.S. language environment. The given date and time are the local time of the US Pacific Time Zone 12:08:56

 

 

Date and Time Modes Result
"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" 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 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" T12: 08: 56.235-0700

 

 

 

 

 

 

 

 

Examples of practical applications of simpledateformat in programming:

(1) format (date-> text)

Generally, the commonly used date and time in Chinese is as follows: 20070719 20:29:30
Simpledateformat formater = new simpledateformat ("yyyymmdd hh: mm: SS ");
System. Out. println ("date to string" + formater. Format (new date ()));

Similar common formats include yymmdd hh: mm: SS yyyy-mm-dd hh: mm: ss dd-mm-yyyy hh: mm: SS
However, it is worth noting that this format (19jul07) and Its similar forms have some tips: 19jul07
Simpledateformat formater = new simpledateformat ("ddmmmyy", new locale ("us "))
System. Out. println ("date to string" + formater. Format (new date (). touppercase ());
Because it is in English, you should use the locale object parameter. Otherwise, the default local locale is used.

(2) analysis (text> date)

Generally, the date and time format and text parameters are used for the desired time format. For example,-7-19, a Java is returned. util. the time object of the date type.

Formater =   New Simpledateformat ( " Yyyymmmdd " , New Locale ( " Us " ));
Try   ... {
System. Out. println ("String to date"+Formater. parse ("2007sep01"));
}   Catch (Parseexception E) ... {
E. printstacktrace ();
}

In particular,

 

Formater =   New Simpledateformat ( " Yyyymmmdd " , New Locale ( " Us " ));
Try   ... {
System. Out. println ("String to date"+Formater. parse ("2007sep01"));
}   Catch (Parseexception E) ... {
E. printstacktrace ();
}

It also returns a date object of the Java. util. date type.

(3) Text-> timestamp, date-> Timestamp

 

Timestamp T;
Simpledateformat format =   New Simpledateformat ( " Yyyy-mm-dd hh: mm: SS " );
Try   ... {
T= NewTimestamp (format. parse ("00:00:00"). Gettime ());
}   Catch (Parseexception E) ... {
E. printstacktrace ();
}

Timestamp T;
Simpledateformat format =   New Simpledateformat ( " Yyyy-mm-dd hh: mm: SS " );
T =   New Timestamp ( New Date (). gettime ());

 

 

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.