How to Use simpledateformat in Java

Source: Internet
Author: User
24-hour time display:

public class Datetime {

    public static void main(String args[]){
         java.util.Date current=new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String c=sdf.format(current);
           System.out.println(c);
    }
}

12-hour time display:


public class Datetime {

    public static void main(String args[]){
         java.util.Date current=new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
           String c=sdf.format(current);
           System.out.println(c);
    }
}

Difference: yyyy-mm-dd hh: mm: SS; yyyy-mm-dd hh: mm: SS

As follows:

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

 

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.