DateUtils

From GreenVulcano Wiki
Jump to: navigation, search

Class FQN: it.greenvulcano.util.txt.DateUtils

Following the DateUtils public constants

    /**
     * The conversion pattern to be used for input string that represent an OS timestamp.
     */
    String FORMAT_SYSTEM_TIME       = "SYSTEM_TIME";

    /**
     * Short ISO date/time format
     */
    String FORMAT_ISO_DATE_S        = "yyyyMMdd";
    String FORMAT_ISO_TIME_S        = "HHmmss";
    String FORMAT_ISO_DATETIME_S    = "yyyyMMdd HHmmss";
    String FORMAT_ISO_TIMESTAMP_S   = "yyyyMMdd HHmmssSSS";
    /**
     * Long ISO date/time format
     */
    String FORMAT_ISO_DATE_L        = "yyyy-MM-dd";
    String FORMAT_ISO_TIME_L        = "HH:mm:ss";
    String FORMAT_ISO_DATETIME_L    = "yyyy-MM-dd HH:mm:ss";
    String FORMAT_ISO_TIMESTAMP_L   = "yyyy-MM-dd HH:mm:ss.SSS";
    /**
     * ISO date/time + UTC format
     */
    String FORMAT_ISO_DATETIME_UTC  = "yyyy-MM-dd'T'HH:mm:ssZ";
    /**
     * IETF date/time format
     */
    String FORMAT_IETF_DATETIME     = "EEE, d MMM yyyy HH:mm:ss z";

    String DEFAULT_FORMAT_DATE      = FORMAT_ISO_DATE_L;
    String DEFAULT_FORMAT_TIME      = FORMAT_ISO_TIME_L;
    String DEFAULT_FORMAT_DATETIME  = FORMAT_ISO_DATETIME_L;
    String DEFAULT_FORMAT_TIMESTAMP = FORMAT_ISO_TIMESTAMP_L;

