=================================================
My summary: A summary of the date-time formats in Java
=================================================
1, date and time related to the class:
First: java.util.Date;
Second: Java.util.Calender;
Third: Java.text.DateFormat
IV: Java.text.SimpleDateFormate
2. Step method:
1 steps to obtain the system datetime (using date Get):
Step one: Determine the event format to output simpledateformate SDF = new Simpledateformate ("Yyyy-mm-dd hh:mm:ss");
Step two: Get the current time date nowdate = new Date ();
Step three: Output the current time System.out.println (Sdfdong.format (Nowdatedong));
2 steps to get the system datetime (use Calendar to get):
Step one: Determine the event format to output simpledateformate SDF = new Simpledateformate ("Yyyy-mm-dd hh:mm:ss");
Step two: Get the current time Calendar nowcal = Calendar.getinstance ();
Step three: Output the current time System.out.println (Sdfdong.format (Nowdatedong));
3 Description of date time and format class:
(1) Format description in Simpledateformate class:
Y on behalf of the year, M for the month, D for the day, h for the time, m for the minute, s for the second
Simpledateformate is a subclass of Dateformate, a concrete class that formats and resolves dates in a locale-related manner.
It allows formatting (date-> text), parsing (text-> date), and normalization
The @ Formatting method is: formate, format the date as a string for output, return type is StringBuilder, parameter is a Date object,
Use the method: String mynowdate = sdf.formate (nowdate)
The @ Resolution method is: Parse, parsing the text to a definite date, the prototype is date parse (String text, parseposition POS)
The return type is date, the argument string text is the input string, and the argument Parposition POS begins parsing from the string
Use the method: Date nowdate = Sdf.parse ("2010-11-24 14:19:00");
"Attention" to the use of dateformate: DateFormat Mydateformat = Dateformate.getdateinstance (); obtained is the default time zone, the default language
Default format date time format; Getdatetimeinstance of this class is often used to set the date-time format for a specific time zone, a specific language
Examples are:
DateFormat df=new SimpleDateFormat ("Yyyy-mm-dd EE hh:mm:ss");
System.out.println (New Date ()) (Df.format);
Date date = new Date ();
DateFormat shortdate=dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short);
DateFormat mediumdate =dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);
DateFormat longdate =dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long);
DateFormat fulldate =dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);
System.out.println (Shortdate.format (date));
System.out.println (Mediumdate.format (date));
System.out.println (Longdate.format (date));
System.out.println (Fulldate.format (date));
08-4-15 3:24
2008-4-15 15:24:31
April 15, 2008 03:24 P.M. 31 seconds
April 15, 2008 Tuesday 03:24 P.M. 31 sec CST
(2) The use of calendar:
The usual ways to use calendar are: getinstance (), add (), get (), set (), GetTime (), SetTime (), set (), Getmaxium (),
Getactualmaximum (), Getactualminimum ()
@ Use getinstance to generate Calendar objects: Calendar RightNow = Calendar.getinstance ();
The "note" GetInstance method has 4 overloads for getting a set time zone, a calendar in the language environment,
No parameter is the calendar that gets the default time zone and language
@ Use the Add () method to change a specific date and time (such as the monthly plus and minus, day plus minus):
abstract void Add (int field, int amount);
Using the example: 10 hours after the time
Java.util.Calendar cal=java.util.calendar.getinstance ();
Cal.settime (Dateoper);
Cal.add (java.util.calendar.hour_of_day,10); If it was 10 hours ago, 10.
System.out.println ("Date:" +forma.format (Cal.gettime ()));
@ Use the Get () method to obtain a specific field in the calendar: int gets (int field); Returns the value of a given calendar field
Use examples: int daysofyear = Nowcal.get (calendar.date_of_year);
@ Use the Set () method to get
@ Use the GetTime () method to get the current time: Date getTime ();//Returns a representation of this Calendar time value
Date object (from the calendar to the current millisecond offset)
@ Using the SetTime () method: void SetTime (date date); Sets the time for this Calendar with the given Date.
@ using the Set () method: The Set () method has 4 overloads, respectively
void set (int field, int value);//Set the given Calendar field to the given value
void Set (int year, int month, int date); Set the values for year, MONTH, and Day_of_month in calendar fields
void Set (int year, int month, int date, int hourofday, int minute); Set calendar fields year, MONTH,
Values for Day_of_month, Hour_of_day, and MINUTE
void Set (int year, int month, int date, int hourofday, int minute, int second); Set field year,
Values for MONTH, Day_of_month, HOUR, MINUTE, and SECOND
@ Use the Getmaxium () method to get the maximum value for a given calendar field: Abstract int getminimum (int field);
parameter is int field, used to set the fields such as year, month, day, time, minute, second, etc.
@ Use the Getactualmaximum () method to get the maximum value that may be in the Calendar field: int getactualmaximum (int field);
Use the Getactualminimum () method to get the smallest possible value in the Calendar field: int getactualminimum (int field);
(3) The use of date:
The main use is used to obtain the current example GMT (January 1, 1970 00:00:00), the common methods are not many
Usages: Find the number of days between two dates:
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date begindate= format.parse ("2007-12-24");
Java.util.Date enddate= format.parse ("2007-12-25");
Long day= (Date.gettime ()-mydate.gettime ())/(24*60*60*1000);
System.out.println ("Days apart =" +day);
Date of a year ago:
Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date todaydate=new java.util.Date ();
Long Beforetime= (Todaydate.gettime ()/1000) -60*60*24*365;
Todaydate.settime (beforetime*1000);
String Beforedate=formatter.format (todaydate);
System.out.println (beforedate);
3. Appendix A small example: to calculate the distance from the birthday how many days
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
/**
* When the date format of the relevant contact
* @author Administrator
* Time: 2010-11-23
* */
public class Calculatebirthday {
/**
* @param str Birthday Date
* How many days @return away from the birthday
* @throws ParseException
* */
public static long Daysbeforebirthday (String str) throws parseexception{
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date birthday = Sdf.parse (str);
Date Nowday = new Date ();
Calendar birthcal = Calendar.getinstance ();
Calendar nowcal = Calendar.getinstance ();
Birthcal.settime (birthday);
Nowcal.settime (Nowday);
Birthcal.set (Calendar.year, Nowcal.get (calendar.year));
int bdayofyear = Birthcal.get (calendar.day_of_year);
int ndayofyear = Nowcal.get (calendar.day_of_year);
if (Bdayofyear > Ndayofyear) {
return (bdayofyear-ndayofyear);
}else if (Bdayofyear < ndayofyear) {
Birthcal.add (calendar.year, 1);
Long disttimeinmills = Birthcal.gettimeinmillis ()-Nowcal.gettimeinmillis ();
Return disttimeinmills/(24*60*60*1000);
}else{
return 0;
}
}
public static void Main (string[] args) throws parseexception{
System.out.println (Daysbeforebirthday ("1988-08-28") + "Day");
System.out.println (Daysbeforebirthday ("1988-03-21") + "Day");
System.out.println (Daysbeforebirthday ("1990-11-24") + "Day");
}
}