Take this Monday and the previous Monday method case

Source: Internet
Author: User
Tags what date
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)






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.