Last Monday
String Getlastmondayyyyymmddhhmmss () {
MonDay =-6;
SimpleDateFormat ("Yyyy-mm-dd 00:00:00"); Set the time format
Calendar resulttime = Calendar.getinstance ();
DayWeek = Resulttime.get (Calendar. Day_of_week);//Gets the current date is the day ordinal of the week
Resulttime.add (Calendar. DATE, DayWeek = = 1? -(7-monday):-(DayWeek-2) + (monDay-1));//According to the rules of the calendar, subtract the difference between the day of the week and the first of the one weeks from the current date
String resultday = Firstsdf.format ( Resulttime.gettime ());
resultday;
}
This week a
String Getweekofmondayyyyymmddhhmmss () {
monDay = 1;
SimpleDateFormat ("Yyyy-mm-dd 00:00:00"); Set the time format
Calendar resulttime = Calendar.getinstance ();
DayWeek = Resulttime.get (Calendar. Day_of_week);//Gets the current date is the day ordinal of the week
Resulttime.add (Calendar. DATE, DayWeek = = 1? -(7-monday):-(DayWeek-2) + (monDay-1));//According to the rules of the calendar, subtract the difference between the day of the week and the first of the one weeks from the current date
String resultday = Firstsdf.format ( Resulttime.gettime ());
resultday;
}
This week a code explains:
MonDay = 1;//Target Week
Calendar resulttime = Calendar.getinstance ();//Get Calendar Object
DayWeek = Resulttime.get (Calendar. Day_of_week);//Gets the current date is the day ordinal of the week
Resulttime.add (Calendar. DATE, DayWeek = = 1? -(7-monday):-(DayWeek-2) + (monDay-1));//calculation of the current date
The point is the interpretation of this trinocular operator DayWeek = = 1? -(7-monday):-(DayWeek-2) + (monDay-1)
DayWeek equals 1, that is, the day is the first day of the week, that is, the same day in Sunday,
So first calculate the difference between 7 and the target Day (7-monday), then take the day date minus this difference is the date to get
If DayWeek is not equal to 1, which means that the day is Monday to Saturday, then the strategy has changed
Figure out what date Monday is. The current date minus the difference from Monday-(DayWeek-2 (Monday is the 2nd day))
Then add the difference between target week and Monday (monDay-1)