- java.lang.Object
-
- com.aoapps.lang.util.CalendarUtils
-
public final class CalendarUtils extends Object
Calendar utilities.- Author:
- AO Industries, Inc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCalendarUtils.DateTimeProducer<T>
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringformatDate(Calendar cal)Formats a date in "YYYY-MM-DD" format.static voidformatDate(Calendar cal, Appendable out)Formats a date in "YYYY-MM-DD" format.static StringformatDateTime(Calendar cal)Formats a date and time in "YYYY-MM-DD HH:MM:SS" format.static voidformatDateTime(Calendar cal, Appendable out)Formats a date and time in "YYYY-MM-DD HH:MM:SS" format.static StringformatTime(Calendar cal)Formats a time in "HH:MM:SS" format.static voidformatTime(Calendar cal, Appendable out)Formats a time in "HH:MM:SS" format.static GregorianCalendargetToday()Gets today's date in the default time zone.static GregorianCalendargetToday(TimeZone timeZone)Gets today's date in the given time zone.static GregorianCalendarparseDate(String yyyy_mm_dd)Gets the date from the "YYYY-MM-DD" format in the default time zone ornullif the parameter isnull.static GregorianCalendarparseDate(String yyyy_mm_dd, TimeZone timeZone)Gets the date from the "YYYY-MM-DD" format in the given time zone ornullif the parameter isnull.static GregorianCalendarparseDateTime(String dateTime)Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.mmm]]]" format in the default time zone ornullif the parameter isnull.static GregorianCalendarparseDateTime(String dateTime, TimeZone timeZone)Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.mmm]]]" format in the given time zone ornullif the parameter isnull.static <T> TparseDateTime(String dateTime, TimeZone timeZone, CalendarUtils.DateTimeProducer<T> producer)Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.nnnnnnnnn]]]" format in the given time zone ornullif the parameter isnull.
-
-
-
Method Detail
-
formatDate
public static String formatDate(Calendar cal)
Formats a date in "YYYY-MM-DD" format.- Returns:
- the formatted date or
nullif the parameter isnull
-
formatDate
public static void formatDate(Calendar cal, Appendable out) throws IOException
Formats a date in "YYYY-MM-DD" format.- Throws:
IOException
-
parseDate
public static GregorianCalendar parseDate(String yyyy_mm_dd, TimeZone timeZone) throws IllegalArgumentException
Gets the date from the "YYYY-MM-DD" format in the given time zone ornullif the parameter isnull. Allows negative years like "-344-01-23". Allows shorter months and days like "1976-1-9".- Parameters:
yyyy_mm_dd- The date in YYYY-MM-DD format.timeZone- The time zone to use ornullto use the default time zone- Throws:
IllegalArgumentException
-
parseDate
public static GregorianCalendar parseDate(String yyyy_mm_dd) throws IllegalArgumentException
Gets the date from the "YYYY-MM-DD" format in the default time zone ornullif the parameter isnull. Allows negative years like "-344-01-23". Allows shorter months and days like "1976-1-9".- Parameters:
yyyy_mm_dd- The date in YYYY-MM-DD format.- Throws:
IllegalArgumentException
-
formatTime
public static String formatTime(Calendar cal)
Formats a time in "HH:MM:SS" format.- Returns:
- the formatted time or
nullif the parameter isnull
-
formatTime
public static void formatTime(Calendar cal, Appendable out) throws IOException
Formats a time in "HH:MM:SS" format.- Throws:
IOException
-
formatDateTime
public static String formatDateTime(Calendar cal)
Formats a date and time in "YYYY-MM-DD HH:MM:SS" format.- Returns:
- the formatted date and time or
nullif the parameter isnull
-
formatDateTime
public static void formatDateTime(Calendar cal, Appendable out) throws IOException
Formats a date and time in "YYYY-MM-DD HH:MM:SS" format.- Throws:
IOException
-
parseDateTime
public static <T> T parseDateTime(String dateTime, TimeZone timeZone, CalendarUtils.DateTimeProducer<T> producer) throws IllegalArgumentException
Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.nnnnnnnnn]]]" format in the given time zone ornullif the parameter isnull. Allows negative years like "-344-01-23". Allows shorter months, days, hours, minutes, and millis like "1976-1-9 1:2:3.1".- Parameters:
timeZone- The time zone to use ornullto use the default time zone- Throws:
IllegalArgumentException
-
parseDateTime
public static GregorianCalendar parseDateTime(String dateTime, TimeZone timeZone) throws IllegalArgumentException
Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.mmm]]]" format in the given time zone ornullif the parameter isnull. Allows negative years like "-344-01-23". Allows shorter months, days, hours, minutes, and millis like "1976-1-9 1:2:3.1".- Parameters:
timeZone- The time zone to use ornullto use the default time zone- Throws:
IllegalArgumentException
-
parseDateTime
public static GregorianCalendar parseDateTime(String dateTime) throws IllegalArgumentException
Gets the date and time from the "YYYY-MM-DD[ HH:MM[:SS[.mmm]]]" format in the default time zone ornullif the parameter isnull. Allows negative years like "-344-01-23". Allows shorter months, days, hours, minutes, and millis like "1976-1-9 1:2:3.1".- Throws:
IllegalArgumentException
-
getToday
public static GregorianCalendar getToday(TimeZone timeZone)
Gets today's date in the given time zone. Hour, minute, second, and millisecond are all set to zero.- Parameters:
timeZone- The time zone to use ornullto use the default time zone
-
getToday
public static GregorianCalendar getToday()
Gets today's date in the default time zone. Hour, minute, second, and millisecond are all set to zero.
-
-