-
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.
- Loading branch information
1 parent
8d94b47
commit fa9cec3
Showing
3 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...adapters/web/src/main/java/it/mulders/traqqr/web/faces/MeasurementTimestampConverter.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,35 @@ | ||
package it.mulders.traqqr.web.faces; | ||
|
||
import jakarta.faces.component.UIComponent; | ||
import jakarta.faces.context.FacesContext; | ||
import jakarta.faces.convert.Converter; | ||
import jakarta.faces.convert.FacesConverter; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.util.Locale; | ||
|
||
@FacesConverter("traqqr.MeasurementTimestampConverter") | ||
public class MeasurementTimestampConverter implements Converter<OffsetDateTime> { | ||
private static final DateTimeFormatter FORMAT = new DateTimeFormatterBuilder() | ||
.appendPattern("dd MMM uuuu, HH:mm.ss") | ||
.toFormatter(Locale.ROOT); | ||
|
||
@Override | ||
public OffsetDateTime getAsObject(FacesContext context, UIComponent component, String value) { | ||
if (context == null || component == null) { | ||
throw new NullPointerException(); | ||
} | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
return OffsetDateTime.parse(value, FORMAT); | ||
} | ||
|
||
@Override | ||
public String getAsString(FacesContext context, UIComponent component, OffsetDateTime value) { | ||
return value.format(FORMAT); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...ters/web/src/test/java/it/mulders/traqqr/web/faces/MeasurementTimestampConverterTest.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,25 @@ | ||
package it.mulders.traqqr.web.faces; | ||
|
||
import jakarta.faces.context.FacesContext; | ||
import org.assertj.core.api.WithAssertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.*; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class MeasurementTimestampConverterTest implements WithAssertions { | ||
private final MeasurementTimestampConverter converter = new MeasurementTimestampConverter(); | ||
|
||
private static final OffsetDateTime INPUT = OffsetDateTime.of( | ||
LocalDate.of(2025, Month.JANUARY, 7), | ||
LocalTime.of(22, 14, 13, 0), | ||
ZoneOffset.UTC | ||
); | ||
|
||
@Test | ||
void should_format_measurement_timestamp() { | ||
assertThat(converter.getAsString(null, null, INPUT)).isEqualTo("07 Jan 2025, 22:14.13"); | ||
} | ||
} |