Pacific-Design.com

    
Home Index

1. Scala

2. Date Computation

Scala / Date Computation /

Scala Compute Dates

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.{Date}
import java.text.{SimpleDateFormat};


object App {

  def compute3Years(beginDate: String): String = {

    val dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    val date = new Date();
    val currentDate = dateFormat.format(date)
    //DEBUG println("currentDate=" + currentDate)

    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    val oldDate = LocalDate.parse(beginDate, formatter)
    val newDate = LocalDate.parse(currentDate, formatter)


    val period = newDate.toEpochDay() - oldDate.toEpochDay()
    //DEBUG println("period=" + period)

    if(period > (365*3)) {
      "up3year"
    } else {
      "over3years"
    }
  }

  def main(args: Array[String]) {

    println(compute3Years("2015-02-25"))
    println("-------------------------")
    println(compute3Years("2010-07-18"))

  }

}

/*
Output

over3years
-------------------------
up3year

*/