How to get current week date range and previous week date range using JodaTime .

Here is Joda time Maven dependency.
<dependency>
                                                <groupId>com.fasterxml.jackson.datatype</groupId>
                                                <artifactId>jackson-datatype-joda</artifactId>
                                                <version>2.8.1</version>
                                </dependency>

Below program you can find out current and previous week date range.

package swain.swain;

import java.util.Date;

import org.joda.time.LocalDate;

public class DataUtill {

    public static void main(String[] args) {
         int currentWeekDay = LocalDate.now().getDayOfWeek();
         int startOfWeekInterval = (currentWeekDay - 1) * -1;
         int endOfWeekInterval = 6 - currentWeekDay;
         if (endOfWeekInterval < 0) {
             endOfWeekInterval = 0;
         }
         Date fromDate = LocalDate.now().plusDays(startOfWeekInterval).toDate();
         Date thruDate = LocalDate.now().plusDays(endOfWeekInterval).toDate();

         System.out.println("Current Week From date:" + fromDate);
         System.out.println("Current Week To date:" + thruDate);

         Date preFromDate = LocalDate.now().minusWeeks(1).withDayOfWeek(1).toDate();
         Date preToDate = LocalDate.now().minusWeeks(1).withDayOfWeek(1).plusDays(6).toDate();

         System.out.println("Previous Week From date:" + preFromDate);
         System.out.println("Previuos Week To date:" + preToDate);

    }
}


OutPut.

Current Week From date:Mon Jan 09 00:00:00 IST 2017
Current Week To date:Sat Jan 14 00:00:00 IST 2017
Previous Week From date:Mon Jan 02 00:00:00 IST 2017

Previuos Week To date:Sun Jan 08 00:00:00 IST 2017

Popular posts from this blog

How to show browser info(name,version) and OS name using java in client side.

Autocomplete Textbox in JSP using jQuery with odbc connection