Date class
In JDK1.0, the date class is the only class that represents time, but since the date class is not easy to internationalize, starting with the JDK1.1 version, it is recommended to use the Calendar class for time and date processing. Here is a brief introduction to the use of the date class.
1. Use the date class to represent the current system time
Date d = new Date ();
System.out.println (d);
The object created by using the default construction method of the date class represents the current time, and because the date class overrides the ToString method, you can output the object of date type directly, and the result is as follows:
Sun Mar 16:35:58 CST 2009
In this format, Sun stands for Sunday (Sunday), Mar for March (March), 08 for 8th, and CST for China Standard Time, which is GMT (East eight zone).
2. Use the date class to represent the specified time
Date D1 = new Date (2009-1900,3-1,9);
System.out.println (D1);
Using a constructed method with parameters, you can construct a date class object of the specified dates, and the parameters for the year in the date class should be the year minus 1900 that actually needs to be represented, minus the value after 1 for the month actually represented. For example, the example code above represents the March 9, 2009.
A Date object that actually represents a specific month and a minute, and this is similar.
3. Get the information in the Date object
Date D2 = new Date ();
Year
int year = D2.getyear () + 1900;
Month
int month = D2.getmonth () + 1;
Date
int date = D2.getdate ();
Hours
int hour = D2.gethours ();
Minutes
int minute = D2.getminutes ();
Seconds
int second = D2.getseconds ();
Day of the Week
int day = D2.getday ();
System.out.println ("Years:" + year);
System.out.println ("Month:" + month);
System.out.println ("Date:" + date);
System.out.println ("Hours:" + hour);
System.out.println ("minutes:" + minute);
System.out.println ("Seconds:" + second);
System.out.println ("Week:" + day);
Using the corresponding Get method in the date class, you can get the relevant information in the date class object, and it is important to note that using getyear to get the value after the year in the Date object minus 1900, so it is necessary to display the corresponding year by adding 1900 on the basis of the return value, similar to the month. The Getday method is also provided in the date class to obtain a Date object representing the day of the week, and the date class specifies that Sunday is 0, Monday is 1, Tuesday is 2, followed by and so on.
4. The reciprocal transfer between the Date object and the relative time
Date D3 = new Date (2009-1900,3-1,10);
Long time = 1290876532190L;
Converts an object of the date class to a relative time
Long T = D3.gettime ();
System.out.println (t);
Convert relative time to object of date class
Date D4 = new Date (time);
System.out.println (D4);
Using the GetTime method in the Date object, you can convert an object of the date class to a relative time, using the construction method of the date class to convert the relative time to the object of the date class. After the conversion, it is convenient for the calculation of time, but also makes the time display more intuitive.
Calendar class
Starting with the JDK1.1 version, it is recommended that the Calendar class be implemented when the date and time are processed. In design, the function of the calendar class is much more powerful than the date class, and in the implementation of the method is more complex than the date class, the following describes the use of the Calendar class.
The Calendar class is an abstract class that implements the object of a particular subclass when it is actually used, and the process of creating the object is transparent to the programmer and only needs to be created using the GetInstance method.
1. Use the Calendar class to represent the current time
Calendar C = calendar.getinstance ();
Because the Calendar class is an abstract class, and the Calendar class is constructed protected, you cannot create objects using the Calendar class's constructor, which provides the GetInstance method for creating objects.
Using this method to obtain the Calendar object represents the current system time, because the Calendar class ToString implementation of the No date class is so intuitive, so the direct output of the Calendar class object is not very meaningful.
2. Use the Calendar class to represent the specified time
Calendar C1 = Calendar.getinstance ();
C1.set (2009, 3-1, 9);
Using the Calendar class to represent a specific time, you need to first create a Calendar object, and then set the date parameter in that object to complete.
The declaration of the Set method is:
Public final void Set (int year,int month,int date)
The example code above is set to March 9, 2009, and its parameters are not the same structure as the date class. The values of the year in the Calendar class are written directly, the value of the month is the actual month value minus 1, and the date value is the actual date value.
If you only set a field, such as a value for a date, you can use the following set method:
public void Set (int field,int value)
In this method, the parameter field represents the type of field to be set, and the common types are as follows:
calendar.year--year
calendar.month--Month
calendar.date--Date
calendar.day_of_month--date, exactly the same as the field above
Number of hours calendar.hour--12 hours
Number of hours calendar.hour_of_day--24 hours
calendar.minute--min
calendar.second--sec
calendar.day_of_week--Day of the week
The subsequent parameter value represents the value that is set to. For example:
C1.set (calendar.date,10);
The purpose of this code is to set the date represented by the C1 object to number 10th, and all other values will be recalculated, such as the day of the week and the corresponding relative time value.
3. Get the information in the Calendar class
Calendar C2 = calendar.getinstance ();
Year
int year = C2.get (calendar.year);
Month
int month = C2.get (calendar.month) + 1;
Date
int date = C2.get (calendar.date);
Hours
int hour = C2.get (Calendar.hour_of_day);
Minutes
int minute = C2.get (Calendar.minute);
Seconds
int second = C2.get (Calendar.second);
Day of the Week
int day = C2.get (Calendar.day_of_week);
System.out.println ("Years:" + year);
System.out.println ("Month:" + month);
System.out.println ("Date:" + date);
System.out.println ("Hours:" + hour);
System.out.println ("minutes:" + minute);
System.out.println ("Seconds:" + second);
System.out.println ("Week:" + day);
Using the Get method in the Calendar class to obtain the corresponding information in the Calendar object, the Get method is declared as follows:
public int get (int field)
Where the parameter field represents the value of the field that needs to be obtained, the field description is consistent with the set method above. It should be stated that the obtained month is the actual month value minus 1, and the obtained week value is not the same as the date class. In the Calendar class, Sunday is 1, Monday is 2, Tuesday is 3, and so on.
4. Other method description
In fact, the Calendar class also provides a lot of other useful methods, the following simple introduction of the use of several common methods.
A, Add method
public abstract void Add (int field,int amount)
The function of this method is to increase or decrease a certain number of values on a field in the Calendar object, increase the value of amount to positive, and decrease the value of amount when it is negative.
For example, after calculating the date 100 days after the current time, the code is as follows:
Calendar C3 = Calendar.getinstance ();
C3.add (calendar.date, 100);
int year1 = C3.get (calendar.year);
Month
int month1 = C3.get (calendar.month) + 1;
Date
int date1 = C3.get (calendar.date);
System.out.println (year1 + "year" + Month1 + "Month" + date1 + "Day");
The Add method here refers to the calendar.date of the C3 object, which is the addition of 100 on the Date field, and the value of the other fields in the Date object is recalculated inside the class to obtain a date after 100 days, for example, the output of the program may be:
June 17, 2009
B, after method
public Boolean after (Object when)
The function of this method is to determine whether the current date object is behind the When object, or False if it returns true after the When object. For example:
Calendar C4 = calendar.getinstance ();
C4.set (2009, 10-1, 10);
Calendar c5 = calendar.getinstance ();
C5.set (2010, 10-1, 10);
Boolean B = C5.after (C4);
System.out.println (b);
In the sample code, the time represented by the object C4 is October 10, 2009, and the time the object C5 represents is October 10, 2010, then the date represented by the object C5 is after the date represented by the C4, so the return value of the After method is true.
Another similar approach is before, which is to determine whether the current date object is in front of another date object.
C, gettime method
Public final Date GetTime ()
The purpose of this method is to convert the object of the calendar type to the corresponding date class object, which represents the same point in time.
A similar approach is settime, which is used to convert a Date object to the corresponding calendar object, which is declared as follows:
Public final void SetTime (date date)
The sample code for the conversion is as follows:
Date d = new Date ();
Calendar C6 = calendar.getinstance ();
object of the calendar type is converted to a Date object
Date D1 = C6.gettime ();
Convert an object of type date to a calendar object
Calendar c7 = Calendar.getinstance ();
C7.settime (d);
5, the Calendar object and the relative time between the mutual transfer
Calendar C8 = Calendar.getinstance ();
Long T = 1252785271098L;
Convert a Calendar object to a relative time
Long T1 = C8.gettimeinmillis ();
Convert relative time to calendar object
Calendar C9 = Calendar.getinstance ();
C9.settimeinmillis (t1);
When converting, use the Gettimeinmillis method in the Calendar class to convert a calendar object to a relative time. When converting a relative time to a calendar object, first create a Calendar object, and then use the Calendar class's Settimeinmillis method to set the time.
Application examples
The basic use of time and date processing is described in two simple examples below.
1. Calculate the number of days between two dates
For example, to calculate the number of days between April 1, 2010 and March 11, 2009, you can use time and date processing for calculations.
The program implementation of the principle is: first represents two specific points of time, where the object of the calendar is represented, and then convert two time points to the corresponding relative time, two time points for the difference between time, and then divided by 1 days of the number of milliseconds (24 hours X60 minutes X60 seconds X1000 milliseconds) You can get the corresponding number of days. The complete code to implement the example is as follows:
Import java.util.*;
/**
* Calculates the number of days between two dates
*/
public class DateExample1 {
public static void Main (string[] args) {
Set a two date
Date: March 11, 2009
Calendar C1 = Calendar.getinstance ();
C1.set (2009, 3-1, 11);
Date: April 1, 2010
Calendar C2 = calendar.getinstance ();
C2.set (2010, 4-1, 1);
Convert to relative time
Long T1 = C1.gettimeinmillis ();
Long t2 = C2.gettimeinmillis ();
Count Days
Long days = (T2-T1)/(24 * 60 * 60 * 1000);
System.out.println (days);
}
}
2, output the current month calendar
The function of this example is to output the calendar for the month of the current system time, for example, the current system time is March 10, 2009, and the March 2009 calendar.
The implementation of the program is: first get the month 1th is the day of the week, and then get the number of days of the month, and finally use the process Control implementation in accordance with the format of the calendar output. That is, if number 1th is Monday, a space of one unit is printed, and if number 1th is Tuesday, two spaces are printed, and so on. After the date of the Saturday is printed, the line is wrapped. The complete code to implement the example is as follows:
Import java.util.*;
/**
* Output the current month's calendar
*/
public class dateexample2{
public static void Main (string[] args) {
Get current time
Calendar C = calendar.getinstance ();
Set the delegate's date to 1th number
C.set (calendar.date,1);
Get number 1th is the day of the week
int start = C.get (Calendar.day_of_week);
Gets the maximum number of dates for the current month
int maxday = C.getactualmaximum (calendar.date);
Output title
System.out.println ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");
Space at the beginning of the output
for (int i = 1;i < start;i++) {
System.out.print ("");
}
Output all dates for the month
for (int i = 1;i <= maxday;i++) {
Output Date number
System.out.print ("" + i);
Output delimited spaces
System.out.print ("");
if (I < 10) {
System.out.print (");
}
Determine if line breaks
if (start + i-1)% 7 = = 0) {
System.out.println ();
}
}
Line break
System.out.println ();
}
}
Common functions in Java for time-date operations