Obtain the specified date

Source: Internet
Author: User
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 );

If (v_intday <10 ){
V_strdate = v_strdate + "0" + v_intday + "-";
} Else {
V_strdate = v_strdate + v_intday + "-";
}
If (v_intmonth <10 ){
V_strdate = v_strdate + "0" + v_intmonth + "-";
} Else {
V_strdate = v_strdate + v_intmonth + "-";
}
V_strdate = v_strdate + v_intyear + "";

If (v_inthour <10 ){
V_strdate = v_strdate + "0" + v_inthour + ":";
} Else {
V_strdate = v_strdate + v_inthour + ":";
}
If (v_intminute <10 ){
V_strdate = v_strdate + "0" + v_intminute + ":";
} Else {
V_strdate = v_strdate + v_intminute + ":";
}
If (v_intsecond <10 ){
V_strdate = v_strdate + "0" + v_intsecond;
} Else {
V_strdate = v_strdate + v_intsecond;
}
Cal = NULL;
Return v_strdate;
}

// 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 );
}

Public static void main (string [] ARGs ){
System. Out. println (getdate ());
System. Out. println (getthismondaydate ());
System. Out. println (getcurrentmonthbegindate ());
System. Out. println (getlastmondaydate ());
System. Out. println (getlastsundaydate ());
System. Out. println (getlastmonthbegindate ());
System. Out. println (getlastmonthenddate ());
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.