DateConversion.java [src/java/util] Revision: default  Date:
package util;

import java.time.LocalDate;
import java.time.YearMonth;
import static m.ghg.ApplicationResources.MODEL_OFFSET;

/**
 *
 * @author sidereus
 */
public class DateConversion {

  // input format 1982.17 (February 1982)
  public static LocalDate daycentLis2date(String value) {
    precondition(value);
    String[] date = value.split("\\.");
    checkDateLength(date);
    String year = date[0];
    String month = date[1];
    month = getActualMonth(month);

    int y = Integer.parseInt(year);
    int m = Integer.parseInt(month);
    if (m == 12) { // Daycent rounds December to the next year
      y--;
    }

    LocalDate d = YearMonth.of(y, m).atEndOfMonth();
    return d;
  }

  // input format 1982.187 (doy 187 of year 1982)
  public static String date2daycentLis(String value) {
    precondition(value);
    String[] date = value.split("\\.");
    checkDateLength(date);
    int year = Integer.parseInt(date[0]);
    int doy = Integer.parseInt(date[1]);

    LocalDate d = LocalDate.ofYearDay(year, doy);

    if (year < 2012) {
      year++; // add 1 year because early-ag results are reported at month 12 of each year which means year+1 in .lis file
      return (MODEL_OFFSET + year) + ".00";
    } else {
      if (d.getMonthValue() == 12) { // Round December to the next year
        year++;
      }
      return (MODEL_OFFSET + year) + "." + getDaycentMonth(d.getMonthValue());
    }
//    return Double.valueOf(daycentYear);
  }

  private static String getActualMonth(String daycentMonth) {
    if (daycentMonth.equals("08")) {
      return "1";
    } else if (daycentMonth.equals("17")) {
      return "2";
    } else if (daycentMonth.equals("25")) {
      return "3";
    } else if (daycentMonth.equals("33")) {
      return "4";
    } else if (daycentMonth.equals("42")) {
      return "5";
    } else if (daycentMonth.equals("50")) {
      return "6";
    } else if (daycentMonth.equals("58")) {
      return "7";
    } else if (daycentMonth.equals("67")) {
      return "8";
    } else if (daycentMonth.equals("75")) {
      return "9";
    } else if (daycentMonth.equals("83")) {
      return "10";
    } else if (daycentMonth.equals("92")) {
      return "11";
    } else if (daycentMonth.equals("00")) {
      return "12";
    } else {
      throw new UnsupportedOperationException("Month " + daycentMonth + " not implemented");
    }
  }

  private static String getDaycentMonth(int month) {
    switch (month) {
      case 1:
        return "08";
      case 2:
        return "17";
      case 3:
        return "25";
      case 4:
        return "33";
      case 5:
        return "42";
      case 6:
        return "50";
      case 7:
        return "58";
      case 8:
        return "67";
      case 9:
        return "75";
      case 10:
        return "83";
      case 11:
        return "92";
      case 12:
        return "00";
      default:
        throw new UnsupportedOperationException("Month " + month + " not implemented");
    }
  }

  private static void precondition(String value) {
    if (value.isEmpty()) {
      String msg = "Input value in DateConversion methods cannot be null.";
      throw new NullPointerException(msg);
    }
  }

  private static void checkDateLength(String[] data) {
    if (data.length < 2) {
      String msg = "Date must in one the following formats: yyyy.daycentMonth - yyyy.doy";
      throw new UnsupportedOperationException(msg);
    }
  }

  private static void test2(String date, Double check) {
    String d = date2daycentLis(date);

    if (!d.equals(check)) {
      String msg = d.toString() + " differ from " + check.toString();
      throw new RuntimeException(msg);
    }
  }

  private static void test(String date, LocalDate check) {
    LocalDate d = daycentLis2date(date);

    if (!d.equals(check)) {
      String msg = d.toString() + " differ from " + check.toString();
      throw new RuntimeException(msg);
    }
  }

  public static void main(String[] args) {
    String daycentdate1 = "1982.17";
    LocalDate check = LocalDate.of(1982, 2, 28);
    test(daycentdate1, check);

    String daycentdate2 = "1984.17";
    LocalDate check2 = LocalDate.of(1984, 2, 29);
    test(daycentdate2, check2);

    String daycentdate3 = "1982.365";
    Double check3 = 1983.00;
    test2(daycentdate3, check3);

    String daycentdate4 = "1984.366";
    Double check4 = 1985.00;
    test2(daycentdate4, check4);
  }

}