How to Use simpledateformat format

Source: Internet
Author: User
Tags cst time date now

In Java, how can we convert a date to the desired format, or convert a string to a date in a certain format, such as converting a date or time in the database into a desired format, java provides the simpledateformat class for implementation. The following is the usage and example of simpledateformat:

Java. Lang. Object
Java. Text. Format
Java. Text. dateformat

Java. Text. simpledateformat

All Implemented interfaces:

Serializable, cloneable

Simpledateformat is a specific class related to the language environment to format and parse dates. It allows formatting (date-> text), parsing (text-> date), and normalization.

Simpledateformat allows you to select any custom date-Time Format mode. However, we recommend that you use gettimeinstance, getdateinstance, or getdatetimeinstance in dateformat to create a date-time formatter. Every such method can return a date/time formatter initialized in the default format.

Package Org. BUPT. xiaoye. chapter3; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. date; public class dateformattest {public static void main (string [] ARGs) throws parseexception {simpledateformat ceshifmt0 = new simpledateformat ("mm minutes SS seconds ON dd, mm, gyyyy"); simpledateformat ceshifmt1 = new simpledateformat ("yyyy/mm/dd hh: MM "); simpledateformat ceshifmt2 = new simpledateformat (" yyyy-mm-dd hh: mm: SS "); simpledateformat ceshifmt3 = new simpledateformat ("mm: DD, yyyy, HH, MM: SS seconds e"); simpledateformat ceshifmt4 = new simpledateformat ("yyyy/mm/dd e "); simpledateformat ceshifmt5 = new simpledateformat ("day d in a year, week W, week W in a month, and Time Zone Z in K"); 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 ));}}

Running result:

October 19, 2014 AD

2014-10-19 18:08:08
Sunday, December 18, October 19, 2014
Sunday
292nd days in a year, 43rd weeks, 4th weeks in a month, 18 hours CST Time Zone


Simpledateformat:

G-era identifier
Y years
M month
D
H: In the morning or afternoon (1 ~ 12)
H: In a day (0 ~ 23)
MB
S seconds
S millisecond
E week
D. day of the year
F The day of the week in January
W. week of the year
W. week of March January
A morning/afternoon Mark
K hours in a day (1 ~ 24)
K hours in the morning or afternoon (0 ~ 11)
Z Time Zone


How to Use simpledateformat format

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.