This is a Date Utility with two purposes:
- Parse String to Date according to a target class, and the pattern strings.
- Format Date to String based on a pattern string.
- If you are using Gradle just add the following dependency to your
build.gradle
.
implementation "com.github.joutvhu:date-parser:1.0.0"
- Or add the following dependency to your
pom.xml
if you are using Maven.
<dependency>
<groupId>com.github.joutvhu</groupId>
<artifactId>date-parser</artifactId>
<version>1.0.0</version>
</dependency>
-
withConvertor(Class<T> typeOfConvertor, Convertor<T> convertor)
orwithConvertor(Convertor<T> convertor)
used to add a custom convertor to theDateFormatter
.- Use these methods to add a
Convertor
.
- Use these methods to add a
-
withLocale(Locale defaultLocale)
used to set defaultjava.util.Locale
, if this value is null the default value isLocale.getDefault()
. -
withZone(TimeZone defaultZone)
used to set defaultjava.util.TimeZone
, if this value is null the default value isTimeZone.getDefault()
. -
withWeekFields(WeekFields defaultWeekFields)
used to set defaultjava.time.temporal.WeekFields
, if this value is null the default value isWeekFields.of(locale)
. -
withStrategyFactory(StrategyFactory strategyFactory)
used to overrideStrategyFactory
.- Use this method to add, update or delete a
Strategy
.
- Use this method to add, update or delete a
-
parse(Class<T> type, String value, String... patterns)
used to parse string to date.type
is the type of target object you want to get.value
is the input string you want to convert to the target object.patterns
are the possible formats of the input string.
-
format(T object, String pattern)
used to format an object to a target string.object
is the input object you want to convert to string.pattern
is the format of the target string.
-
DateParser.formatter()
will be return a newDateFormatter
. -
DateParser.parse(Class<T> type, String value, String... patterns)
is equivalent toDateParser.formatter().parse(Class<T> type, String value, String... patterns)
. -
DateParser.format(T object, String pattern)
is equivalent toDateParser.formatter().format(T object, String pattern)
.
- Parse Date
Date date = DateParser.formatter().parse(Date.class, "2021-06-27 21:52:25.408", "yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime localDateTime = DateParser.parse(LocalDateTime.class, "2021-06-27 21:52:25.408", "yyyy-MM-dd HH:mm:ss.SSS");
Instant instant = DateParser.parse(Instant.class, "2021-06-28T02:22:48.780101Z", "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DayOfWeek dayOfWeek = DateParser.formatter().withWeekFields(WeekFields.ISO).parse(DayOfWeek.class, "Sun", "E");
- Format Date
String date = DateParser.format(new Date(), "MMM dd, yyyy hh:mm:ss a");
String instant = DateParser.format(Instant.now(), "yyyy-MM-dd'T'HH:mm:ss.SSSX");
String yearMonth = DateParser.format(YearMonth.of(2021, Month.DECEMBER), "Mo yyyy");