SimpleDateFormat use of the detailed

Source: Internet
Author: User
Tags cst time date now dateformat

public class SimpleDateFormat extends DateFormat

SimpleDateFormatis a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-and-text), parsing (text, date), and normalization.

SimpleDateFormat   allows you to start by selecting any user-specified method for date-time formatting. However, want to use   dateformat   gettimeinstance ,   getdateinstance   or   getdatetimeinstance   Create a date-time formatter. Each class method returns a date/time formatter initialized in the default format. You can modify the formatting by   the Applypattern   method as needed.  

SimpleDateFormat function Inheritance relationship:
java.lang.object
   |
   +----Java.text.Format
           |
           +----Java.text.DateFormat
                    |
                    +----Java.text.SimpleDateFormat
Below is a small example:
Import java.text.*;
Import java.util.Date;

/**
SimpleDateFormat function Syntax:

G-Age Marker
Y years
M-month
D-Day
h in the morning or afternoon (1~12)
H at the time of Day (0~23)
M min
s S
S ms
E-week
D the first day of the year
F January The first few weeks
W The first few weeks of the year
W January The first few weeks
A morning/afternoon marker
K in one day (1~24)
K in the morning or afternoon (0~11)
Z Time zone
*/
public class FormatDateTime {

public static void Main (string[] args) {
SimpleDateFormat myfmt=new SimpleDateFormat ("YYYY year mm month DD Day hh mm min ss seconds");
SimpleDateFormat myfmt1=new SimpleDateFormat ("Yy/mm/dd hh:mm");
SimpleDateFormat myfmt2=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//equivalent to now.tolocalestring ()
SimpleDateFormat myfmt3=new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec E");
SimpleDateFormat myfmt4=new SimpleDateFormat (
"D-day of the year, the first W week of January, the first W week of the year, the K-time Z time zone in a day");
Date Now=new date ();
System.out.println (Myfmt.format (now));
System.out.println (Myfmt1.format (now));
System.out.println (Myfmt2.format (now));
System.out.println (Myfmt3.format (now));
System.out.println (Myfmt4.format (now));
System.out.println (Now.togmtstring ());
System.out.println (Now.tolocalestring ());
System.out.println (Now.tostring ());
}

}

Effect:
December 16, 2004 17:24 27 seconds
04/12/16 17:24
2004-12-16 17:24:27
December 16, 2004 17:24 27 sec Thu
No. 351 Day of the Year 51st Monday Month 3rd week in one day 17 o'clock CST time zone
Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 17:24:27 CST 2004

Here is a JavaBean:
public class FormatDateTime {

public static String tolongdatestring (Date dt) {
SimpleDateFormat myfmt=new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec E");
return Myfmt.format (DT);
}

public static String toshortdatestring (Date dt) {
SimpleDateFormat myfmt=new SimpleDateFormat ("yy years mm month DD day hh mm");
return Myfmt.format (DT);
}

public static String tolongtimestring (Date dt) {
SimpleDateFormat myfmt=new SimpleDateFormat ("HH mm SS SSSS");
return Myfmt.format (DT);
}
public static String toshorttimestring (Date dt) {
SimpleDateFormat myfmt=new SimpleDateFormat ("Yy/mm/dd hh:mm");
return Myfmt.format (DT);
}

public static void Main (string[] args) {

Date Now=new date ();

System.out.println (Formatdatetime.tolongdatestring (now));
System.out.println (Formatdatetime.toshortdatestring (now));
System.out.println (Formatdatetime.tolongtimestring (now));
System.out.println (Formatdatetime.toshorttimestring (now));
}

}
The main test result of the call:
December 16, 2004 17:38 26 sec Thu
December 16, 04 17:38
17 38 26 0965
04/12/16 17:38

SimpleDateFormat use of the detailed

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.