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

com.jslsolucoes.spring.converter.LocalTimeConverter Maven / Gradle / Ivy

There is a newer version: 1.0.34
Show newest version
package com.jslsolucoes.spring.converter;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

public class LocalTimeConverter implements Converter {

    private static final Logger logger = LoggerFactory.getLogger(LocalTimeConverter.class);

    @Override
    public LocalTime convert(String value) {
	if (StringUtils.isEmpty(value))
	    return null;
	List patterns = Arrays.asList("HH:mm:ss", "HH:mm");
	for (String pattern : patterns) {
	    try {
		return LocalTime.parse(value, DateTimeFormatter.ofPattern(pattern));
	    } catch (DateTimeParseException e) {
		logger.debug("pattern {} does not match with value {}", pattern, value);
	    }
	}
	throw new RuntimeException("Could not parse " + value + " to java.time.LocalDateTime");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy