Java uses the calendar class to get the specified date
The acquisition of a specified date is based on the number of days when the specified date differs from the current date, and then sets the value of calendar. day_of_month using the set method. Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. day_of_month, Cal. Get (calendar. day_of_month)-dayplus );
(Dayplus indicates the number of days when the specified date differs from the current date)
If no set is performed, the system time is obtained by default. If no problem is found in the preliminary test, the specified date can be obtained correctly.
**************************************** **************************************** *****************
Code:
// Obtain the current date
Public static string getdate (){
Calendar Cal = calendar. getinstance ();
Return getdate (CAL );
}
// Obtain the date
Private Static string getdate (calendar Cal ){
String v_strdate = "";
Int v_intyear = Cal. Get (calendar. year );
Int v_intmonth = Cal. Get (calendar. month) + 1;
Int v_intday = Cal. Get (calendar. day_of_month );
Int v_inthour = Cal. Get (calendar. hour_of_day );
Int v_intminute = Cal. Get (calendar. Minute );
Int v_intsecond = Cal. Get (calendar. Second );
// Obtain the number of days for which the current date differs from the current Sunday
Private Static int getmondayplus (){
Calendar Cd = calendar. getinstance ();
// Obtain the day of the week, Sunday, and Tuesday ......
Int dayofweek = CD. Get (calendar. day_of_week)-1; // because the first day of Sunday in China is used as the first day, 1 is subtracted here.
If (dayofweek = 1 ){
Return 0;
} Else {
Return dayofweek-1;
}
}
// Obtain the date of this Monday
Public static string getthismondaydate (){
Calendar Cal = calendar. getinstance ();
Int mondayplus = getmondayplus ();
Cal. Set (calendar. day_of_month, Cal. Get (calendar. day_of_month)-mondayplus );
Cal. Set (calendar. hour_of_day, 0 );
Cal. Set (calendar. Minute, 0 );
Cal. Set (calendar. Second, 0 );
Return getdate (CAL );
}
// Obtain the date of January 1, 1st day of this month
Public static string getcurrentmonthbegindate (){
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. day_of_month, 1 );
Cal. Set (calendar. hour_of_day, 0 );
Cal. Set (calendar. Minute, 0 );
Cal. Set (calendar. Second, 0 );
Return getdate (CAL );
}
// Obtain the date of last Monday
Public static string getlastmondaydate (){
Calendar Cal = calendar. getinstance ();
Int dayplus = getmondayplus () + 7;
Cal. Set (calendar. day_of_month, Cal. Get (calendar. day_of_month)-dayplus );
Cal. Set (calendar. hour_of_day, 0 );
Cal. Set (calendar. Minute, 0 );
Cal. Set (calendar. Second, 0 );
Return getdate (CAL );
}
// Obtain the date of last Sunday
Public static string getlastsundaydate (){
Calendar Cal = calendar. getinstance ();
Int dayplus = getmondayplus () + 1;
Cal. Set (calendar. day_of_month, Cal. Get (calendar. day_of_month)-dayplus );
Cal. Set (calendar. hour_of_day, 23 );
Cal. Set (calendar. Minute, 59 );
Cal. Set (calendar. Second, 59 );
Return getdate (CAL );
}
// Obtain the date of January 1, 1st day of last month
Public static string getlastmonthbegindate (){
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. Month, Cal. Get (calendar. month)-1 );
Cal. Set (calendar. day_of_month, 1 );
Cal. Set (calendar. hour_of_day, 0 );
Cal. Set (calendar. Minute, 0 );
Cal. Set (calendar. Second, 0 );
Return getdate (CAL );
}
// Obtain the date of the last day of the last month
Public static string getlastmonthenddate (){
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. day_of_month, 0 );
Cal. Set (calendar. hour_of_day, 23 );
Cal. Set (calendar. Minute, 59 );
Cal. Set (calendar. Second, 59 );
Return getdate (CAL );
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.