记录一下
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);
}
}