
com.maccloud.spring.converter.TimeConverter 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;
public class TimeConverter 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}$";
public static void main(String[] args) {
System.out.println(Time.valueOf("00:00").getTime());
System.out.println(Time.valueOf("00:00:00").getTime());
}
@Override
public Time convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
try {
if (source.matches(regex_HH)) {
return Time.valueOf(source + ":00:00");
} else if (source.matches(regex_HHmm)) {
return Time.valueOf(source + ":00");
} else if (source.matches(regex_HHmmss)) {
return Time.valueOf(source);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy