Java Get date and time method summaryIn writing a Java program is always unavoidable with the date and time, especially in the project, in accordance with a variety of requirements to display different formats of the date and time, it is necessary to quickly determine what kind of good, the following on the date and time of the format of the summary.
method One: Use the Java.util.Date class to implement, and combine the Java.text.DateFormat class to realize the time format
Package net.singlex.javadate;
Transport
/** * The following default time dates are displayed in Chinese way * general language on the default Chinese on it, the time date format defaults to Medium style, * For example: 2013-6-10 13:14:41 * The date times shown below are based on the date class, and you can use the Calendar class to implement see Class Testdate2.java * * @author singlex * */ import java.util.*; import java.text.*; public class testdate { public static void main (String[] args) { date now = new date (); calendar cal = calendar.getinstance (); DateFormat d1 = Dateformat.getdateinstance (); // default style (medium style, for example: 2013-6-10 13:14:41) in default language (Chinese) string str1 = d1.format (now); DateFormat d2 = Dateformat.getdatetimeinstance (); string str2 = d2.format (now); DateFormat d3 = Dateformat.gettimeinstance (); string str3 = d3.format (now); dateformat d4 = dateformat.getinstance (); // display date and time string str4 = using short style d4.format (now); DateFormat d5 = Dateformat.getdatetimeinstance (dateformat.full, dateformat.full); // display date, week, time (accurate to second) string str5 = d5.format (now); DateFormat d6 = Dateformat.getdatetimeinstance (Dateformat.long, dateformat.long); // displays the date. Time (accurate to second) string str6 = d6.format (now); DateFormat d7 = Dateformat.getdatetimeinstance (dateformat.short, dateformat.short); // display date, time (exact to minute) string str7 = d7.format (now); DateFormat d8 = Dateformat.getdatetimeinstance (dateformat.medium, dateformat.medium); // display date, time (exact to minute) string str8 = d8.format (now);// compared to the short style , it is best to use         SYSTEM.OUT.PRINTLN ("Show Time in Date: " + Now);// the results shown by this method and Calendar.getinstance (). GetTime () system.out.println ("Dateformat.getdateinstance () after the format of the time:"  + STR1); system.out.println ("with Dateformat.getdatetimeinstance () After the format of the time: " + STR2"; system.out.println ("with Dateformat.gettimeinstance () After the format of the time: " + STR3";         SYSTEM.OUT.PRINTLN ("Format time with Dateformat.getinstance () is:"  + STR4); system.out.println ("with Dateformat.getdatetimeinstance ( Dateformat.full,dateformat.full) After the format of the time: " + STR5"; system.out.println ("with Dateformat.getdatetimeinstance ( Dateformat.long,dateformat.long) After the format of the time: " + STR6";         SYSTEM.OUT.PRINTLN ("Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short) after the format of the time:"  + STR7); system.out.println ("with Dateformat.getdatetimeinstance ( Dateformat.medium,dateformat.medium) After the format of the time: " + str8";    &NBSP} }
Row results:
date display time: Mon June 13:14:41 CST 2013
After the format of Dateformat.getdateinstance (): 2013-6-10
After the dateformat.getdatetimeinstance () format time is: 2013-6-10 13:14:41
with dateformat.gettimeinstance () format time is: 13:14:41
after the dateformat.getinstance () format time is: 13-6-10 afternoon 1:14
with Dateformat.getdatetimeinstance (Dateformat.full, Dateformat.full) format time: June 10, 2013 Monday 01:14 P.M. 41 sec CST
with dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long) After the format time: June 10, 2013 01:14 P.M. 41 seconds
with Dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short) After the format of the time: 13-6-10 afternoon 1:14
with Dateformat.getdatetimeinstance (dateformat.medium,dateformat.medium ) after the format date: 2013-6-10 13:14:41
method Two: Use Java.util.Calendar class to realize
Transport
package net.singlex.javadate; /** * The following is the use of the Calendar class to implement DateTime, and the date class is relatively simple * * @author & nbsp Singlex */ import java.util.*; public class testdate2 { public static void main (String[] args) { calendar ca = calendar.getinstance (); int year = ca.get (Calendar.YEAR);// Get year int month = ca.get (Calendar.MONTH);// Get month int day = ca.get (Calendar.DATE) ;// Get day int minute = ca.get ( Calendar.minute);// int hour = ca.get (calendar.hour);// hours int second = ca.get (Calendar.second);// sec int weekofyear = ca.get (Calendar.day_of_week);         SYSTEM.OUT.PRINTLN ("Use Calendar.getinstance ()." GetTime () mode displays the time: " + ca.gettime ()");         SYSTEM.OUT.PRINTLN ("Get Date with Calendar is:" + year + "Year" + month + "month" + day + "Day");         SYSTEM.OUT.PRINTLN ("Get Time with Calendar is:" + hour + "Time" + minute + "minutes" + second + "seconds");         SYSTEM.OUT.PRINTLN (WeekOfYear);// Show today is the first day of the week (counting from the weekend, I run the Monday, so the result is 2) &NBSP} }
The line result is:
use Calendar.getinstance (). GetTime () to display the time: Mon June 13:27:34 CST 2013
with calendar date is: May 10, 2013
Time to use calendar is: 1:27 34 seconds
2
method Three: SimpleDateFormat 24-hour time display
An alphabetic date or time element represents an example
G Era identifier Text AD
Y year 1996; 96
M years of the month Month July; June June; 07
Number of weeks in W year 27
Number of weeks in the W month 2
Number of days in D year 189
Days in D of month Number 10
Week number 2 in the F month
Days of E week Text Tuesday; Tue
A am/pm tagged Text pm
H hours in a day (0-23) number 0
K Hours in a day (1-24) Number 24
Hours in K-am/pm (0-11) number 0
H am/pm hours (1-12) Number 12
Minutes in M-hour number 30
s minutes in seconds number 55
S milliseconds number 978
Z Time zone zone Pacific Standard; PST; gmt-08:00
Z timezone RFC 822 time zone-0800
One advantage of this is that it supports two-bit months, two-bit dates. With a prefix of 0
date date = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String Sdatesuffix = Dateformat.format (date);
System.out.println ("Sdatesuffix:" + sdatesuffix); This piece of the program ran results: sdatesuffix:2013-06-10 13:31:23