Display LocalTime with HH:mm format

If you just use the standard Label in wicket it will print out LocalTime in the following
format HH:mm:ss this is not what we always want.

The new HourMinuteLabel will print LocalTime with HH:mm format where we need it.
This commit is contained in:
Nico Athanassiadis 2025-04-15 14:29:44 +02:00
parent cd09ec3f14
commit 3176a5430e

@ -0,0 +1,40 @@
package se.su.dsv.scipro.components;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
/**
* A label that displays a LocalTime in the format HH:mm.
*/
public class HourMinuteLabel extends Label {
public HourMinuteLabel(String id, IModel<LocalTime> localTimeIModel) {
super(id, localTimeIModel);
}
@SuppressWarnings("unchecked")
@Override
public <C> IConverter<C> getConverter(Class<C> type) {
if (type == LocalTime.class) {
return (IConverter<C>) new HourMinuteConverter();
}
return super.getConverter(type);
}
private static class HourMinuteConverter implements IConverter<LocalTime> {
@Override
public LocalTime convertToObject(String value, Locale locale) throws ConversionException {
throw new UnsupportedOperationException();
}
@Override
public String convertToString(LocalTime value, Locale locale) {
return DateTimeFormatter.ofPattern("HH:mm", locale).format(value);
}
}
}