Recently, I had a problem with the completion of the establishment. For a given time period and a specific week, I need to get the date of all the given weeks in this time period.
For example, get all the Monday and Tuesday dates from 2015-01-01 to 2015-01-21.
Package Com.cc.common;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.*;
public class Weekdayutil {
/**
* Enter a time period for a date, and the corresponding day of the week to get the dates for these weeks
*/
private static Map<integer, integer> Weeknumbermap = new Hashmap<integer, integer> ();
static {
Weeknumbermap.put (0,1);
(Weeknumbermap.put);
Weeknumbermap.put (2,3);
Weeknumbermap.put (3,4);
Weeknumbermap.put (4,5);
Weeknumbermap.put (5,6);
Weeknumbermap.put (6,7);
}
public static list<string> GetDates (String datefrom, String dateend, list<integer> weekdays) {
Long time;
Long perdaymilsec = 24L * 60 * 60 * 1000;
list<string> datelist = new arraylist<string> ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Week coefficients that need 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) {
Query the week factor for a certain time
Integer WeekDay = Dayforweek (date);
Determine if the week factor for the current date is the one that needs to be queried
if (Strweeknumber.contains (weekday.tostring ())) {
Datelist.add (Datefrom);
}
} else {
Break
}
}
} catch (ParseException e) {
E.printstacktrace ();
}
return datelist;
}
Wait until the time of the period of the week factor. 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);
}
/**
* Get the corresponding week factor 0:1, week
*/
public static String Weekfornum (list<integer> weekdays) {
Return the result as a combined week factor
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) {
Outputs all Monday and Tuesday dates from 2015-01-01 to 2015-01-21.
list<integer> Daysofoneweek = new arraylist<integer> ();
Daysofoneweek.add (1);
Daysofoneweek.add (2);
list<string> 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:
2015-01-05
2015-01-06
2015-01-12
2015-01-13
2015-01-19
2015-01-20
The Java implementation enters a time period, and the corresponding number of weeks, to get the dates for these weeks in this time period