今天在处理nginx生成的access log时,发现时间是类似这种格式的:15/Oct/2019:10:20:42,以为只要简单的使用SimpleDateFormat处理即可,自己写了个处理格式dd/MMM/yyyy:HH:mm:ss ,可谁知直接执行竟然报错:java.text.ParseException: Unparseable date: "15/Oct/2019:10:20:42"
at java.text.DateFormat.parse(DateFormat.java:366)
at cn.hxj.task.AccessLogDayReport.getLogMap(AccessLogDayReport.java:55)
at cn.hxj.task.AccessLogDayReport.runOnce(AccessLogDayReport.java:39)
at cn.hxj.task.AccessLogDayReport.main(AccessLogDayReport.java:70)
代码如下:SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
Date date= sdf2.parse(time);
实际只要在构建SimpleDateFormat时加上, Locale.ENGLISH即可正常识别,即:SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);