java-using SimpleDateFormat to format time output

Source: Internet
Author: User
Tags dateformat days in month

Before a blog is to use NSDateFormatter to format the output of time, but the use of a bit cumbersome, today introduced under the recently used SimpleDateFormat.

1  Public class extends DateFormat
Date and Time mode

Date and time formats are specified by a date and time pattern string. In a date and time pattern string, the unquoted letters ‘A‘ to ‘Z‘ and ‘a‘ to ‘z‘ are interpreted as pattern letters, which are used to represent date or time string elements. Text can be caused by using single quotation marks ( ) to avoid interpretation. "‘‘"represents a single quotation mark. All other characters are not interpreted; Simply copy them to the output string when they are formatted, or 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 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
Example The following example shows how to interpret the date and time patterns in the U.S. locale. Given the date and time of the United States Pacific Time zone for local time 2001-07-04 12:08:56.
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

Common construction Methods:

SimpleDateFormat Sformat = new SimpleDateFormat (String pattern);

Or

SimpleDateFormat Sformat = new SimpleDateFormat ();

Sformat.applypattern (String pattern);

Or

DateFormat DateFormat = dateformat.getdateinstance (DateFormat. full, Locale.getdefault ());

Specific desirable dateformat.short,default,long,full, etc.

Sample code

1  Public voidTestcalendar () {2Calendar C1 =calendar.getinstance (); 3C1.settime (NewDate ()); 4           5         //when the time set in the calendar exceeds the maximum value for each item, the time is set with the value minus the maximum, such as the month setting of 13, and finally the 13-11=026Calendar C2 =calendar.getinstance (); 7C2.set (1920, 13, 24, 22, 32, 22); 8         //using the pattern9SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd h:m:s"); TenSimpleDateFormat FORMAT2 =NewSimpleDateFormat ("Yy-mm-dd h:m:s");  OneSimpleDateFormat FORMAT3 =NewSimpleDateFormat ("Y-m-d h:m:s");  A         //Use contract Format -    -DateFormat DateFormat =dateformat.getdateinstance (Dateformat.full, Locale.getdefault ());  the          //ways to get the individual 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 first few weeks of the month:" +C1.get (C1.  Day_of_week_in_month));  +Log.info ("The Day_of_year now time is" +C1.get (C1.  Day_of_year));  A         //different modes correspond to a slightly different format, there is time to test a little more mode atLog.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 ()));  -}

java-using SimpleDateFormat to format time output

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.