All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.rs.converters.InstantParamConverter Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package nl.vpro.rs.converters;

import java.time.Instant;
import java.time.ZoneId;
import java.util.regex.Pattern;

import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.Provider;

import nl.vpro.util.TimeUtils;


/**
 * @author Michiel Meeuwissen
 * @since 0.31
 */
@Provider
public class InstantParamConverter implements ParamConverter {

    private static final ZoneId ZONE_ID = ZoneId.of("Europe/Amsterdam");

    static InstantParamConverter INSTANCE = new InstantParamConverter();
    private static final Pattern NUMERIC = Pattern.compile("\\d+");

    @Override
    public Instant fromString(String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        // zo kunnen we het gewoon op de URL copy/pasten. + -> " " -> +....
        value = value.replaceAll(" ", "+");
        return TimeUtils.parse(value).orElse(null);
    }

    @Override
    public String toString(Instant value) {
        if (value == null) {
           return null;
        }
        return value.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy