public static void main(String[] args) { String stringDate = "1501730332"; try { if( Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", stringDate) ) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(simpleDateFormat.parse(stringDate)); } else if( Pattern.matches("\\d+", stringDate) ) { System.out.println(new Date(Long.valueOf(stringDate))); } else if( Pattern.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+Z", stringDate) ){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z"); stringDate = stringDate.replace("Z", " UTC"); System.out.println(simpleDateFormat.parse(stringDate)); } } catch (ParseException e) { e.printStackTrace(); } }
java正则表达式检测并转换Date
最新推荐文章于 2023-05-25 10:34:18 发布