Formatted output date and time

Source: Internet
Author: User
If you want to output the date and time format you want, it is necessary to use the SimpleDateFormat class, which is the only non-abstract subclass of the DateFormat class, which has a powerful function in displaying the date and time. For example, to output "Sunday 2004.06.06 at 06:53:01 pm CST", as long as an instance is explicitly constructed:
SimpleDateFormat formatter=new SimpleDateFormat ("E yyyy. Mm.dd ' at ' hh:mm:ss a zzz ');
where e represents the day of the week, YYYY, MM, DD denotes the year, month, and date; ' At ' means the string "at"; Hh:mm:ss represents time; a indicates that the A.M. or p.m;zzz represents the time zone;
The following is the format code in the SimpleDateFormat class
Symbolic meaning
G A.D.
Y years
M-month
D-Day
H a.m/p.m Hour (1-12)
H 24 hour clock (0-23)
M min
s S
S ms
E Week
D the first day of the year
F the first few weeks of the month
W The first few weeks of the year
W one months in the first few weeks
A a.m/p.m mark
K 24-hour clock (1-24)
The hour of the K a.m/p.m system (0-11)
Z Time zone
' Escape character
"Single quote import java.util.*;
Import java.text.*;
public class Dodate {public dodate () {
} public String fotmatDate1 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec");
String strdate = Formatter.format (mydate);
return strdate;
}
Public String FotmatDate2 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy year mm DD day");
String strdate = Formatter.format (mydate);
return strdate;
}
Public String fotmatDate3 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String strdate = Formatter.format (mydate);
return strdate;
}
Public String fotmatDate4 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("E yyyy. Mm.dd ' at ' hh:mm:ss a zzz ');
String strdate = Formatter.format (mydate);
return strdate;
}
Public String fotmatDate5 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy/mm/dd");
String strdate = Formatter.format (mydate);
return strdate;
}
Public String fotmatDate6 (Date mydate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Mm-dd hh:mm");
String strdate = Formatter.format (mydate);
return strdate;
}
public static void Main (String args[]) {
System.out.println ("AAA");
Dodate dd=new dodate ();
System.out.println (Dd.fotmatdate1 (New Date ()));
System.out.println (Dd.fotmatdate2 (New Date ()));
System.out.println (Dd.fotmatdate3 (New Date ()));
System.out.println (Dd.fotmatdate4 (New Date ()));
System.out.println (Dd.fotmatdate5 (New Date ()));
System.out.println (Dd.fotmatdate6 (New Date ()));
}
}
Running Result: C:/java>java dodate
Aaa
June 06, 2004 18:53 00 seconds
June 06, 2004
2004-06-06 18:53:01
Sunday 2004.06.06 at 06:53:01 pm CST
2004/06/06
06-06 18:53

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.