Brief usage of SimpleDateFormat in Java

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

How can you convert a date into a desired format in Java, or convert a string to a date of a certain format, such as converting a date or time in a database to the format you want, Java provides the SimpleDateFormat class to implement, Here are the usages and examples of SimpleDateFormat:
Java.text
Class SimpleDateFormat
Java.lang.Object
Java.text.Format
Java.text.DateFormat
Java.text.SimpleDateFormat
All implemented interfaces:
Serializable, cloneable

SimpleDateFormat is a specific class that formats and parses dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization.
SimpleDateFormat allows you to select any user-defined pattern of date-time formats. However, it is still recommended to create a date-time formatter through gettimeinstance, getdateinstance, or getdatetimeinstance in DateFormat. Each of these class methods can return a date/time formatter initialized in the default format pattern.

Package Ceshi;

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Ceshi {

public static void Main (string[] args) throws ParseException {
SimpleDateFormat ceshifmt0=new SimpleDateFormat ("Gyyyy year mm month DD Day hh mm min ss seconds");
SimpleDateFormat ceshifmt1=new SimpleDateFormat ("Yyyy/mm/dd hh:mm");
SimpleDateFormat ceshifmt2=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
SimpleDateFormat ceshifmt3=new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec E");
SimpleDateFormat ceshifmt4=new SimpleDateFormat ("Yyyy/mm/dd E");
SimpleDateFormat ceshifmt5=new SimpleDateFormat (
"D Day of the year, W Week, one-month, W-week, K-time Z time zone");
Date Now=new date ();
System.out.println (Ceshifmt0.format (now));
System.out.println (Ceshifmt1.format (now));
System.out.println (Ceshifmt2.format (now));
System.out.println (Ceshifmt3.format (now));
System.out.println (Ceshifmt4.format (now));
System.out.println (Ceshifmt5.format (now));
}

}



The result is: July 27, 2010 A.D. 09:19 29 seconds
2010/07/27 09:19
2010-07-27 09:19:29
July 27, 2010 09:19 29 sec Tuesday
2010/07/27 Tuesday
No. 208 Day of the year, 31 weeks, one months, 5 weeks, 9 o'clock CST time zone


SimpleDateFormat Date-time format mode parameter:

Alphabetical date or time element represents an example
G Era marker Text AD
Y year 1996; 96
The month in M year July; Jul; 07
Weeks in W year number 27
Weeks in W month number 2
Days in D year number 189
Days in the D month Number 10
Week number 2 in the F month
number of days in E-week Text Tuesday; Tue (when I was deploying in native Oracle is the return week is Tuesday, and on the Oracle server is the return Tue.)A am/pm tagged Text pm
H hours in the day (0-23) number 0
K Hours in the day (1-24) Number 24
Hours in K am/pm (0-11) number 0
Hours in H am/pm (1-12) Number 12
Minutes in M-hour number 30
Seconds in S minutes number 55
S MS Number 978
Z TimeZone general time zone Pacific PST; gmt-08:00
Z timezone RFC 822 time zone-0800

Brief usage of SimpleDateFormat in Java

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.