-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new utility methods for DateUtils
- Loading branch information
1 parent
ad99231
commit bf120b4
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/YearWeek.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.shortthirdman.primekit.essentials.common; | ||
|
||
import java.text.MessageFormat; | ||
import java.time.DateTimeException; | ||
import java.time.LocalDate; | ||
import java.time.chrono.Chronology; | ||
import java.time.chrono.IsoChronology; | ||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Objects; | ||
|
||
|
||
public record YearWeek(int year, int week) { | ||
|
||
public static YearWeek from(TemporalAccessor temporal) { | ||
Objects.requireNonNull(temporal, "temporal"); | ||
try { | ||
if (!IsoChronology.INSTANCE.equals(Chronology.from(temporal))) { | ||
temporal = LocalDate.from(temporal); | ||
} | ||
return new YearWeek(temporal.get(ChronoField.YEAR), temporal.get(ChronoField.ALIGNED_WEEK_OF_YEAR)); | ||
} catch (DateTimeException ex) { | ||
String mf = MessageFormat.format("Unable to obtain YearWeek from TemporalAccessor: {0} of type {1}", temporal, temporal.getClass().getName()); | ||
throw new DateTimeException(mf, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MessageFormat.format("{0}-{1}", year, week); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters