Package com.ocs.dynamo.utils
Class DateUtils
- java.lang.Object
-
- com.ocs.dynamo.utils.DateUtils
-
public final class DateUtils extends Object
Date utility class- Author:
- bas.rutten
-
-
Field Summary
Fields Modifier and Type Field Description static StringWEEK_CODE_PATTERN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TcreateJava8Date(Class<T> clazz, String dateStr, String format)static LocalDatecreateLocalDate(String dateStr)Creates a java.time.LocalTime based on a String representationstatic LocalDatecreateLocalDate(String dateStr, String format)Creates a java.time.LocalTime based on a String representationstatic LocalDateTimecreateLocalDateTime(String dateTimeStr)Creates a java.time.LocalDateTime based on a String representationstatic LocalDateTimecreateLocalDateTime(String dateTimeStr, String format)Creates a java.time.LocalDateTime based on a String representationstatic LocalTimecreateLocalTime(String timeStr)Creates a java.time.LocalTime based on a String representationstatic LocalTimecreateLocalTime(String timeStr, String format)Creates a java.time.LocalTime based on a String representationstatic ZonedDateTimecreateZonedDateTime(String dateTimeStr)Create a ZonedDateTime based from a String, using the default formatstatic ZonedDateTimecreateZonedDateTime(String dateTimeStr, String format)Creates a ZonedDateTime from a String, using the provided formatstatic StringformatDate(LocalDate date, String format)Formats a LocalDate according to the specified formatstatic StringformatDateTime(LocalDateTime dateTime, String format)Formats a LocalDateTime according to the specified formatstatic StringformatJava8Date(Class<?> clazz, Object value, String format)Formats a Java 8 date/time/datetime based on the specified formatstatic StringformatTime(LocalTime time, String format)Formats a java.time.LocalTime according to the specified formatstatic StringformatZonedDateTime(ZonedDateTime dateTime, String format)Formats a ZonedDateTime according to the specified formatstatic intgetLastWeekOfYear(int year)Return the week number (1 - 53) of the last week of the specified yearstatic StringgetNextWeekCode(String weekCode)Returns the next week code given an existing week codestatic intgetQuarter(LocalDate date)Returns the quarter of the year of a date, as an integer (1 to 4).static LocalDategetStartDateOfWeek(String weekCode)Translates a week code (yyyy-ww) to the starting day (this is taken to be a Monday) of that weekstatic booleanisJava8DateType(Class<?> clazz)Çhecks whether a class represents a Java 8 date or time typestatic booleanisSupportedDateType(Class<?> clazz)Checks if a class represents a supported Date time.static booleanisValidWeekCode(String weekCode)Checks if a string represents a valid week code (yyyy-ww).static DatetoLegacyDate(LocalDate d)Converts a LocalDate to a legacy java.time.LocalTimestatic DatetoLegacyDate(LocalDateTime ldt)Converts a java.time.LocalDateTime to a java.time.LocalTimestatic StringtoWeekCode(LocalDate date)Converts a date to its corresponding week code
-
-
-
Field Detail
-
WEEK_CODE_PATTERN
public static final String WEEK_CODE_PATTERN
- See Also:
- Constant Field Values
-
-
Method Detail
-
createLocalDate
public static LocalDate createLocalDate(String dateStr)
Creates a java.time.LocalTime based on a String representation- Parameters:
dateStr- the String (in the format ddMMyyyy)- Returns:
-
createLocalDate
public static LocalDate createLocalDate(String dateStr, String format)
Creates a java.time.LocalTime based on a String representation- Parameters:
dateStr- the Stringformat- the desired format- Returns:
-
createLocalDateTime
public static LocalDateTime createLocalDateTime(String dateTimeStr)
Creates a java.time.LocalDateTime based on a String representation- Parameters:
dateTimeStr- the String representation (ddMMyyyy HHmmss)- Returns:
-
createLocalDateTime
public static LocalDateTime createLocalDateTime(String dateTimeStr, String format)
Creates a java.time.LocalDateTime based on a String representation- Parameters:
dateTimeStr- the String representationformat- the desired format- Returns:
-
createLocalTime
public static LocalTime createLocalTime(String timeStr)
Creates a java.time.LocalTime based on a String representation- Parameters:
timeStr- the String representation- Returns:
-
createLocalTime
public static LocalTime createLocalTime(String timeStr, String format)
Creates a java.time.LocalTime based on a String representation- Parameters:
timeStr- the String representationformat- the desired format- Returns:
-
createZonedDateTime
public static ZonedDateTime createZonedDateTime(String dateTimeStr)
Create a ZonedDateTime based from a String, using the default format- Parameters:
dateTimeStr- the String to parse- Returns:
-
createZonedDateTime
public static ZonedDateTime createZonedDateTime(String dateTimeStr, String format)
Creates a ZonedDateTime from a String, using the provided format- Parameters:
dateTimeStr- the Stringformat- the format- Returns:
-
formatDate
public static String formatDate(LocalDate date, String format)
Formats a LocalDate according to the specified format- Parameters:
date- the dateformat- the format- Returns:
-
formatDateTime
public static String formatDateTime(LocalDateTime dateTime, String format)
Formats a LocalDateTime according to the specified format- Parameters:
dateTime- the DateTime to formatformat- the desired format- Returns:
-
formatJava8Date
public static String formatJava8Date(Class<?> clazz, Object value, String format)
Formats a Java 8 date/time/datetime based on the specified format- Parameters:
clazz- the class of the object to formatvalue- the valueformat- the desired format- Returns:
-
formatTime
public static String formatTime(LocalTime time, String format)
Formats a java.time.LocalTime according to the specified format- Parameters:
time- the objec to formatformat- the desired format- Returns:
-
formatZonedDateTime
public static String formatZonedDateTime(ZonedDateTime dateTime, String format)
Formats a ZonedDateTime according to the specified format- Parameters:
dateTime- the DateTime to formatformat- the desired format- Returns:
-
getLastWeekOfYear
public static int getLastWeekOfYear(int year)
Return the week number (1 - 53) of the last week of the specified year- Parameters:
year- the year- Returns:
-
getNextWeekCode
public static String getNextWeekCode(String weekCode)
Returns the next week code given an existing week code- Parameters:
weekCode- the week code- Returns:
-
getQuarter
public static int getQuarter(LocalDate date)
Returns the quarter of the year of a date, as an integer (1 to 4). Returns -1 in case the argument passed to this function is null- Parameters:
date- the date- Returns:
-
getStartDateOfWeek
public static LocalDate getStartDateOfWeek(String weekCode)
Translates a week code (yyyy-ww) to the starting day (this is taken to be a Monday) of that week- Parameters:
weekCode- the week code- Returns:
- the date
-
isJava8DateType
public static boolean isJava8DateType(Class<?> clazz)
Çhecks whether a class represents a Java 8 date or time type- Parameters:
clazz- the class- Returns:
-
isSupportedDateType
public static boolean isSupportedDateType(Class<?> clazz)
Checks if a class represents a supported Date time. This includes the Java 8 date types and the legacy java.time.LocalTime- Parameters:
clazz- the class to check- Returns:
-
isValidWeekCode
public static boolean isValidWeekCode(String weekCode)
Checks if a string represents a valid week code (yyyy-ww). An empty String is considered valid- Parameters:
weekCode- the week code- Returns:
-
toLegacyDate
public static Date toLegacyDate(LocalDate d)
Converts a LocalDate to a legacy java.time.LocalTime- Parameters:
d- the LocalDate to convert- Returns:
-
toLegacyDate
public static Date toLegacyDate(LocalDateTime ldt)
Converts a java.time.LocalDateTime to a java.time.LocalTime- Parameters:
d- the LocalDatetime to convert- Returns:
-
-