Parses a formatted time string into a Date. @param s The string, in the form YYYYMMDDHHMMSS. @return The Date object. @throws TextParseExcetption The string was invalid.
(String s)
| 54 | * @throws TextParseExcetption The string was invalid. |
| 55 | */ |
| 56 | public static Date |
| 57 | parse(String s) throws TextParseException { |
| 58 | if (s.length() != 14) { |
| 59 | throw new TextParseException("Invalid time encoding: " + s); |
| 60 | } |
| 61 | |
| 62 | Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); |
| 63 | c.clear(); |
| 64 | try { |
| 65 | int year = Integer.parseInt(s.substring(0, 4)); |
| 66 | int month = Integer.parseInt(s.substring(4, 6)) - 1; |
| 67 | int date = Integer.parseInt(s.substring(6, 8)); |
| 68 | int hour = Integer.parseInt(s.substring(8, 10)); |
| 69 | int minute = Integer.parseInt(s.substring(10, 12)); |
| 70 | int second = Integer.parseInt(s.substring(12, 14)); |
| 71 | c.set(year, month, date, hour, minute, second); |
| 72 | } |
| 73 | catch (NumberFormatException e) { |
| 74 | throw new TextParseException("Invalid time encoding: " + s); |
| 75 | } |
| 76 | return c.getTime(); |
| 77 | } |
| 78 | |
| 79 | } |
no test coverage detected