To find the weekly date range based on the input

674 views Asked by At

I am having scenario with my property file input like below for weekly reports

Data lag - 10 days

Run day - Tuesday.

The requirement is based on the date lag: I have to move the calendar 10 days back. Then I want to find the Tuesday (any weekday may be specified in a parameter).

From that I want to get a weekly report.

When this config specified, say example today 11-may 2021

11 May - 10 days = 01 May 2021. After this based on the run day (for example Monday), my calendar should move to 26 April.

The final result will be

(Monday) 26 April to (Sunday) 02 May.

 val format_w = new SimpleDateFormat("yyyy-MM-dd")
    val cal_ins = Calendar.getInstance
    cal_ins.add(Calendar.DATE, -datalaginterval)
    val datlag_date = cal_ins.getTime()
    logger.info("datalag date"+datlag_date)

 cal_ins.add(Calendar.DATE, -7)
    val startdate = cal_ins.getTime
    val start_date   = format_w.format(startdate)

    println("start date-"+start_date)
    // calculate sunday last week (moves cal 6 days fwd)
    cal_ins.add(Calendar.DATE, 6)
    val enddate = cal_ins.getTime
    val end_date   = format_w.format(enddate)

    println("End date-"+end_date)

I am using the Java Calendar class.

2

There are 2 answers

0
Anonymous On BEST ANSWER

java.time

Like others I warmly recommend that you use java.time, the modern Java date and time API, for your date work. I believe that the code can be somewhat simpler than in the other answer. Excuse my Java. I trust you to translate to Scala.

    int lagDays = 10;
    DayOfWeek runDay = DayOfWeek.TUESDAY;
    
    LocalDate today = LocalDate.now(ZoneId.of("Africa/Ceuta"));
    
    LocalDate start = today.minusDays(lagDays)
                           .with(TemporalAdjusters.previousOrSame(runDay));
    LocalDate end = start.plusDays(6);
    
    System.out.format("From %s through %s%n", start, end);

When I ran the code just now, the output was:

From 2021-04-27 through 2021-05-03

If you want to use the default time zone of the JVM, use:

    LocalDate today = LocalDate.now(ZoneId.systemDefault());

How .with(TemporalAdjusters.previousOrSame(runDay)) works: If 10 days ago was already a Tuesday, that is the date you get. Otherwise you get the previous Tuesday, that is, the closest Tuesday before the calculated date 10 days ago. In this example 10 days ago was Sunday, 2 May, so we got Tuesday, 27 April.

Link

Oracle tutorial: Date Time explaining how to use java.time.

0
jwvh On

Your question is rather confusing. Maybe this will help get you started in the right direction.

import java.time.LocalDate
import java.time.DayOfWeek

val lagDate: LocalDate = LocalDate.now()          //today
                                  .minusDays(9L)  //less lag days

val nearestTues: LocalDate =  //Tuesday closest to lag date
  lagDate.minusDays(lagDate.getDayOfWeek().getValue() -
                    DayOfWeek.valueOf("TUESDAY").getValue())

val (tuesBefore
    ,tuesAfter) = if (nearestTues isAfter lagDate)
                    (nearestTues.minusDays(7L), nearestTues)
                  else
                    (nearestTues, nearestTues.plusDays(7L))