Package gr.zeus.util

Class DateHelper


  • public final class DateHelper
    extends java.lang.Object
    Helper methods for java.util.Date

    Since:
    1.05
    Author:
    Gregory Kotsaftis
    • Constructor Summary

      Constructors 
      Constructor Description
      DateHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addDaysToDate​(java.util.Calendar cal, int days)
      Adds 'days' to a calendar taking into consideration the month, year and leap years change as well.
      static java.lang.String dateToString​(java.util.Date uDate, java.lang.String pattern)
      Converts a date to a string based on a SimpleDateFormat pattern.
      static int getDayForDate​(java.lang.String dateToCheck, java.lang.String pattern)
      Parses a string into a date.
      static java.lang.String getDayNameForDate​(java.util.Date dt, boolean fullname)
      Gets the name of a day based on a date and current locale.
      static int getMonthForDate​(java.lang.String dateToCheck, java.lang.String pattern)
      Parses a string into a date.
      static int getYearForDate​(java.lang.String dateToCheck, java.lang.String pattern)
      Parses a string into a date.
      static boolean isDateValid​(java.lang.String dateToCheck, java.lang.String pattern)
      Checks a string to see if it contains a valid date in SimpleDateFormat.
      static boolean isLeapYear​(int year)
      Leap years occur in years exactly divisible by four, except those years ending in 00 are leap years only if they are divisible by 400.
      static java.util.Date parseDate​(java.lang.String myDate, java.lang.String pattern)
      Parses a string into a date.
      static void subDaysFromDate​(java.util.Calendar cal, int days)
      Subtracts 'days' from a calendar taking into consideration the month, year and leap years change as well.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DateHelper

        public DateHelper()
    • Method Detail

      • subDaysFromDate

        public static void subDaysFromDate​(java.util.Calendar cal,
                                           int days)
        Subtracts 'days' from a calendar taking into consideration the month, year and leap years change as well.

        Parameters:
        cal - The Calendar.
        days - The number of days.
      • addDaysToDate

        public static void addDaysToDate​(java.util.Calendar cal,
                                         int days)
        Adds 'days' to a calendar taking into consideration the month, year and leap years change as well.

        Parameters:
        cal - The Calendar.
        days - The number of days.
      • isLeapYear

        public static boolean isLeapYear​(int year)
        Leap years occur in years exactly divisible by four, except those years ending in 00 are leap years only if they are divisible by 400.

        Parameters:
        year - The year number.

        Returns:
        true if it is a leap year.
      • parseDate

        public static java.util.Date parseDate​(java.lang.String myDate,
                                               java.lang.String pattern)
                                        throws java.text.ParseException
        Parses a string into a date. String should be in SimpleDateFormat format. e.g. java.util.Date d = parseDate(myDate, "dd/MM/yyyy");

        Parameters:
        myDate - The date string.
        pattern - The pattern to use.

        Returns:
        The Date.

        Throws:
        java.text.ParseException
      • dateToString

        public static java.lang.String dateToString​(java.util.Date uDate,
                                                    java.lang.String pattern)
        Converts a date to a string based on a SimpleDateFormat pattern. e.g. String s = dateToString(uDate, "dd/MM/yyyy");

        Parameters:
        uDate - The date string.
        pattern - The pattern to use.

        Returns:
        The string of the date or null on error.
      • isDateValid

        public static boolean isDateValid​(java.lang.String dateToCheck,
                                          java.lang.String pattern)
        Checks a string to see if it contains a valid date in SimpleDateFormat.

        Parameters:
        dateToCheck - The date string to check.
        pattern - The pattern to use.

        Returns:
        true if it contains a valid date in SimpleDateFormat.
      • getYearForDate

        public static int getYearForDate​(java.lang.String dateToCheck,
                                         java.lang.String pattern)
        Parses a string into a date. String should be in SimpleDateFormat format. Returns only the year of the date or -1 on error.

        NOTE: only 'yyyy' is supported!

        Parameters:
        dateToCheck - The date string to check.
        pattern - the The pattern to use.

        Returns:
        The date or -1 on error.
      • getMonthForDate

        public static int getMonthForDate​(java.lang.String dateToCheck,
                                          java.lang.String pattern)
        Parses a string into a date. String should be in SimpleDateFormat format. Returns only the month of the date or -1 on error.

        NOTE: only 'MM' is supported!

        Parameters:
        dateToCheck - The date string to check.
        pattern - The pattern to use.

        Returns:
        The date or -1 on error.
      • getDayForDate

        public static int getDayForDate​(java.lang.String dateToCheck,
                                        java.lang.String pattern)
        Parses a string into a date. String should be in SimpleDateFormat format. Returns only the day of the date or -1 on error.

        NOTE: only 'dd' is supported!

        Parameters:
        dateToCheck - The date string to check.
        pattern - The pattern to use.

        Returns:
        The day for the date or -1 on error.
      • getDayNameForDate

        public static java.lang.String getDayNameForDate​(java.util.Date dt,
                                                         boolean fullname)
        Gets the name of a day based on a date and current locale.

        Parameters:
        dt - The date.
        fullname - Fetch complete day's name or the short one.

        Returns:
        A string with the name of the day.