Package gr.zeus.util

Class DateHelper

java.lang.Object
gr.zeus.util.DateHelper

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

Since:
1.05
Author:
Gregory Kotsaftis
  • Constructor Details

    • DateHelper

      public DateHelper()
  • Method Details

    • subDaysFromDate

      public static void subDaysFromDate(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(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 Date parseDate(String myDate, String pattern) throws 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:
      ParseException
    • dateToString

      public static String dateToString(Date uDate, 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(String dateToCheck, 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(String dateToCheck, 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(String dateToCheck, 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(String dateToCheck, 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 String getDayNameForDate(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.