Java operations on the day of the week

Source: Internet
Author: User
Tags date1

In the most recent OA, I need to determine whether two dates are the same week and get the dates of Monday and Friday of the week based on a given date.

After completing the preceding tasks, I found that the calendar is indeed a powerful class.

The source code is provided below for the reference of the brothers.

/*
* Creation date: 2005-3-30

Package com. infoearth;

Import java. SQL. timestamp;
Import java. Text .*;
Import java. util .*;
/**
* @ Li chunlei
*
Public class manageweek {
// Determine whether two dates are in the same week
Boolean issameweekdates (date date1, date date2 ){
Calendar cal1 = calendar. getinstance ();
Calendar cal2 = calendar. getinstance ();
Cal1.settime (date1 );
Cal2.settime (date2 );
Int subyear = cal1.get (calendar. Year)-cal2.get (calendar. year );
If (0 = subyear ){
If (cal1.get (calendar. week_of_year) = cal2.get (calendar. week_of_year ))
Return true;
}
Else if (1 = subyear & 11 = cal2.get (calendar. month )){
// If the last week of March spans the first week of the next year, the last week is counted as the first week of the next year.
If (cal1.get (calendar. week_of_year) = cal2.get (calendar. week_of_year ))
Return true;
}
Else if (-1 = subyear & 11 = cal1.get (calendar. month )){
If (cal1.get (calendar. week_of_year) = cal2.get (calendar. week_of_year ))
Return true;
}
Return false;
}
 
 
// Generate the weekly Sequence
Public static string getseqweek (){
Calendar c = calendar. getinstance (locale. China );
String week = integer. tostring (C. Get (calendar. week_of_year ));
If (Week. Length () = 1) Week = "0" + Week;
String year = integer. tostring (C. Get (calendar. Year ));
Return year + Week;

}

// Obtain the Monday date
Public static string getmonday (date ){
Calendar c = calendar. getinstance ();
C. settime (date );
C. Set (calendar. day_of_week, calendar. Monday );
Return new simpledateformat ("yyyy-mm-dd"). Format (C. gettime ());
}


// Obtain the Friday date
Public static string getfriday (date ){
Calendar c = calendar. getinstance ();
C. settime (date );
C. Set (calendar. day_of_week, calendar. Friday );
Return new simpledateformat ("yyyy-mm-dd"). Format (C. gettime ());
}

// The day before or after the current date

Public static string afternday (int n ){
Calendar c = calendar. getinstance ();
Dateformat df = new simpledateformat ("yyyy-mm-dd ");
C. settime (new date ());
C. Add (calendar. Date, N );
Date D2 = C. gettime ();
String S = DF. Format (D2 );
Return S;
}

Public static void main (string [] ARGs ){

}

}

I often encounter the problem of converting a string into a date. In fact, there are three sentences:

String mystring = "2005/12/20 ";

Simpledateformat SDF = new simpledateformat ("yyyy/mm/DD", locale. China );
Date d = SDF. parse (mystring );

The following is an example.

Package formatdatetime;

Import java. Text .*;
Import java. util .*;

/**
* @ Author Administrator
*
* To change the template of the type comment generated by todo, go
* Window-preferences-Java-code style-Code Template
*/
Public class stringtodatetimetest {

Public static void main (string [] ARGs ){

String mystring = "Tue Oct 18 04:11:56 CST 2005 ";
// String mystring = "2005/12/20 ";
Try {
Simpledateformat SDF = new simpledateformat ("eee Mmm dd hh: mm: ss zzz yyyy", locale. US );
// Simpledateformat SDF = new simpledateformat ("yyyy/mm/DD", locale. China );
Date d = SDF. parse (mystring );
Calendar c = calendar. getinstance ();
C. settime (d );
String S = new simpledateformat ("yyyy-mm-dd"). Format (C. gettime ());
System. Out. println (s );

} Catch (exception e ){
E. printstacktrace ();
}
}
}

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.