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

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

package com.maccloud.spring.converter;

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

import java.sql.Time;
import java.time.LocalTime;

public class LocalTimeConverter implements Converter {

    public static final String regex_HH = "^[0-9]{2}$";

    public static final String regex_HHmm = "^[0-9]{2}:[0-9]{2}$";

    public static final String regex_HHmmss = "^[0-9]{2}:[0-9]{2}:[0-9]{2}$";

    @Override
    public LocalTime convert(String source) {

        if (StringUtils.isBlank(source)) {
            return null;
        }

        try {
            if (source.matches(regex_HH)) {
                return Time.valueOf(source + ":00:00").toLocalTime();
            } else if (source.matches(regex_HHmm)) {
                return Time.valueOf(source + ":00").toLocalTime();
            } else if (source.matches(regex_HHmmss)) {
                return Time.valueOf(source).toLocalTime();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy