Java implements the input of a time period and the corresponding number of weeks to obtain the date of these weeks in this time period

Source: Internet
Author: User

Java implements the input of a time period and the corresponding number of weeks to obtain the date of these weeks in this time period

Recently, I have encountered a problem. For a given period of time and a specific week, I need to obtain all the days of the given week in this period of time.

For example, get the dates of all Monday and Tuesday from.

Package com. cc. common;


Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util .*;




Public class WeekDayUtil {


/**
* Enter a date period and the corresponding number of weeks to obtain the date of these weeks.
*/
Private static Map WeekNumberMap = new HashMap ();
Static {
WeekNumberMap. put (0, 1 );
WeekNumberMap. put (1, 2 );
WeekNumberMap. put (2, 3 );
WeekNumberMap. put (3, 4 );
WeekNumberMap. put (4, 5 );
WeekNumberMap. put (5, 6 );
WeekNumberMap. put (6, 7 );


}


Public static List GetDates (String dateFrom, String dateEnd, List WeekDays ){
Long time;
Long perDayMilSec = 24L * 60*60*1000;
List DateList = new ArrayList ();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
// Week coefficient to be queried
String strWeekNumber = weekForNum (weekDays );
Try {
DateFrom = sdf. format (sdf. parse (dateFrom). getTime ()-perDayMilSec );
While (true ){
Time = sdf. parse (dateFrom). getTime ();
Time = time + perDayMilSec;
Date date = new Date (time );
DateFrom = sdf. format (date );
If (dateFrom. compareTo (dateEnd) <= 0 ){
// Week coefficient of a certain time point of query
Integer weekDay = dayForWeek (date );
// Determine whether the week coefficient of the current date needs to be queried
If (strWeekNumber. contains (weekDay. toString ())){
DateList. add (dateFrom );
}
} Else {
Break;
}
}
} Catch (ParseException e ){
E. printStackTrace ();
}
Return dateList;
}


// Wait until the week coefficient of the current time. Sunday: 1, Monday: 2, Tuesday: 3, Wednesday: 4, Thursday: 5, Friday: 6, Saturday: 7
Public static Integer dayForWeek (Date date ){
Calendar calendar = Calendar. getInstance ();
Calendar. setTime (date );
Return calendar. get (Calendar. DAY_OF_WEEK );
}


/**
* Obtain the ratio of the week.
*/
Public static String weekForNum (List WeekDays ){
// The returned result is the weekly coefficient of the combination.
String weekNumber = "";


For (Integer weekDay: weekDays ){
WeekNumber = weekNumber + "" + getWeekNum (weekDay). toString ();
}
Return weekNumber;


}


// Convert the week to the corresponding coefficient 0, Sunday; 1, Monday; 2 ....
Public static Integer getWeekNum (int strWeek ){
Return weekNumberMap. get (strWeek );
}


Public static void main (String [] args ){
// Output the dates of all Monday and Tuesday from.
List DaysOfOneWeek = new ArrayList ();
DaysOfOneWeek. add (1 );
DaysOfOneWeek. add (2 );
List DaysNeedBookList = getDates ("2015-01-01", "2015-01-21", daysOfOneWeek );
For (String s: daysNeedBookList ){
System. out. println (s );
}
}
}

 

After the main method is tested, the output is:


2015-01-05
2015-01-06
2015-01-12
2015-01-13
2015-01-19
Septem

 

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.