. Calculate the maximum number of days of a month
Calendar time = calendar. getinstance ();
Time. Clear ();
Time. Set (calendar. year, year); // year is int
Time. Set (calendar. Month, I-1); // note that the Calendar Object is 0 by default
Int day = time. getactualmaximum (calendar. day_of_month); // number of days of the current month
Note: before using the set method, you must clear it first. Otherwise, many information will inherit from the current system time.
2. Conversion of calendar and date
(1) convert calendar to date
Calendar Cal = calendar. getinstance ();
Date = Cal. gettime ();
(2) convert date to calendar
Date = new date ();
Calendar Cal = calendar. getinstance ();
Cal. settime (date );
3. format the output date and time (this is usually used)
Date = new date ();
Simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String time = DF. Format (date );
System. Out. println (time );
4. Calculate the week of the year
(1) Calculate the week number of a day in a year.
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. Year, 2006 );
Cal. Set (calendar. Month, 8 );
Cal. Set (calendar. day_of_month, 3 );
Int weekno = Cal. Get (calendar. week_of_year );
(2) calculate the number of weeks in a year
Simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. Year, 2006 );
Cal. Set (calendar. week_of_year, 1 );
Cal. Set (calendar. day_of_week, calendar. Monday );
System. Out. println (DF. Format (Cal. gettime ()));
Output:
2006-01-02
5. Add () and roll () usage (not commonly used)
(1) Add () method
Simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. Year, 2006 );
Cal. Set (calendar. Month, 8 );
Cal. Set (calendar. day_of_month, 3 );
Cal. Add (calendar. Date,-4 );
Date = Cal. gettime ();
System. Out. println (DF. Format (date ));
Cal. Add (calendar. date, 4 );
Date = Cal. gettime ();
System. Out. println (DF. Format (date ));
Output:
2006-08-30
2006-09-03
(2) Roll Method
Cal. Set (calendar. Year, 2006 );
Cal. Set (calendar. Month, 8 );
Cal. Set (calendar. day_of_month, 3 );
Cal. Roll (calendar. Date,-4 );
Date = Cal. gettime ();
System. Out. println (DF. Format (date ));
Cal. Roll (calendar. date, 4 );
Date = Cal. gettime ();
System. Out. println (DF. Format (date ));
Output:
2006-09-29
2006-09-03
It can be seen that the roll () method loops over the current month, and the add () method is generally used;
6. Calculate the number of days between two arbitrary periods (this is usually used)
(1) upload to the Calendar Object
Public int getintervaldays (calendar startday, calendar endday )...{
If (startday. After (endday ))...{
Calendar Cal = startday;
Startday = endday;
Endday = Cal;
}
Long SL = startday. gettimeinmillis ();
Long El = endday. gettimeinmillis ();
Long Ei = El-Sl;
Return (INT) (Ei/(1000*60*60*24 ));
}
(2) input date object
Public int getintervaldays (date startday, date endday )...{
If (startday. After (endday ))...{
Date Cal = startday;
Startday = endday;
Endday = Cal;
}
Long SL = startday. gettime ();
Long El = endday. gettime ();
Long Ei = El-Sl;
Return (INT) (Ei/(1000*60*60*24 ));
}
(3) improved methods for accurate calculation of days separated
Public int getdaysbetween (calendar D1, calendar D2 )...{
If (d1.after (D2 ))...{
Java. util. Calendar swap = D1;
D1 = d2;
D2 = swap;
}
Int days = d2.get (calendar. day_of_year)-d1.get (calendar. day_of_year );
Int y2 = d2.get (calendar. year );
If (d1.get (calendar. Year )! = Y2 )...{
D1 = (calendar) d1.clone ();
Do ...{
Days + = d1.getactualmaximum (calendar. day_of_year); // obtain the actual number of days of the current year.
D1.add (calendar. Year, 1 );
} While (d1.get (calendar. Year )! = Y2 );
}
Return days;
}
Note: The above method can be used to derive any time, for example, to find the email received within three weeks (get the current system time-then get the time three weeks ago) it is best to compare the received time with the long time.
For example, the date 1 year ago (pay attention to millisecond conversion)
Java. util. Date mydate = new java. util. Date ();
Long mytime = (mydate. gettime ()/1000)-60*60*24*365;
Mydate. settime (mytime * 1000 );
String mdate = formatter. Format (mydate );
7. Conversion between string, date, and long (most commonly used)
Convert a string to a time type (the string can be of any type, as long as it is consistent with the format in simpledateformat)
When we take the time span, substring will issue the specific time -- long-Comparison
Java. Text. simpledateformat SDF = new java. Text. simpledateformat ("m/DD/YYYY hh: mm: ss a", java. util. locale. US );
Java. util. Date d = SDF. parse ("5/13/2003 10:31:37 am ");
Long dvalue = D. gettime ();
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String mdatetime1 = formatter. Format (d );
8. Calculate the time by Time
Year, month, week, and date
Simpledateformat formatter2 = new simpledateformat ("yyyy-mm f e ");
Java. util. Date date2 = formatter2.parse ("Friday 2003-05 ");
Simpledateformat formatter3 = new simpledateformat ("yyyy-mm-dd ");
String mydate2 = formatter3.format (date2 );
The day of the week.
Mydate = myformatter. parse ("2001-1-1 ");
Simpledateformat formatter4 = new simpledateformat ("e ");
String mydate3 = formatter4.format (mydate );
9. Java and specific database integration
In developing Web applications, for different database date types, we need to convert the date types in our programs. If the corresponding database data is of the Oracle date type, that is, only the year, month, and day are required, you can select Java. SQL. date type. if it corresponds to the datetime type of the MSSQLServer database, that is, year, month, day, hour, minute, and second, select Java. SQL. timestamp type
You can use dateformat to define the time and date format and convert it to a string.
Class datetest {
* Method converts a string-type date to a timestamp (timestamp java. SQL. timestamp)
* @ Param datestring the string to be converted to Timestamp
* @ Return datatime Timestamp
Public final static java. SQL. timestamp string2time (string datestring)
Throws java. Text. parseexception {
Dateformat;
Dateformat = new simpledateformat ("yyyy-mm-dd KK: mm: Ss. Sss", locale. English); // set the format
// Dateformat = new simpledateformat ("yyyy-mm-dd KK: mm: SS", locale. English );
Dateformat. setlenient (false );
Java. util. Date TimeDate = dateformat. parse (datestring); // util type
Java. SQL. timestamp datetime = new java. SQL. timestamp (TimeDate. gettime (); // type of Timestamp, TimeDate. gettime () returns a long type
Return datetime;
}
* Method converts a string-type date to a date (Java. SQL. Date)
* @ Param datestring the string to be converted to date
* @ Return datatime date
Public final static java. SQL. Date string2date (string datestring)
Throws java. Lang. Exception {
Dateformat;
Dateformat = new simpledateformat ("yyyy-mm-dd", locale. English );
Dateformat. setlenient (false );
Java. util. Date TimeDate = dateformat. parse (datestring); // util type
Java. SQL. Date datetime = new java. SQL. Date (TimeDate. gettime (); // SQL type
Return datetime;
}
Public static void main (string [] ARGs ){
Date da = new date ();
Note: Da. gettime () obtains a long value.
System. Out. println (DA. gettime ());
Convert from date to Timestamp
Method 1: Use new timestamp (long)
Timestamp T = new timestamp (new date (). gettime ());
System. Out. println (t );
Method 2: Use timestamp (INT year, int month, int date, int hour, int minute, int second, int nano)
Timestamp TT = new timestamp (calendar. getinstance (). Get (
Calendar. Year)-1900, calendar. getinstance (). Get (
Calendar. month), calendar. getinstance (). Get (
Calendar. Date), calendar. getinstance (). Get (
Calendar. Hour), calendar. getinstance (). Get (
Calendar. Minute), calendar. getinstance (). Get (
Calendar. Second), 0 );
System. Out. println (TT );
Try {
String stodate = "2005-8-18"; // string used for conversion to Java. SQL. Date
String stotimestamp = "14:21:12. 123"; // string used for conversion to Java. SQL. Timestamp
Date date1 = string2date (stodate );
Timestamp date2 = string2time (stotimestamp );
System. Out. println ("Date:" + date1.tostring (); // The result is displayed.
System. Out. println ("timestamp:" + date2.tostring (); // The result is displayed.
} Catch (exception e ){
E. printstacktrace ();
}
}
}