Following the DateUtils public static methods

  • Date/Time conversion
    /**
     * Convert a String date representation to a Date instance.
     * 
     * @param date 
     *             the string representation of Date/Time
     * @param format 
     *             the conversion pattern
     * @return a Date instance
     */
    public static Date stringToDate(String date, String format)

    /**
     * Convert a String date representation to a Date instance.
     * 
     * @param date 
     *             the string representation of Date/Time
     * @param format 
     *             the conversion pattern
     * @param tZone 
     *             the TimeZone to be used in conversion
     * @return a Date instance
     */
    public static Date stringToDate(String date, String format, String tZone)

    /**
     * Convert a String date representation to a Date instance.
     * 
     * @param date 
     *             the string representation of Date/Time
     * @param format 
     *             the conversion pattern
     * @param tZone 
     *             the TimeZone to be used in conversion
     * @param lang 
     *             the Locale to be used in conversion
     * @return a Date instance
     */
    public static Date stringToDate(String date, String format, String tZone, String lang)

    /**
     * Convert a Date instance to a String representation.
     * 
     * @param date 
     *             the Date instance to convert
     * @param format 
     *             the conversion pattern
     * @return a String representation of date
     */
    public static String dateToString(Date date, String format)

    /**
     * Convert a Date instance to a String representation.
     * 
     * @param date 
     *             the Date instance to convert
     * @param format 
     *             the conversion pattern
     * @param tZone 
     *             the TimeZone to be used in conversion
     * @return a String representation of date
     */
    public static String dateToString(Date date, String format, String tZone)

    /**
     * Convert a Date instance to a String representation.
     * 
     * @param date 
     *             the Date instance to convert
     * @param format 
     *             the conversion pattern
     * @param tZone 
     *             the TimeZone to be used in conversion
     * @param lang 
     *             the Locale to be used in conversion
     * @return a String representation of date
     */
    public static String dateToString(Date date, String format, String tZone, String lang)

    /**
     * Convert a String representation of date/Time to another format. 
     * The string representation is interpreted according to
     * default locale/timezone.
     * 
     * @param date 
     *             the date/time representation to convert
     * @param formatIn 
     *             the input date/time conversion pattern
     * @param formatOut 
     *             the output date/time conversion pattern
     * @return the new String representation
     */
    public static String convertString(String date, String formatIn, String formatOut)

    /**
     * Convert a String representation of date/Time to another format. 
     * The string representation is interpreted according tolocal timezone.
     * 
     * @param date 
     *             the date/time representation to convert
     * @param formatIn 
     *             the input date/time conversion pattern
     * @param tZoneIn 
     *             the input time zone to be used in conversion
     * @param formatOut 
     *             the output date/time conversion pattern
     * @param tZoneOut 
     *             the output time zone to be used in conversion
     * @return the new String representation
     */
    public static String convertString(String date, String formatIn, String tZoneIn, String formatOut, String tZoneOut)

    /**
     * Convert a String representation of date/Time to another format. 
     * The string representation is interpreted according to specified
     * locale/timezone.
     * 
     * @param date 
     *             the date/time representation to convert
     * @param formatIn 
     *             the input date/time conversion pattern
     * @param tZoneIn 
     *             the input time zone to be used in conversion
     * @param langIn 
     *             the input locale to be used in conversion
     * @param formatOut 
     *             the output date/time conversion pattern
     * @param tZoneOut 
     *             the output time zone to be used in conversion
     * @param langOut 
     *             the output locale to be used in conversion
     * @return the new String representation
     */
    public static String convertString(String date, String formatIn, String tZoneIn, String langIn, String formatOut, String tZoneOut, String langOut)

    /**
     * Convert a String representation of date and an ordinal hour to another format. 
     * The string representation is interpreted according to specified
     * locale/timezone.
     * 
     * @param date 
     *             the date/time representation to convert
     * @param formatIn 
     *             the input date/time conversion pattern
     * @param hour 
     *             the ordinal day hour [1..[23|24|25]
     * @param formatOut 
     *             the output date/time conversion pattern
     * @param tZoneOut 
     *             the output time zone to be used in conversion
     * @return the new String representation of date/time
     */
    public static String convertDayHourString(String date, String formatIn, int hour, String formatOut, String tZoneOut)

    /**
     * Return a String representation of the current date/time, according to default timezone.
     * 
     * @param format 
     *             the date/time conversion pattern
     * @return the String representation of current date/time
     */
    public static String nowToString(String format)

    /**
     * Return a String representation of the current date/time, according to tZone timezone.
     * 
     * @param format 
     *             the date/time conversion pattern
     * @param tZone 
     *             the destination timezone
     * @return the String representation of current date/time
     */
    public static String nowToString(String format, String tZone)

    /**
     * Return a String representation of the current date/time, according to tZone timezone
     * and lang locale.
     * 
     * @param format 
     *             the date/time conversion pattern
     * @param tZone 
     *             the destination timezone
     * @param lang 
     *             the locale to be used in conversion
     * @return the String representation of current date/time
     */
    public static String nowToString(String format, String tZone, String lang)

    /**
     * Return a String representation, in the given format, of tomorrow date.
     * 
     * @param format 
     *             the required format.
     * @return a String representation of tomorrow.
     */
    public static String tomorrowToString(String format)




  • Special Date/Time access
    /**
     * Return {@link java.util.Date} representing the first day of the week
     * containing date.
     * 
     * @param date the contained date
     * @return the first week day
     */
    public static Date getWeekStart(Date date)


    /**
     * Return {@link java.util.Date} representing the last day of the week
     * containing date.
     * 
     * @param date the contained date
     * @return the last week day
     */
    public static Date getWeekEnd(Date date)


    /**
     * Return {@link java.util.Date} representing the first day of the n't  week of year.
     * 
     * @param week the requested week
     * @param year the requested year
     * @return the first week day
     */
    public static Date getWeekStart(int week, int year)


    /**
     * Return {@link java.util.Date} representing the last day of the n't week of year.
     * 
     * @param week the requested week
     * @param year the requested year
     * @return the last week day
     */
    public static Date getWeekEnd(int week, int year)

    /**
     * Return {@link java.lang.String} representing the first day of the week containing datein the given format.
     * 
     * @param date the contained date
     * @param format the input/output date format
     * @return the first week day
     */
    public static String getWeekStart(String date, String format)
 
    /**
     * Return {@link java.lang.String} representing the last day of the week containing date in the given format.
     * 
     * @param date the contained date
     * @param format the input/output date format
     * @return the last week day
     */
    public static String getWeekEnd(String date, String format)

    /**
     * Return {@link java.lang.String} representing the first day of the n't week of year, in the given format.
     * 
     * @param week the requested week
     * @param year the requested year
     * @param format the output date format
     * @return the first week day
     */
    public static String getWeekStart(String week, String year, String format)


    /**
     * Return {@link java.lang.String} representing the last day of the n't week of year, in the given format.
     * 
     * @param week the requested week
     * @param year the requested year
     * @param format the output date format
     * @return the last week day
     */
    public static String getWeekEnd(String week, String year, String format)
 
    /**
     * Return {@link java.util.Date} representing the first day of the month containing date.
     * 
     * @param date the contained date
     * @return the first month day
     */
    public static Date getMonthStart(Date date)

    /**
     * Return {@link java.util.Date} representing the last day of the month containing date.
     * 
     * @param date the contained date
     * @return the last month day
     */
    public static Date getMonthEnd(Date date)
 

    /**
     * Return {@link java.lang.String} representing the first day of the month containing date in the given format.
     * 
     * @param date the contained date
     * @param format the input/output date format
     * @return the first month day
     */
    public static String getMonthStart(String date, String format)

    /**
     * Return {@link java.lang.String} representing the last day of the month containing date in the given format.
     * 
     * @param date the contained date
     * @param format the input/output date format
     * @return the last month day
     */
    public static String getMonthEnd(String date, String format)

    /**
     * Return {@link java.util.Date} representing the first day of the n't month of year.
     * 
     * @param month the requested month
     * @param year the requested year
     * @return the first month day
     */
    public static Date getMonthStart(int month, int year)


    /**
     * Return {@link java.util.Date} representing the last day of the n't month of year.
     * 
     * @param month the requested month
     * @param year the requested year
     * @return the last month day
     */
    public static Date getMonthEnd(int month, int year)

    /**
     * Return {@link java.lang.String} representing the first day of the n't month of year, in the given format.
     * 
     * @param month the requested month
     * @param year the requested year
     * @param format the output date format
     * @return the first month day
     */
    public static String getMonthStart(String month, String year, String format)

    /**
     * Return {@link java.lang.String} representing the last day of the n't month of year, in the given format.
     * 
     * @param month the requested month
     * @param year the requested year
     * @param format the output date format
     * @return the last month day
     */
    public static String getMonthEnd(String month, String year, String format)

    /**
     * Return {@link java.util.Date} representing the first day of the n't quarter of year.
     * 
     * @param quarter  the requested quarter
     * @param year the requested year
     * @return the first quarter day
     */
    public static Date getQuarterStart(int quarter, int year)


    /**
     * Return {@link java.util.Date} representing the last day of the n't quarter of year.
     * 
     * @param quarter the requested quarter
     * @param year the requested year
     * @return the last quarter day
     */
    public static Date getQuarterEnd(int quarter, int year)

    /**
     * Return {@link java.lang.String} representing the first day of the n't quarter of year, in the given format.
     * 
     * @param quarter the requested quarter
     * @param year he requested year
     * @param format the output date format
     * @return the first quarter day
     */
    public static String getQuarterStart(String quarter, String year, String format)

    /**
     * Return {@link java.lang.String} representing the last day of the n't
     * quarter of year, in the given format.
     * 
     * @param quarter the requested quarter
     * @param year the requested year
     * @param format the output date format
     * @return the last quarter day
     */
    public static String getQuarterEnd(String quarter, String year, String format)

    /**
     * Return true if the given date is outside the daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDaySolar(Date date)

    /**
     * Return true if the given date, represented as {@link java.lang.String} in the given format,
     * is outside the daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDaySolar(String data, String format)

    /**
     * Return true if the given date is in the daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDayLegal(Date data)

    /**
     * Return true if the given date, represented as {@link java.lang.String} in the given format,
     * is in the daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDayLegal(String data, String format)

    /**
     * Return true if the given date is the transition day to/from daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDayOfHourChange(Date data)

    /**
     * Return true if the given date, represented as {@link java.lang.String} in the given format,
     * is the transition day to/from daylight saving time.
     * 
     * @param date
     * @return
     */
    public static boolean isDayOfHourChange(String data, String format)

    /**
     * Return the transition day to daylight saving time for the current year.
     * 
     * @param date
     * @return
     */
    public static Date getDayOfSolarToLegalChange()

    /**
     * Return the transition day to daylight saving time for the given year.
     * 
     * @param date
     * @return
     */
    public static Date getDayOfSolarToLegalChange(int year)

    public static boolean isDayOfSolarToLegalChange(Date date)

    public static boolean isDayOfSolarToLegalChange(String date, String format)

    public static boolean encloseDayOfSolarToLegalChange(String date1, String format1, String date2, String format2)

    public static boolean encloseDayOfSolarToLegalChange(Date date1, Date date2)

    public static boolean encloseDayOfSolarToLegalChange(Calendar date1, Calendar date2)

    public static Date getDayOfLegalToSolarChange()

    public static Date getDayOfLegalToSolarChange(int year)

    public static boolean isDayOfLegalToSolarChange(Date date)

    public static boolean isDayOfLegalToSolarChange(String date, String format)

    public static boolean encloseDayOfLegalToSolarChange(String date1, String format1, String date2, String format2)

    public static boolean encloseDayOfLegalToSolarChange(Date date1, Date date2)

    public static boolean encloseDayOfLegalToSolarChange(Calendar date1, Calendar date2)

    public static boolean encloseDayOfHourChange(String date1, String format1, String date2, String format2)

    public static boolean encloseDayOfHourChange(Date date1, Date date2)

    public static boolean encloseDayOfHourChange(Calendar date1, Calendar date2)

    /**
     * @return the haveEasterDay
     */
    public static boolean haveEasterDay()

    /**
     * Calculate Easter Sunday.
     * This algorithm holds for any year in the Gregorian Calendar,
     * which (of course) means years including and after 1583.
     * 
     * @param nYear
     *        4 digit year
     */
    public static Calendar getEasterSunday(int nYear)


  • Date/Time manipulation
   /**
     * Returns the default Locale instance used by DateUtils.
    **/
    public static Locale getDefaultLocale()

    /**
     * Returns the default TimeZone instance used by DateUtils.
    **/
    public static TimeZone getDefaultTimeZone()

    /**
     * Create a Calendar instance with default timezone.
     **/
    public static Calendar createCalendar()

    /**
     * @return the tomorrow {@link java.util.Date} object
     */
    public static Date getTomorrow()


    /**
     * Compare two date/time represented as {@link java.lang.String} in the  given format.
     * 
     * @return -1 if date1 < date2, 0 if date1 = date2, 1 if date1 > date2
     */
    public static int compare(String date1, String format1, String date2, String format2)


    /**
     * Compare two date/time represented as {@link java.lang.String} in the  given format and {@link java.util.Date}.
     * 
     * @return -1 if date1 < date2, 0 if date1 = date2, 1 if date1 > date2
     */
    public static int compare(String date1, String format1, Date date2)




    /**
     * Change the date/time represented as {@link java.lang.String} in the given format.
     * The parameters type and value must be the same of {@link java.util.Calendar}.
     * 
     * @param date the date/time to change
     * @param format the date/time conversion pattern
     * @param type the field to change
     * @param value the field value
     * @return the modified date
     */
    public static Date setTime(String date, String format, int type, int value)

    /**
     * Change the date/time represented as {@link java.util.Date}. 
     * The parameters type and value must be the same of {@link java.util.Calendar}.
     * 
     * @param date the date/time to change
     * @param type the field to change
     * @param value the field value
     * @return the modified date
     */
    public static Date setTime(Date date, int type, int value)

    /**
     * Change the date/time represented as {@link java.lang.String} in the given format. 
     * The parameters type and value must be the same of {@link java.util.Calendar}.
     * 
     * @param date
     *        the date/time to change
     * @param format
     *        the date/time conversion pattern
     * @param type
     *        the field to change
     * @param value
     *        the offset value
     * @return the modified date
     */
    public static Date addTime(String date, String format, int type, int value)

    /**
     * Change the date/time represented as {@link java.lang.String} in the given format.
     * The parameters type and value must be the same of {@link java.util.Calendar}.
     * 
     * @param date the date/time to change
     * @param format the date/time conversion pattern
     * @param type the field to change
     * @param value the offset value
     * @return the modified date
     */
    public static String addTime(String date, String format, String type, String value)

    /**
     * Change the date/time represented as {@link java.util.Date}.
     * The parameters type and value must be the same of {@link java.util.Calendar}.
     * 
     * @param date the date/time to change
     * @param type the field to change
     * @param value the offset value
     * @return the modified date
     */
    public static Date addTime(Date date, int type, int value)

    /**
     * Change the current date/time. The parameters type and
     * value must be the same of {@link java.util.Calendar}.
     * 
     * @param type the field to change
     * @param value the offset value
     * @param onlyDate
     * if true the following fields are resetted: Calendar.HOUR_OF_DAY
     *        Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
     * @return the modified date
     */
    public static Date addTime(int type, int value, boolean onlyDate)

    /**
     * Copy the following fields of the date/time date2 to
     * date1, represented as {@link java.util.Date}:
     * Calendar.DAY_OF_MONTH Calendar.MONTH Calendar.YEAR
     * 
     * @param date1 the date/time to change
     * @param date2 the source date/time
     * @return the modified date1
     */
    public static Date copyDate(Date date1, Date date2)

    /**
     * Copy the following fields of the date/time date2 to
     * date1, represented as {@link java.util.Calendar}:
     * Calendar.DAY_OF_MONTH Calendar.MONTH Calendar.YEAR
     * 
     * @param date1 the date/time to change
     * @param date2 the source date/time
     * @return the modified date1
     */
    public static Calendar copyDate(Calendar date1, Calendar date2)

    /**
     * Copy the following fields of the date/time date2 to
     * date1, represented as {@link java.util.Date}:
     * Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date1 the date/time to change
     * @param date2 the source date/time
     * @return the modified date1
     */
    public static Date copyTime(Date date1, Date date2)

    /**
     * Copy the following fields of the date/time date2 to
     * date1, represented as {@link java.util.Calendar}:
     * Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date1 the date/time to change
     * @param date2 the source date/time
     * @return the modified date1
     */
    public static Calendar copyTime(Calendar date1, Calendar date2)

    /**
     * Reset the following fields of the date/time represented as
     * {@link java.util.Date}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Date resetTime(Date date)

    /**
     * Reset the following fields of the date/time represented as
     * {@link java.util.Calendar}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Calendar resetTime(Calendar date)

    /**
     * Reset the following fields of the date/time represented as
     * {@link java.util.Date}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Date setStartOfDay(Date date)

    /**
     * Reset the following fields of the date/time represented as
     * {@link java.util.Calendar}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Calendar setStartOfDay(Calendar date)

    /**
     * Set to their maximum, the following fields of the date/time represented
     * as {@link java.util.Date}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Date setEndOfDay(Date date)

    /**
     * Set to their maximum, the following fields of the date/time represented
     * as {@link java.util.Calendar}: Calendar.HOUR_OF_DAY Calendar.MINUTE
     * Calendar.SECOND Calendar.MILLISECOND
     * 
     * @param date the date/time to change
     * @return the modified date
     */
    public static Calendar setEndOfDay(Calendar date)

    /**
     * Generate a range of days starting from date1 till
     * date2, represented as {@link java.lang.String} in the given
     * format.
     * 
     * @param date1 start date (inclusive)
     * @param format1 format of start date
     * @param date2 end date (inclusive)
     * @param format2 format of end date
     * @return an array of {@link java.util.Date}
     */
    public static Date[] generateRange(String date1, String format1, String date2, String format2)

    /**
     * Generate a range of days starting from date1 till
     * date2, represented as {@link java.util.Date}.
     * 
     * @param date1 start date (inclusive)
     * @param date2 end date (inclusive)
     * @return an array of {@link java.util.Date}
     */
    public static Date[] generateRange(Date date1, Date date2)


    /**
     * Generate a range of days starting from date1 tilldate2, represented as {@link java.util.Calendar}.
     * 
     * @param date1 start date (inclusive)
     * @param date2 end date (inclusive)
     * @return an array of {@link java.util.Date}
     */
    public static Date[] generateRange(Calendar date1, Calendar date2)

    /**
     * Return the days difference as date2 - date1, represented as {@link java.util.Date}.
     * 
     * @param date1
     * @param date2
     * @param tZone
     * @return the days difference
     */
    public static DateDiff getDiff(Date date1, Date date2, TimeZone tZone)

    /**
     * Return the days difference as date2 - date1, represented as {@link java.lang.String} in the given format.
     * 
     * @param date1
     * @param format1
     * @param date2
     * @param format2
     * @param tZone
     * @return the days difference
     */
    public static DateDiff getDiff(String date1, String format1, String date2, String format2, TimeZone tZone)


    public static int numHoursInDay(Date date)

    public static int numHoursInDay(String date, String format)

    /**
     * Returns type of day: 1 = Feriale; 2 = Prefestivo; 4 = Festivo;
     * 
     * @param today
     * @return
     */
    public static int checkDay(Date day) throws IllegalStateException

    public static int checkDay(String data, String format)

    public static boolean checkDayType(int dayType, Date day) throws IllegalStateException