MCPDDate.java

/*
 * Copyright 2020 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.community.CommunityCodeValues;

/**
 * MCPD date "YYYYMMDD" utilities.
 * 
 * @author Matija Obreza
 */
@Slf4j
public abstract class MCPDDate {

	public static final Pattern MCPDDATE_PATTERN = Pattern.compile("^((?:[1-9]\\d{3})|(?:----)|(?:0000))(--|(?:0[0-9])|(?:1[0-2]))(--|(?:0[0-9])|(?:[1-2][0-9])|(?:3[0-1]))$");

	public static interface IDateRecipient {
		void accept(Date date, String format);
	}

	public static interface IDateValue {
		void accept(Date date);
	}

	public static interface IDateFormat {
		void accept(String format);
	}

	public static void convert(String dateInYYYYMMDD, IDateValue dateValue, IDateFormat dateFormat) {
		Date parsedDate = null;
		String parsedDateFormat = null;

		if (StringUtils.isNotBlank(dateInYYYYMMDD)) {
			dateInYYYYMMDD = dateInYYYYMMDD.strip();

			// Process
			Matcher acqDate = MCPDDATE_PATTERN.matcher(dateInYYYYMMDD);
			if (acqDate.matches()) {
				String year = acqDate.group(1);
				String month = acqDate.group(2);
				String day = acqDate.group(3);
				if (StringUtils.isNotBlank(year) && !"----".equals(year)) {
					Calendar cal = Calendar.getInstance();
					cal.clear();
					cal.set(Calendar.MONTH, 0);
					cal.set(Calendar.DATE, 1);
					cal.set(Calendar.YEAR, Integer.parseInt(year));
					String actualDateFormat = null;
					if (StringUtils.isNotBlank(month) && !"--".equals(month) && !"00".equals(month)) {
						cal.set(Calendar.MONTH, Integer.parseInt(month) - 1); // months are 0-based
						if (StringUtils.isNotBlank(day) && !"--".equals(day) && !"00".equals(day)) {
							cal.set(Calendar.DATE, Integer.parseInt(day));
							actualDateFormat = CommunityCodeValues.DATE_FORMAT_DATE.value; // "MM/dd/yyyy";
						} else {
							actualDateFormat = CommunityCodeValues.DATE_FORMAT_MONTHYEAR.value; // "MM/yyyy";
						}
					} else {
						actualDateFormat = CommunityCodeValues.DATE_FORMAT_YEAR.value; // "yyyy";
					}
					parsedDate = cal.getTime();
					parsedDateFormat = actualDateFormat;
				}
			} else {
				log.debug("Date is not in MCPD format, ignoring " + dateInYYYYMMDD);
			}
		}

		if (dateValue != null) {
			dateValue.accept(parsedDate);
		}
		if (dateFormat != null) {
			dateFormat.accept(parsedDateFormat);
		}
	}

	private static ThreadLocal<SimpleDateFormat> FORMAT_YYYYMMDD = DateUtils.makeDateFormat("yyyyMMdd");
	private static ThreadLocal<SimpleDateFormat> FORMAT_YYYYMM = DateUtils.makeDateFormat("yyyyMM--");
	private static ThreadLocal<SimpleDateFormat> FORMAT_YYYY = DateUtils.makeDateFormat("yyyy----");

	public static String convert(Date value, String dateCode) {
		if (value == null || value.getTime() == 0) {
			return null;
		}
		if (StringUtils.isBlank(dateCode)) {
			return FORMAT_YYYYMMDD.get().format(value);
		} else if (CommunityCodeValues.DATE_FORMAT_DATE.value.equals(dateCode)) {
			return FORMAT_YYYYMMDD.get().format(value);
		} else if (CommunityCodeValues.DATE_FORMAT_MONTHYEAR.value.equalsIgnoreCase(dateCode)) {
			return FORMAT_YYYYMM.get().format(value);
		} else if (CommunityCodeValues.DATE_FORMAT_YEAR.value.equalsIgnoreCase(dateCode)) {
			return FORMAT_YYYY.get().format(value);
		} else {
			return FORMAT_YYYYMMDD.get().format(value);
		}
	}

}