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 );
}
}