Directly on the code
Copy Code code as follows:
Import Java.util.Calendar;
Import Java.util.Date;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
/**
* Date Tool Class
* @author WXQ
*
*/
public class Dateutils {
private static Final log = Logfactory.getlog (Dateutils.class);
/**
* How many weeks to get the current date
*
* @param date
* @return
*/
public static int Getweekofyear (date date) {
Calendar C = calendar.getinstance ();
C.setfirstdayofweek (Calendar.monday);
/** sets the minimum number of days required for the first one week of the year, for example, if the first week of the definition contains the first day of the year, the method is called using a value of 1.
* If the minimum number of days must be an entire week, call this method with a value of 7. **/
C.setminimaldaysinfirstweek (1);
C.settime (date);
Return C.get (calendar.week_of_year);
}
/**
* Get the total number of weeks of a year
*
* @param year
* @return
*/
public static int getmaxweeknumofyear (int year) {
Calendar C = calendar.getinstance ();
C.set (year, Calendar.december, 31, 23, 59, 59);
Return Getweekofyear (C.gettime ());
}
/**
* Get the first day of the week of a certain year
*
* @param year
* @param Week
* @return
*/
public static Date Getfirstdayofweek (int-year, int week) {
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.week_of_year, WEEK);
C.set (Calendar.day_of_week, calendar.monday);/Set Monday
C.setfirstdayofweek (Calendar.monday);
return C.gettime ();
}
/**
* Get the last day of a certain year
*
* @param year
* @param Week
* @return
*/
public static Date Getlastdayofweek (int-year, int week) {
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.week_of_year, WEEK);
C.setfirstdayofweek (Calendar.monday);
C.set (Calendar.day_of_week, C.getfirstdayofweek () + 6); Sunday
return C.gettime ();
}
/**
* Get the first day of a certain month
*
* @param year
* @param month
* @return
*/
public static Date getfirstdayofmonth (int-year, int month) {
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.month, month-1);
C.set (Calendar.day_of_month, C.getactualminimum (Calendar.day_of_month));
return C.gettime ();
}
/**
* Get the last day of a certain year
*
* @param year
* @param month
* @return
*/
public static Date getlastdayofmonth (int-year, int month) {
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.month, month-1);
C.set (Calendar.day_of_month, C.getactualmaximum (Calendar.day_of_month));
return C.gettime ();
}
/**
* Get the first day of a quarter of a year
*
* @param year
* @param Quarter
* @return
*/
public static Date getfirstdayofquarter (int-year, int quarter) {
int month = 0;
if (Quarter > 4) {
return null;
} else {
month = (quarter-1) * 3 + 1;
}
Return Getfirstdayofmonth (year, month);
}
/**
* Get the last day of a quarter of a year
*
* @param year
* @param Quarter
* @return
*/
public static Date getlastdayofquarter (int-year, int quarter) {
int month = 0;
if (Quarter > 4) {
return null;
} else {
Month = quarter * 3;
}
Return Getlastdayofmonth (year, month);
}
/**
* Get the first day of a year
*
* @param year
* @return
*/
public static Date getfirstdayofyear (int year) {
Return Getfirstdayofquarter (year,1);
}
/**
* Get the last day of a year
*
* @param year
* @return
*/
public static Date getlastdayofyear (int year) {
Return Getlastdayofquarter (year,4);
}
public static void Main (string[] args) {
Log.info (Getfirstdayofweek (2013,1));
Log.info (Getlastdayofweek (2013,1));
Log.info (Getfirstdayofmonth (2013,2));
Log.info (Getlastdayofmonth (2013,2));
Log.info (Getfirstdayofquarter (2013,2));
Log.info (Getlastdayofquarter (2013,2));
Log.info (Getfirstdayofyear (2013));
Log.info (Getlastdayofyear (2013));
}
}