Common Methods for date formatting in Java

Source: Internet
Author: User
Generally, the format (date time) Methods of dateformat and simpledateformat are commonly used to format the date.

1. First introduce java. Text. simpledateformat class.

public class SimpleDateFormat extends DateFormat

SimpleDateFormat

Format and parse a date in a language environment-related manner. It allows formatting (date-> text), parsing (text-> date), and normalization.

This allows you to select any custom date-Time Format mode. However, we recommend that you use DateFormatIn getTimeInstance, getDateInstanceOr getDateTimeInstanceTo create a date-time formatter. Every such method can return a date/time formatter initialized in the default format. You can use applyPatternTo modify the format.

 

Date and Time Modes

The date and time formats are as follows:Date and Time ModesString. 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 parsing.

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
Example

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 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

Common constructor methods:

Simpledateformat sformat = new simpledateformat (string pattern );

Or

Simpledateformat sformat = new simpledateformat ();

Sformat. applypattern (string pattern );

Or

Dateformat = dateformat. getdateinstance (dateformat.Full, Locale. getdefault ());

// Dateformat. Short, default, long, full, etc.

 

 2. Commonly Used date classes include calendar and gregoriancalendar. The latter is a subclass of the former and extends some functions, such as whether it is a leap year or not.

Note:Before JDK 1.1DateThere are two other functions. It can be interpreted as the year, month, day, hour, minute, and second values. It also allows formatting and parsing date strings. However, the APIs of these functions are not easy to internationalize. From JDK 1.1, you should useCalendarClass to convert the date and time fields.DateFormatClass to format and parse the date string.DateThe corresponding method in has been discarded.

 Common constructor methods:

Calendar c = calendar. getinstance ();

C. settime (date );//Or C. settime (INT year, int month, int day, int hour, int minute, int second );

 

Gregoriancalendar GC = new gregoriancalendar (INT year, int month, int day, int hour, int minute, int second); // The method can be reloaded. For details, refer to the API

 

3. Sample Code (log4j is used here. You only need to change log.info in the Code to the corresponding system. Out. println and run it in the main () method)

Public void testcalendar (){
Calendar C1 = calendar. getinstance ();
C1.settime (new date ());

// When the time set in the calendar exceeds the maximum value of each item, the time is set based on the value after the maximum value is subtracted. For example, if the month is set to 13, the time is changed to 13-11 = 02.
Calendar C2 = calendar. getinstance ();
C2.set (1920, 13, 24, 22, 32, 22 );
// Use pattern
Simpledateformat format = new simpledateformat ("yyyy-mm-dd H: M: s ");
Simpledateformat format2 = new simpledateformat ("YY-mm-dd H: M: s ");
Simpledateformat format3 = new simpledateformat ("Y-m-d h: M: s ");
// Use the agreed format

Dateformat = dateformat. getdateinstance (dateformat. Full, locale. getdefault ());

 

// Obtain the attribute fields in the calendar
Log.info ("the year now time is" + c1.get (c1.year ));
Log.info ("the month now time is" + c1.get (c1.month ));
Log.info ("The day_of_month now time is" + c1.get (c1.day _ of_month ));
Log.info ("The day_of_week now time is" + c1.get (c1.day _ of_week ));
Log.info ("Today is the week of this month:" + c1.get (c1.day _ of_week_in_month ));
Log.info ("The day_of_year now time is" + c1.get (c1.day _ of_year ));
// The formats of different modes are slightly different. You can test more modes if you have time.
Log.info ("yyyy-mm-dd H: M: s -->" + format. Format (c1.gettime ()));
Log.info ("YY-mm-dd H: M: s -->" + format2.format (c1.gettime ()));
Log.info ("Y-m-d h: M: s -->" + format3.format (c1.gettime ()));
Log.info ("dateformat. Full -->" + dateformat. fomat (c1.gettime ()));

Log.info (format. Format (c2.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.