Home > Mobile >  First and Last Date of Current and Previous Quarter For a Given Date in Scala
First and Last Date of Current and Previous Quarter For a Given Date in Scala

Time:01-27

I'm writing a code for automatic execution once a month, which will take the date from the computer as the start and end date for execution of a while loop.

 val today = new DateTime().withZone(DateTimeZone.forID("Asia/Kolkata"))
 var start =  today.toString(DateTimeFormat.forPattern("yyyy-MM-dd"))
 val end = today.toString(DateTimeFormat.forPattern("yyyy-MM-dd"))

 while(start<=end){
    Data(start,end)
 }

def Data (start: DateTime, end: DateTime) {

    val start_temp = start
    val end_temp = end
    var start_temp_1 = start_temp.minusMonths(1).withDayOfMonth(1);
    var start_date_monthly = start_temp_1.toString(DateTimeFormat.forPattern("yyyy-MM-dd"))
    println("Last Month Start Date: "   start_date_monthly)
    var end_temp_1 = end_temp.minusMonths(0).withDayOfMonth(1);
    var end_temp_2 = end_temp_1.minusDays(1)
    var end_date_monthly = end_temp_2.toString(DateTimeFormat.forPattern("yyyy-MM-dd"))
    println("Last Month End Date: "   end_date_monthly)
    
}

The date in the variable today needs to be converted to the first and last date of

  1. Previous month from today
  2. Current and previous quarter from today

I was able to do the first one as displayed in the Data function.

Suppose today gets the following date - 2022-01-27T14:25:26.374 05:30 The above function returns

Last Month Start Date: 2021-12-01
Last Month End Date: 2021-12-31

How can I achieve this for the second one - first and last dates of current and previous quarter?

Ex - today gets the following date - 2022-01-27T14:25:26.374 05:30 I need to return 2021-10-01 and 2021-12-31 for the previous quarter And 2022-01-01 and 2022-03-31 for the current quarter.

Certain solutions suggest using Spark SQL & Dataframes but that isn't applicable in this situation.

Is there a direct way do this as done in the month case? Or is a udf the only option here?

CodePudding user response:

The following may do it. This is probably a UDF if it has to be applied to every row in the DF

import java.time.temporal.IsoFields
import java.time.temporal.IsoFields.QUARTER_OF_YEAR
import java.time.{LocalDate, YearMonth}

def printQuarterBeginAndEnd(localDate: LocalDate): Unit = {
  val currentQuarter = localDate.get(QUARTER_OF_YEAR)
  val currentYear = localDate.getYear
  val currentMonth = localDate.getMonth
  val startOfQuarter = YearMonth.of(currentYear, currentMonth).`with`(QUARTER_OF_YEAR, currentQuarter).atDay(1)
  val endOfQuarter = YearMonth.of(currentYear, currentQuarter * 3).`with`(QUARTER_OF_YEAR, currentQuarter).atEndOfMonth()
  println(s"Start $startOfQuarter ends $endOfQuarter")
}

printQuarterBeginAndEnd(LocalDate.now())
printQuarterBeginAndEnd(LocalDate.now().minus(1, IsoFields.QUARTER_YEARS))

Prints the following

import java.time.temporal.IsoFields
import java.time.temporal.IsoFields.QUARTER_OF_YEAR
import java.time.{LocalDate, YearMonth}

printQuarterBeginAndEnd: (localDate: java.time.LocalDate)Unit



Start 2022-01-01 ends 2022-03-31
Start 2021-10-01 ends 2021-12-31
  •  Tags:  
  • Related