[Java] Get the dates of the past Monday and Sunday in order

Thing you want to do

If the acquisition target is the past 3 months This Week: Get the dates of the Monday and Sunday of the week that contains the current date Past Weeks: Get dates for each Monday and Sunday Last week: Get dates for Mondays and Sundays of the week containing dates 3 months ago

Source

I received an indication from swordone and corrected the logic (6/25). * See comments

public static void main(String[] args) {

        //Get current date
        LocalDate now = LocalDate.now();

        //Get the date 3 months ago (change this date as appropriate, 3 months in this example)
        LocalDate targetDate = now.minusMonths(3);

        //Get the Monday of the current week
        LocalDate monday = now.with(DayOfWeek.MONDAY);

        //Get the Sunday of the current week
        LocalDate sunday = now.with(DayOfWeek.SUNDAY);

        int i = 0;
        while (!sunday.isBefore(targetDate)) {
            System.out.println(monday + " ~ " + sunday + " : " + (i == 0 ? "this week" : i + "Week ago"));
            monday = monday.minusDays(7);
            sunday = sunday.minusDays(7);
            i++;
        }
    }

Execution result

The current date is 6/25. The acquisition target is up to 3 months in advance. I think it can be used when you want to specify the period for the past few weeks. Change the date to be acquired as appropriate and use it.

2019-06-24 ~ 2019-06-30 :this week
2019-06-17 ~ 2019-06-23 :1 week ago
2019-06-10 ~ 2019-06-16 :2 weeks ago
2019-06-03 ~ 2019-06-09 :3 weeks ago
2019-05-27 ~ 2019-06-02 :4 weeks ago
2019-05-20 ~ 2019-05-26 :5 weeks ago
2019-05-13 ~ 2019-05-19 :6 weeks ago
2019-05-06 ~ 2019-05-12 :7 weeks ago
2019-04-29 ~ 2019-05-05 :8 weeks ago
2019-04-22 ~ 2019-04-28 :9 weeks ago
2019-04-15 ~ 2019-04-21 :10 weeks ago
2019-04-08 ~ 2019-04-14 :11 weeks ago
2019-04-01 ~ 2019-04-07 :12 weeks ago
2019-03-25 ~ 2019-03-31 :13 weeks ago

Recommended Posts

[Java] Get the dates of the past Monday and Sunday in order
Get the result of POST in Java
Get the URL of the HTTP redirect destination in Java
[Java] Get the file in the jar regardless of the environment
Order of processing in the program
Spring validation was important in the order of Form and BindingResult
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
[Java] Get the day of the specific day of the week
How to get the date in java
The story of writing Java in Emacs
[Java8] Search the directory and get the file
Discrimination of Enums in Java 7 and above
Specify the order in which configuration files and classes are loaded in Java
The story of forgetting to close a file in Java and failing
This and that of the implementation of date judgment within the period in Java
How to get the absolute path of a directory running in Java
Find the maximum and minimum of the five numbers you entered in Java
How to create your own annotation in Java and get the value
[Order method] Set the order of data in Rails
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the description order of Java system properties
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Basics of threads and Callable in Java [Beginner]
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java
[Rails] Ranking and pagination in order of likes
I received the data of the journey (diary application) in Java and visualized it # 001
Sample code to get the values of major SQL types in Java + MySQL 8.0
Write ABNF in Java and pass the email address
[Android development] Get an image from the server in Java and set it in ImageView! !!
[Java] Get miscellaneous dates
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
Get the name of the test case in the JUnit test class
[Java] How to get the key and value stored in Map by iterative processing
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] How to get the maximum value of HashMap
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Source used to get the redirect source URL in Java
[Java] Get the file path in the folder with List
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
Change the storage quality of JPEG images in Java
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
Use of Abstract Class and Interface properly in Java
Get the URL of the HTTP redirect destination in Ruby
Get location information in Rails and sort in ascending order