Here you can find the source of dateOfAny(final String value)
Parameter | Description |
---|---|
value | yyyy-MM-dd [hh:mm:ss.SSS][AM|PM] |
public static Date dateOfAny(final String value)
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**// w ww .j av a 2 s . c om * * @param value yyyy-MM-dd [hh:mm:ss.SSS][AM|PM] * @return */ public static Date dateOfAny(final String value) { if (value == null || value.trim().length() == 0) return null; Calendar calendat = Calendar.getInstance(); final String datePattern = "[0-9]{4}-([0-1]?[0-9])-(([0-3]?[0-9])|([3][0-1]))"; final String timePattern = "(([0-1]?[0-9])|([2][0-3]))((:([0-5]){0,1}([0-9]){1}){1,2})"; final String timesPattern = " ([0-1]?[0-9]){1}(((:([0-5]){0,1}([0-9]){1}){1,2}) (([AP]M)|([ap]m)))?"; Pattern p = Pattern.compile(datePattern); Matcher m = p.matcher(value); m.find(); final String date_String = m.group(); if (date_String != null) { final String[] test = date_String.split("-"); if (test.length == 3) { calendat.set(Calendar.YEAR, Integer.parseInt(test[0].trim())); calendat.set(Calendar.MONTH, Integer.parseInt(test[1].trim()) - 1); calendat.set(Calendar.DAY_OF_MONTH, Integer.parseInt(test[2].trim())); } } if ((value.indexOf("pm") != -1) || (value.indexOf("PM") != -1) || (value.indexOf("am") != -1) || (value.indexOf("AM") != -1)) { p = Pattern.compile(timesPattern); m = p.matcher(value); m.find(); String time_String = m.group(); if (time_String != null) { final String apm = time_String.substring(time_String.trim().indexOf(" ") + 1); if (apm.trim().equalsIgnoreCase("pm")) { time_String = time_String.trim().substring(0, time_String.trim().indexOf(" ")); final String[] test = time_String.split(":"); if (test.length == 3) { calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()) + 12); calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim())); calendat.set(Calendar.SECOND, Integer.parseInt(test[2].trim())); } if (test.length == 2) { calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()) + 12); calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim())); calendat.set(Calendar.SECOND, 0); } } else { final String[] test = time_String.split(":"); calendat = setTime(calendat, test); } } } else { p = Pattern.compile(timePattern); m = p.matcher(value); final Boolean isFind = m.find(); if (isFind == true) { final String time_String = m.group(); if (time_String != null) { final String[] test = time_String.split(":"); calendat = setTime(calendat, test); } } else { calendat.set(Calendar.HOUR_OF_DAY, 0); calendat.set(Calendar.MINUTE, 0); calendat.set(Calendar.SECOND, 0); } } return calendat.getTime(); } private static Calendar setTime(final Calendar calendat, final String[] test) { if (test.length == 3) { calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim())); calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim())); calendat.set(Calendar.SECOND, Integer.parseInt(test[2].trim())); } if (test.length == 2) { calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim())); calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim())); calendat.set(Calendar.SECOND, 0); } return calendat; } }