From 6bb2428643e1158089d19f2537f215e6a3b48d1c Mon Sep 17 00:00:00 2001 From: Cyrus Venn Casada Date: Thu, 3 Dec 2015 14:50:25 +0800 Subject: [PATCH] New DateUtil function code taken from v4.0 as dependency for fieldbook designRunner/expDesign code BMS-2015 Reviewer: Aldrin Batac --- .../generationcp/commons/util/DateUtil.java | 437 +++++++++++++----- 1 file changed, 314 insertions(+), 123 deletions(-) diff --git a/src/main/java/org/generationcp/commons/util/DateUtil.java b/src/main/java/org/generationcp/commons/util/DateUtil.java index 57e3d260e..0968cc1fa 100644 --- a/src/main/java/org/generationcp/commons/util/DateUtil.java +++ b/src/main/java/org/generationcp/commons/util/DateUtil.java @@ -1,209 +1,400 @@ + package org.generationcp.commons.util; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import org.generationcp.commons.constant.VaadinMessage; import org.generationcp.commons.exceptions.InvalidDateException; +import org.generationcp.middleware.domain.oms.TermId; +import org.generationcp.middleware.util.Util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Configurable; @Configurable public class DateUtil { - - public static final String DATE_AS_NUMBER_FORMAT = "yyyyMMdd"; - - /** - * Returns the current date in format "yyyyMMdd" - * - * @param - * @return + + private static final Logger LOG = LoggerFactory.getLogger(DateUtil.class); + + public static final String DATE_AS_NUMBER_FORMAT = Util.DATE_AS_NUMBER_FORMAT; + public static final String FRONTEND_DATE_FORMAT = Util.FRONTEND_DATE_FORMAT; + public static final String FRONTEND_DATE_FORMAT_2 = Util.FRONTEND_DATE_FORMAT_2; + public static final String FRONTEND_TIMESTAMP_FORMAT = Util.FRONTEND_TIMESTAMP_FORMAT; + + private DateUtil() { + // use a private constructor to hide the implicit public one + } + + /** + * Returns the current date in format "yyyyMMdd" as Integer + * + * @return current date as Integer */ - public static Integer getCurrentDate(){ - Calendar now = Calendar.getInstance(); - SimpleDateFormat formatter = new SimpleDateFormat(DATE_AS_NUMBER_FORMAT); - String dateNowStr = formatter.format(now.getTime()); - Integer dateNowInt = Integer.valueOf(dateNowStr); - return dateNowInt; + public static Integer getCurrentDateAsIntegerValue() { + return Util.getCurrentDateAsIntegerValue(); + } + /** + * Returns the current date in format "yyyyMMdd" as Long + * + * @return current date as Long + */ + public static Long getCurrentDateAsLongValue() { + return Util.getCurrentDateAsLongValue(); } - + + /** + * Returns the current date in format "yyyyMMdd" as String + * + * @return current date as String + */ + public static String getCurrentDateAsStringValue() { + return Util.getCurrentDateAsStringValue(); + } + + /** + * Returns the current date + * + * @return current date as Date + */ + public static Date getCurrentDate() { + return Util.getCurrentDate(); + } + + /** + * Returns the calendar instance + * + * @return calendar instance + */ + public static Calendar getCalendarInstance() { + return Util.getCalendarInstance(); + } + + /** + * Returns the current date in the specified format as String + * + * @return current date as String + */ + public static String getCurrentDateAsStringValue(String format) { + return Util.getCurrentDateAsStringValue(format); + } + + /** + * Returns the date in the specified format as String + * + * @return date in the specified format as String + */ + public static String formatDateAsStringValue(Date date, String format) { + return Util.formatDateAsStringValue(date, format); + } + + /** + * Returns the date object from the specified format + * + * @return date object + * @throws ParseException + */ + public static Date parseDate(String date, String format) throws ParseException { + return Util.parseDate(date, format); + } + + /** + * Parses the date given default format + * + * @param date the date + * @return the date + * @throws ParseException the parse exception + */ + public static Date parseDate(String date) throws ParseException { + return Util.parseDate(date, Util.DATE_AS_NUMBER_FORMAT); + } + + /** + * Returns the SimpleDateFormat of the current display locale + * + * @return SimpleDateFormat + */ + public static SimpleDateFormat getSimpleDateFormat(String format) { + return Util.getSimpleDateFormat(format); + } + /** * Returns in format "yyyyMMdd" - * + * * @param time - the date in long format * @return */ - public static Integer getIBPDate(long time){ - SimpleDateFormat formatter = new SimpleDateFormat(DATE_AS_NUMBER_FORMAT); + public static Integer getIBPDate(long time) { + SimpleDateFormat formatter = DateUtil.getSimpleDateFormat(DateUtil.DATE_AS_NUMBER_FORMAT); String dateStr = formatter.format(time); - Integer dateInt = Integer.valueOf(dateStr); - return dateInt; + return Integer.valueOf(dateStr); } - /** + /** * Returns in format "yyyyMMdd" - * + * * @param year * @param month * @param day * @return */ - public static Integer getIBPDate(Integer year, Integer month, Integer day) throws InvalidDateException{ - validateDate(year, month, day); + public static Integer getIBPDate(Integer year, Integer month, Integer day) throws InvalidDateException { + DateUtil.validateDate(year, month, day); return Integer.valueOf(year * 10000 + month * 100 + day); } - + /** * Checks if a given date is valid. - * + * * @param year * @param month * @param day * @return VaadinMessage Enum that is to be interpreted in specific web application */ - public static void validateDate(int year, int month, int day) throws InvalidDateException{ - - if(!isValidYear(year)){ - throw new InvalidDateException("Year must be greater than or equal to 1900", VaadinMessage.INVALID_YEAR); + public static void validateDate(int year, int month, int day) throws InvalidDateException { + if (!DateUtil.isValidYear(year)) { + throw new InvalidDateException("Year must be greater than or equal to 1900", VaadinMessage.INVALID_YEAR); } - if (month < 0 || month > 12) { - throw new InvalidDateException("Month out of range", VaadinMessage.ERROR_MONTH_OUT_OF_RANGE); + if (month <= 0 || month > 12) { + throw new InvalidDateException("Month out of range", VaadinMessage.ERROR_MONTH_OUT_OF_RANGE); } - if (month == 2){ - if (isLeapYear(year)){ - if (day < 0 || day > 29){ - throw new InvalidDateException("Day out of range", VaadinMessage.ERROR_DAY_OUT_OF_RANGE); - } - } else { - if (day < 0 || day > 28){ - throw new InvalidDateException("Day out of range", VaadinMessage.ERROR_DAY_OUT_OF_RANGE); - } - } - } else if (((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30)) || (day < 0 || day > 31)){ - throw new InvalidDateException("Day out of range", VaadinMessage.ERROR_DAY_OUT_OF_RANGE); + if (day <= 0 || day > DateUtil.daysInMonth(year, month)) { + throw new InvalidDateException("Day out of range", VaadinMessage.ERROR_DAY_OUT_OF_RANGE); } } - + /** * Checks if a given date is valid. - * + * * @param year * @param month * @param day * @return */ public static boolean isValidDate(int year, int month, int day) { - if(!isValidYear(year)){ - return false; - } - if (month < 0 || month > 12) { - return false; - } - if (month == 2){ - if (isLeapYear(year)){ - if (day < 0 || day > 29){ - return false; - } - } else { - if (day < 0 || day > 28){ - return false; - } - } - } else if (((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30)) || (day < 0 || day > 31)){ - return false; - } - return true; + boolean yearOk = DateUtil.isValidYear(year); + boolean monthOk = month >= 1 && month <= 12; + boolean dayOk = day >= 1 && day <= DateUtil.daysInMonth(year, month); + return yearOk && monthOk && dayOk; } - + /** * Checks if the given year is a leap year. - * + * * @param year * @return */ - public static boolean isLeapYear(int year){ + public static boolean isLeapYear(int year) { boolean isLeapYear = false; if (year % 400 == 0) { isLeapYear = true; } else if (year % 100 == 0) { isLeapYear = false; - } else if (year % 4 == 0 ) { + } else if (year % 4 == 0) { isLeapYear = true; } else { isLeapYear = false; } return isLeapYear; - } - - /** + } + + /** * Returns the actual year given a date object - * - * @param Date object - * @return Integer year - */ - public static Integer getYear(Date date){ - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - Integer year = calendar.get(Calendar.YEAR); - - return year; - } - - /** - * Returns integer value of year month date concatenation - * if no date value, yyyymm - * eg. if month = 1, and year = 2005 => 200501 - * if no month and date, yyyy - * + * @param date + * @return + */ + public static Integer getYear(Date date) { + String year = DateUtil.getSimpleDateFormat("yyyy").format(date); + return Integer.parseInt(year); + } + + /** + * Returns integer value of year month date concatenation if no date value, yyyymm eg. if month = 1, and year = 2005 => 200501 if no + * month and date, yyyy + * * Return 0 if no year, month and date - * + * * @param year * @param month * @param day * @return */ - public static Integer getIBPDateNoZeroes(int year, int month, int day){ - String dayString = (day == 0) ? "" : String.valueOf(day); - if (!dayString.isEmpty() && dayString.length() == 1){ - dayString = "0" + dayString; + public static Integer getIBPDateNoZeroes(int year, int month, int day) { + String dayString = day == 0 ? "" : String.valueOf(day); + if (!dayString.isEmpty() && dayString.length() == 1) { + dayString = "0" + dayString; } - String monthString = (month == 0) ? "" : String.valueOf(month); - if (!monthString.isEmpty() && monthString.length() == 1){ - monthString = "0" + monthString; + String monthString = month == 0 ? "" : String.valueOf(month); + if (!monthString.isEmpty() && monthString.length() == 1) { + monthString = "0" + monthString; } - String yearString = (year == 0) ? "" : String.valueOf(year); + String yearString = year == 0 ? "" : String.valueOf(year); String fulldate = yearString + monthString + dayString; - if (!fulldate.isEmpty()){ - return new Integer(fulldate); + if (!fulldate.isEmpty()) { + return new Integer(fulldate); } return 0; } - - /*** + + /** * Returns true if year is greater than or equal to 1900 - * - * @param Integer year - * @return boolean - * */ - public static boolean isValidYear(Integer year){ - if(year < 1900){ - return false; - } - else if(year > 9999){ - return false; - } - return true; - } - + * @param year + * @return + */ + public static boolean isValidYear(Integer year) { + if (year < 1900) { + return false; + } else if (year > 9999) { + return false; + } + return true; + } + /*** * Returns true if year is greater than or equal to 1900 - * - * @param Date date + * + * @param date * @return boolean * */ - public static boolean isValidYear(Date date){ - return isValidYear(getYear(date)); + public static boolean isValidYear(Date date) { + return DateUtil.isValidYear(DateUtil.getYear(date)); + } + + /** + * Converts the date from the old format to the new format + * + * @param date + * @param oldFormat + * @param newFormat + * @return String converted date from old format to new format + * @throws ParseException + */ + public static String convertDate(String date, String oldFormat, String newFormat) throws ParseException { + SimpleDateFormat sdf = DateUtil.getSimpleDateFormat(oldFormat); + Date d = sdf.parse(date); + sdf.applyPattern(newFormat); + return sdf.format(d); + } + + /** + * Return current date in "yyyy-MM-dd" format + * + * @return String + */ + public static String getCurrentDateInUIFormat() { + return Util.getCurrentDateAsStringValue(Util.FRONTEND_DATE_FORMAT); + } + + /** + * Return specified date in "yyyy-MM-dd" format + * + * @param date to convert + * @return String + */ + public static String getDateInUIFormat(Date date) { + if (date == null) { + return ""; + } + return Util.formatDateAsStringValue(date, Util.FRONTEND_DATE_FORMAT); + } + + public static String convertToDBDateFormat(Integer dataTypeId, String value) { + String returnVal = value; + + if (DateUtil.isInDBDateFormat(returnVal)) { + return returnVal; + } + + if (dataTypeId != null && dataTypeId == TermId.DATE_VARIABLE.getId() && value != null && !"".equalsIgnoreCase(value)) { + try { + return DateUtil.convertDate(value, DateUtil.FRONTEND_DATE_FORMAT, DateUtil.DATE_AS_NUMBER_FORMAT); + } catch (ParseException e) { + DateUtil.LOG.error(e.getMessage(), e); + returnVal = ""; + } + } + return returnVal; + } + + public static boolean isInDBDateFormat(String dateString) { + if (dateString != null) { + return dateString.matches("((19|20)\\d\\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])") ? true : false; + } + return false; + } + + public static String convertToUIDateFormat(Integer dataTypeId, String value) { + String returnVal = value; + if (dataTypeId != null && dataTypeId == TermId.DATE_VARIABLE.getId() && value != null && !"".equalsIgnoreCase(value)) { + try { + return DateUtil.convertDate(value, DateUtil.DATE_AS_NUMBER_FORMAT, DateUtil.FRONTEND_DATE_FORMAT); + } catch (ParseException e) { + DateUtil.LOG.error(e.getMessage(), e); + returnVal = ""; + } + } + return returnVal; + } + + public static boolean isValidDate(String dateString) { + if (dateString == null || dateString.length() != DateUtil.DATE_AS_NUMBER_FORMAT.length()) { + return false; + } + + int date; + try { + date = Integer.parseInt(dateString); + } catch (NumberFormatException e) { + return false; + } + + int year = date / 10000; + int month = date % 10000 / 100; + int day = date % 100; + + return DateUtil.isValidDate(year, month, day); + } + + public static boolean isValidFieldbookDate(String dateString) { + if (dateString == null || dateString.length() != Util.DATE_AS_NUMBER_FORMAT.length()) { + return false; + } + + int date; + try { + date = Integer.parseInt(dateString); + } catch (NumberFormatException e) { + return false; + } + + int year = date / 10000; + int month = date % 10000 / 100; + int day = date % 100; + + // leap years calculation not valid before 1581 + boolean yearOk = year >= 1581; + boolean monthOk = month >= 1 && month <= 12; + boolean dayOk = day >= 1 && day <= DateUtil.daysInMonth(year, month); + + return yearOk && monthOk && dayOk; + } + + public static int daysInMonth(int year, int month) { + int daysInMonth; + if (month == 2) { + if (DateUtil.isLeapYear(year)) { + daysInMonth = 29; + } else { + daysInMonth = 28; + } + } else if (month == 4 || month == 6 || month == 9 || month == 11) { + daysInMonth = 30; + } else { + daysInMonth = 31; + } + return daysInMonth; } }