Java date conversion simpledateformat format Daquan and judge whether it is a legal Date and Time string?

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

-------------------------------------------------------------

Public class judgedate {

/**
* Judge whether it is a valid date and time string
* @ Param str_input
* @ Return Boolean; true: false.
*/
Public static Boolean isdate (string str_input, string rdateformat ){
If (! Isnull (str_input )){
Simpledateformat formatter = new simpledateformat (rdateformat );
Formatter. setlenient (false );
Try {
Formatter. Format (formatter. parse (str_input ));
} Catch (exception e ){
Return false;
}
Return true;
}
Return false;

}
Public static Boolean isnull (string Str ){
If (STR = NULL)
Return true;
Else
Return false;
}
Public static void main (string ARGs []) {
String STR = "2007-5-12 ";
If (isdate (STR, "YYYY. Mm. dd") | isdate (STR, "yyyy-mm-dd "))
System. Out. Print ("this is true ");
Else
System. Out. println (STR );
}
}

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.