java将日期中的斜线或中文改成统一日期格式 yyyy-MM-dd HH:mm:ss

前言

最近在优化代码方面,因为之前在字符串中截取日期,日期的格式样式不统一。

日期格式

2019/6/14 14:00:00
2019/03/4 1:00:00
2019年8月4日12:00

要求改成统一格式 :
2019-06-08 12:00:00

把斜线改成横线

	   // 斜线变成横线格式
       String strDate = "2019/6/14 14:00"; // 传入的日期 
       Date str = DateUtil.stringToDate(strDate.replace("/", "-", DateExtendUtil.FULL);

	   // 中文改成横线格式
       String strDate = "2019年6月14日 14:00 "; // 传入的日期 
	   String str = strDate.replaceAll("[年,月]", "-");
	   Date str = DateUtil.stringToDate(str.replace("[日]", "-"+ ":00" , DateExtendUtil.FULL);

时间处理类

public class DateExtendUtil extends DateUtil {
    public static final String FULL = "yyyy-MM-dd HH:mm:ss";
    private static final long ONE_DAY = 24 * 60 * 60 * 1000L;
    
    public static Date stringToDate(String strDate, String formartStr) {
        Date date = null;
        if (formartStr != null && !"".equals(formartStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat(formartStr);
            try {
                date = sdf.parse(strDate);
            } catch (Exception ignored) {
            }
        }

        return date;
    }
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值