Date时间处理

记录一下
Jul  6 09:55:02 转为yyyy-MM-dd hh:mm:ss(年 我按照2019年 -> 设置默认为1970)
public class Method {

    public static void main(String[] args) throws ParseException {
        Method m = new Method();
        m.action("Jul  6 09:55:02 localhost sshd[27693]: Accepted publickey for root from 20.21.1.167 port 57454 ssh2: RSA SHA256:O0mZ30yZcvlZJYj7UvDGK5V2kZLmdUENBxijbnIg818");

    }

    private void action(String str) throws ParseException {
        Map<String,String> map = new HashMap<>();
        String one = " localhost sshd[";
        String two = "]: ";
        String three = "Accepted";
        String four = " publickey for ";
        String five = " from ";
        String six = " port ";

        map.put("1",convertDate(str.substring(0,str.indexOf(one))));
        map.put("2", str.substring(str.indexOf(one) + one.length(), str.indexOf(two)));
        map.put("3", three);
        map.put("4", str.substring(str.indexOf(four) + four.length(), str.indexOf(five)));
        map.put("5", str.substring(str.indexOf(five) + five.length(), str.indexOf(six)));

        map.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
    }

    public String convertDate(String dateParam) throws ParseException {
        dateParam = dateParam + " CST 2019";
        Date createDate = new SimpleDateFormat("MMM  dd hh:mm:ss z yyyy", Locale.US).parse(dateParam);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(createDate);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值