package ru.micord.ervu.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import org.springframework.util.StringUtils;

/**
 * @author gulnaz
 */
public final class DateUtil {

  public static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");

  private DateUtil() {}

  public static LocalDate convertToLocalDate(String date) {
    return StringUtils.hasText(date)
           ? LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE)
           : null;
  }

  public static String convertToString(LocalDate date) {
    return date == null ? "" : date.format(DEFAULT_FORMATTER);
  }

  public static String convertToString(LocalDate date, DateTimeFormatter formatter) {
    return date == null ? "" : date.format(formatter);
  }

  public static int getDaysTill(LocalDate end) {
    return (int) ChronoUnit.DAYS.between(
        LocalDate.now(), end == null ? LocalDate.now() : end);
  }
}
