[Java]
/**
* Java date/time processing class
* @ Author Sking
*/
Package datetime;
Import java. util. Calendar;
Import java. util. Date;
Import java. util. GregorianCalendar;
Public class DateUtil {
/***** ----------------- Date operation ------------------******/
/**
* (1) calculate the number of days between two dates
* @ Param start date
* @ Param end date
* @ Return the number of days between two dates
*/
Public static int getIntervalDays (Date start, Date end ){
If (start. after (end) {// exchange, keep the start time before
Date t = start;
Start = end;
End = t;
}
Long s = start. getTime ();
Long e = end. getTime ();
Return (int) (e-s)/(60*60*24*1000) + 0.5 );
}
/**
* (2) obtain the Date after the specified number of days from the specified Date
* @ Param date specifies the start date
* @ Param n specifies the number of days
* @ Return specifies the date after the specified number of days from the start of the date.
*/
Public static Date afterNDays (Date date, int n ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (date );
Cal. add (Calendar. DATE, n );
Return cal. getTime ();
}
/**
* Obtain the number of days in the month of the current date.
* @ Param date: current date
* @ Return the number of days in the month of the current date
*/
Public static int DaysOfMonth (Date date ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (date );
Return cal. getActualMaximum (Calendar. DAY_OF_MONTH );
}
/**
* Get the specified week of the specified year
* @ Param year specifies the year
* @ Param week the week of the year
* @ Return indicates the Calendar string array of the specified week of the year.
* (Or Date [])
*/
// Public static Date [] getWeek (int year, int week)
Public static String [] getWeek (int year, int week ){
// Date [] result = new Date [7];
String [] result = new String [7];
GregorianCalendar cal = new GregorianCalendar ();
Cal. set (Calendar. YEAR, year );
Cal. set (Calendar. WEEK_OF_YEAR, week );
Cal. set (Calendar. DAY_OF_WEEK, cal. getFirstDayOfWeek ());
// Result [0] = cal. getTime ();
Result [0] = cal. getTime (). toString ();
For (int I = 1; I <7; I ++ ){
Cal. add (Calendar. DAY_OF_WEEK, 1 );
// Result [I] = cal. getTime ();
Result [I] = cal. getTime (). toString ();
}
Return result;
}
/**
* Get the first week of the specified year
* @ Param year specifies the year
* @ Return the Calendar String Array for the first week
*/
Public static String [] FirstWeek (int year ){
Return getWeek (year, 1 );
}
/**
* Get the last week of the specified year
* @ Param year specifies the year
* @ Return the Calendar string array of the last week
*/
Public static String [] LastWeek (int year ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. set (Calendar. YEAR, year );
Int last = cal. getActualMaximum (Calendar. WEEK_OF_YEAR );
Return getWeek (year, last );
}
/**
* Calculate the number of complete weeks in a month of a year. The full week must contain
* GetFirstDayOfWeek () to (getFirstDayOfWeek () + 7) % 7 all days
* @ Param year specifies the year
* @ Param month specifies the month
* @ Return the number of complete weeks in a month of a year
* @ Throws Exception the month is not between 1 and 12
*/
Public static int FullWeekOfMonth (int year, int month) throws Exception {
If (month <1 | month> 12)
Throw new Exception ("the month must be between 1 and 12! ");
GregorianCalendar cal = new GregorianCalendar ();
Int first = cal. getFirstDayOfWeek ();
Int weekCount = 0;
Cal. set (Calendar. YEAR, year );
Cal. set (Calendar. MONTH, month-1 );
Cal. set (Calendar. DAY_OF_MONTH, 1 );
Int endDay = cal. getMaximum (Calendar. DAY_OF_MONTH );
// Remove the incomplete Week at the beginning
While (cal. get (Calendar. DAY_OF_WEEK )! = First ){
Cal. add (Calendar. DAY_OF_MONTH, 1 );
}
Cal. add (Calendar. DAY_OF_MONTH, 7 );
While (cal. get (Calendar. DAY_OF_MONTH) <= endDay
& Cal. get (Calendar. MONTH) = month-1 ){
WeekCount ++;
Cal. add (Calendar. DAY_OF_MONTH, 7 );
}
Return weekCount;
}
/**
* Calculate the number of weeks that a month spans.
* @ Param year specifies the year
* @ Param month specifies the month
* @ Return the number of weeks in a month in a year
* @ Throws Exception the month is not between 1 and 12
*/
Public static int CrossWeekOfMonth (int year, int month) throws Exception {
If (month <1 | month> 12)
Throw new Exception ("the month must be between 1 and 12! ");
GregorianCalendar cal = new GregorianCalendar ();
Int first = cal. getFirstDayOfWeek ();
Int weekCount = 0;
Cal. set (Calendar. YEAR, year );
Cal. set (Calendar. MONTH, month-1 );
Cal. set (Calendar. DAY_OF_MONTH, 1 );
Int endDay = cal. getMaximum (Calendar. DAY_OF_MONTH );
// Remove the incomplete Week at the beginning
If (cal. get (Calendar. DAY_OF_WEEK )! = First ){
WeekCount ++;
While (cal. get (Calendar. DAY_OF_WEEK )! = First ){
Cal. add (Calendar. DAY_OF_MONTH, 1 );
}
}
Cal. add (Calendar. DAY_OF_MONTH, 7 );
While (cal. get (Calendar. DAY_OF_MONTH) <= endDay
& Cal. get (Calendar. MONTH) = month-1 ){
WeekCount ++;
Cal. add (Calendar. DAY_OF_MONTH, 7 );
}
If (cal. get (Calendar. MONTH) = month)
WeekCount ++;
Return weekCount;
}
/***** ----------------- Formatting operation ------------------******/
/*
* Common date format strings include:
* 1. yyyy-MM-dd hh: mm: ss
*/
/**
* (3) convert the specified date to XXXX, XX, and XX seconds.
* @ Param date specifies the date
* @ Return time string in the format of XX minutes and XX seconds on XX, and XX
*/
Public static String ToChineseStyle (Date date ){
GregorianCalendar cal = new GregorianCalendar ();
Cal. setTime (date );
Int year = cal. get (Calendar. YEAR );
Int month = cal. get (Calendar. MONTH) + 1;
Int day = cal. get (Calendar. DAY_OF_MONTH );
Int hour = cal. get (Calendar. HOUR_OF_DAY); // in 24-hour format
Int minute = cal. get (Calendar. MINUTE );
Int second = cal. get (Calendar. SECOND );
Return year + "year" + month + "month" + day + "day" + hour + "hour" + minute + "minute" + second + "second ";
}
Public static void main (String [] args) throws Exception {
}
}