Spring Boot中前台日期String转后台Timestamp

编写转换器转换为TimeStamp

@Configuration
public class StringToTimestampConverter implements Converter<String,Timestamp> {

    private final SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Timestamp convert(String text) {
        Timestamp timestamp=null;
        if(StringUtils.hasText(text)){
            text = text.trim();
            boolean isLong = true;
            try {
                long millisecond = Long.parseLong(text);
                timestamp=new Timestamp(millisecond);
            }catch(Exception e){
                isLong = false;
            }
            if(!isLong) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat(FormatUtils.getFormatter(text.length()));
                    String format = defaultDateFormat.format(sdf.parse(text));
                    System.out.println(format);
                } catch (ParseException var3) {
                    throw new IllegalArgumentException("Could not parse date: " + var3.getMessage(), var3);
                }
            }
        }
        return timestamp;
    }
}
public class FormatUtils {

    public static String getFormatter(int length) {
        String formatter = "yyyy-MM-dd";
        if (length == 16) {
            formatter = "yyyy-MM-dd HH:mm";
        } else if (length == 19) {
            formatter = "yyyy-MM-dd HH:mm:ss";
        } else if (length == 20) {
            formatter = "yyyy-MM-dd HH:mm:ss.S";
        } else if (length == 21) {
            formatter = "yyyy-MM-dd HH:mm:ss.SS";
        } else if (length == 22) {
            formatter = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        return formatter;
    }
}

拦截器

@Configuration
public class WebConfigBeans {

    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;

    /**
     * 增加字符串转timStamp的功能
     */
    @PostConstruct
    public void initEditableValidation() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer
                    .getConversionService();
            genericConversionService.addConverter(new StringToTimestampConverter());
        }

    }
}

@PostConstruct

@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

@StringToDate

@Configuration
public class StringToTimestampConverter implements Converter<String,Date> {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
    private static final String shortDateFormat2 = "yyyy/MM/dd";
    @Override
    public Date convert(String source) {
        if (StringUtils.isBlank(source)) {
            return null;
        }
        source = source.trim();
        try {
            SimpleDateFormat formatter;
            if (source.contains("-")) {
                if (source.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat);
                }
                Date dtDate = formatter.parse(source);
                return dtDate;
            } else if (source.contains("/")) {
                if (source.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat2);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat2);
                }
                Date dtDate = formatter.parse(source);
                return dtDate;
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("parser %s to Date fail", source));
        }

        throw new RuntimeException(String.format("parser %s to Date fail", source));

    }
}

设置默认页面

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/hello.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值