DateUtils { @SuppressWarnings({ "deprecation", "unused" }) public static Date str_to_time(String str) { int length = str.length(); int was_cut; Date time = new Date(0); int field_length; int digits; int year_length = 0; int add_hours = 0, start_loop; long not_zero_date, allow_space; boolean is_internal_format; int pos; int last_field_pos = 0; int cur_pos = 0; int i; int date[] = new int[MAX_DATE_PARTS]; int date_len[] = new int[MAX_DATE_PARTS]; int number_of_fields; boolean found_delimitier = false, found_space = false; int frac_pos; int frac_len; was_cut = 0; str = str.trim(); int end = str.length(); if (str.isEmpty() || !isdigit(str.charAt(0))) { was_cut = 1; return null; } is_internal_format = false; int format_position[] = internal_format_positions; for (pos = 0; pos < str.length() && (isdigit(str.charAt(pos)) || str.charAt(pos) == 'T'); pos++) ; digits = pos; start_loop = 0; date_len[format_position[0]] = 0; if (pos == str.length() || str.charAt(pos) == '.') { year_length = (digits == 4 || digits == 8 || digits >= 14) ? 4 : 2; field_length = year_length; is_internal_format = true; format_position = internal_format_positions; } else { if (format_position[0] >= 3) { while (pos < end && !my_isspace(str.charAt(pos))) pos++; while (pos < end && !isdigit(str.charAt(pos))) pos++; if (pos == end) { date[0] = date[1] = date[2] = date[3] = date[4] = 0; start_loop = 5; } } field_length = format_position[0] == 0 ? 4 : 2; } i = max(format_position[0], format_position[1]); if (i < format_position[2]) i = format_position[2]; allow_space = ((1 << i) | (1 << format_position[6])); allow_space &= (1 | 2 | 4 | 8); not_zero_date = 0; for (i = start_loop; i < MAX_DATE_PARTS - 1 && cur_pos != end && isdigit(str.charAt(cur_pos)); i++) { int start = cur_pos; int tmp_value = (str.charAt(cur_pos++) - '0'); boolean scan_until_delim = !is_internal_format && ((i != format_position[6])); while (cur_pos != end && isdigit(str.charAt(cur_pos)) && (scan_until_delim || (--field_length != 0))) { tmp_value = tmp_value * 10 + (str.charAt(cur_pos) - '0'); cur_pos++; } date_len[i] = (cur_pos - start); if (tmp_value > 999999) { was_cut = 1; return null; } date[i] = tmp_value; not_zero_date |= tmp_value; field_length = format_position[i + 1] == 0 ? 4 : 2; if ((last_field_pos = cur_pos) == end) { i++; break; } if (i == format_position[2] && str.charAt(cur_pos) == 'T') { cur_pos++; continue; } if (i == format_position[5]) { if (str.charAt(cur_pos) == '.') { cur_pos++; field_length = 6; } continue; } while (cur_pos != end && (my_ispunct(str.charAt(cur_pos)) || my_isspace(str.charAt(cur_pos)))) { if (my_isspace(str.charAt(cur_pos))) { if (!((allow_space & (1 << i)) != 0)) { was_cut = 1; return null; } found_space = true; } cur_pos++; found_delimitier = true; } if (i == format_position[6]) { i++; if (format_position[7] != 255) { if (cur_pos + 2 <= end && (str.charAt(cur_pos + 1) == 'M' || str.charAt(cur_pos) + 1 == 'm')) { if (str.charAt(cur_pos) == 'p' || str.charAt(cur_pos) == 'P') add_hours = 12; else if (str.charAt(cur_pos) != 'a' || str.charAt(cur_pos) != 'A') continue; str += 2; while (cur_pos != end && my_isspace(str.charAt(cur_pos))) cur_pos++; } } } last_field_pos = cur_pos; } cur_pos = last_field_pos; number_of_fields = i - start_loop; while (i < MAX_DATE_PARTS) { date_len[i] = 0; date[i++] = 0; } if (!is_internal_format) { year_length = date_len[format_position[0]]; if (!(year_length != 0)) { was_cut = 1; return null; } time.setYear(date[format_position[0]] - 1900); time.setMonth(date[format_position[1]] - 1); time.setDate(date[format_position[2]]); time.setHours(date[format_position[3]]); time.setMinutes(date[format_position[4]]); time.setSeconds(date[format_position[5]]); frac_pos = format_position[6]; frac_len = date_len[frac_pos]; if (format_position[7] != 255) { if (time.getHours() > 12) { was_cut = 1; return null; } time.setHours(time.getHours() % 12 + add_hours); } } else { time.setYear(date[0] - 1900); time.setMonth(date[1] - 1); time.setDate(date[2]); time.setHours(date[3]); time.setMinutes(date[4]); time.setSeconds(date[5]); } if (year_length == 2 && (not_zero_date != 0)) time.setYear(time.getYear() + (time.getYear() < YY_PART_YEAR ? 2000 : 1900)); if (number_of_fields < 3 || time.getYear() > 9999 || time.getMonth() > 12 || time.getDate() > 31 || time.getHours() > 23 || time.getMinutes() > 59 || time.getMinutes() > 59) { if (!(not_zero_date != 0)) { for (; cur_pos != end; cur_pos++) { if (!my_isspace(str.charAt(cur_pos))) { not_zero_date = 1; break; } } } return null; } if (check_date(time, not_zero_date != 0, was_cut)) return null; for (; cur_pos != end; cur_pos++) { if (!my_isspace(str.charAt(cur_pos))) { was_cut = 1; break; } } return time; }  @SuppressWarnings({ "deprecation", "unused" }) static Date str_to_time(String str); static boolean check_date(Date time, boolean not_zero_date, int was_cut); static boolean my_ispunct(char v); static int max(int i, int j); static boolean my_isspace(char v); static boolean isdigit(char v); static final int TIME_DATETIME_ONLY; static int internal_format_positions; static final int MAX_DATE_PARTS; static final int YY_PART_YEAR; }
@Test public void test_day() { String time = "2014-03-26"; Date date = DateUtils.str_to_time(time); System.out.println(date); }
@Test public void test_date() { String time = "2014-03-26 12:12:12"; Date date = DateUtils.str_to_time(time); System.out.println(date); }
@Test public void test_timestamp() { String time = "2014-03-26 12:12:12.339"; Date date = DateUtils.str_to_time(time); System.out.println(date); }
@Test public void test_time() { String time = "200801"; Date date = DateUtils.str_to_time(time); System.out.println(date); }