List of usage examples for org.joda.time.format DateTimeFormatter getDefaultYear
public int getDefaultYear()
From source file:me.vertretungsplan.parser.ParserUtils.java
License:Mozilla Public License
static LocalDateTime parseDateTime(String string) { if (string == null) return null; reinitIfNeeded();//w w w. j av a 2 s. c o m string = string.replace("Stand:", "").replace("Import:", "").trim(); int i = 0; for (DateTimeFormatter f : dateTimeFormatters) { try { LocalDateTime dt = f.parseLocalDateTime(string); if (dateTimeFormats[i].contains("yyyy")) { return dt; } else { Duration currentYearDifference = abs(new Duration(DateTime.now(), dt.toDateTime())); Duration lastYearDifference = abs(new Duration(DateTime.now(), dt.minusYears(1).toDateTime())); Duration nextYearDifference = abs(new Duration(DateTime.now(), dt.plusYears(1).toDateTime())); if (lastYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() - 1).parseLocalDateTime(string); } else if (nextYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() + 1).parseLocalDateTime(string); } else { return dt; } } } catch (IllegalArgumentException e) { // Does not match this format, try the next one } i++; } // Does not match any known format :( return null; }
From source file:me.vertretungsplan.parser.ParserUtils.java
License:Mozilla Public License
static LocalDate parseDate(String string) { if (string == null) return null; reinitIfNeeded();/*from w ww . j av a2 s . co m*/ string = string.replace("Stand:", "").replace("Import:", "").replaceAll(", Woche [A-Z]", "").trim(); int i = 0; for (DateTimeFormatter f : dateFormatters) { try { LocalDate d = f.parseLocalDate(string); if (dateFormats[i].contains("yyyy")) { return d; } else { Duration currentYearDifference = abs(new Duration(DateTime.now(), d.toDateTimeAtCurrentTime())); Duration lastYearDifference = abs( new Duration(DateTime.now(), d.minusYears(1).toDateTimeAtCurrentTime())); Duration nextYearDifference = abs( new Duration(DateTime.now(), d.plusYears(1).toDateTimeAtCurrentTime())); if (lastYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() - 1).parseLocalDate(string); } else if (nextYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() + 1).parseLocalDate(string); } else { return d; } } } catch (IllegalArgumentException e) { // Does not match this format, try the next one } i++; } // Does not match any known format :( return null; }