All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickzetta.platform.util.DateUtil Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.clickzetta.platform.util;


import java.sql.Date;
import java.time.LocalDate;

public class DateUtil {
  public static final int MIN_DATE_VALUE =
      (int)LocalDate.parse("0001-01-01").toEpochDay();
  public static final int MAX_DATE_VALUE =
      (int)LocalDate.parse("9999-12-31").toEpochDay();

  private DateUtil() {
  }


  public static void checkDateWithinRange(long days) {
    if (days < MIN_DATE_VALUE || days > MAX_DATE_VALUE) {
      throw new IllegalArgumentException(
          "Date value <" + days + ">} is out of range '0001-01-01':'9999-12-31'");
    }
  }


  public static int sqlDateToEpochDays(Date date) {
    long days = date.toLocalDate().toEpochDay();
    checkDateWithinRange(days);
    return (int)days;
  }


  public static Date epochDaysToSqlDate(int days) {
    checkDateWithinRange(days);
    return Date.valueOf(LocalDate.ofEpochDay(days));
  }

  public static String epochDaysToDateString(int days) {
    return LocalDate.ofEpochDay(days).toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy