private boolean isDatePatternHourly(String datePattern) { DateTimeFormatter formatter = DateTimeFormat.forPattern(datePattern); LocalDateTime refDateTime = new LocalDateTime(2017, 01, 01, 10, 0, 0); String refDateTimeString = refDateTime.toString(formatter); LocalDateTime refDateTimeAtStartOfDay = refDateTime.withHourOfDay(0); String refDateTimeStringAtStartOfDay = refDateTimeAtStartOfDay.toString(formatter); return !refDateTimeString.equals(refDateTimeStringAtStartOfDay); }
ldt = ldt.withHourOfDay(3);
private boolean isDatePatternHourly(String datePattern) { DateTimeFormatter formatter = DateTimeFormat.forPattern(datePattern); LocalDateTime refDateTime = new LocalDateTime(2017, 01, 01, 10, 0, 0); String refDateTimeString = refDateTime.toString(formatter); LocalDateTime refDateTimeAtStartOfDay = refDateTime.withHourOfDay(0); String refDateTimeStringAtStartOfDay = refDateTimeAtStartOfDay.toString(formatter); return !refDateTimeString.equals(refDateTimeStringAtStartOfDay); }
/** * Implemented based on this answer: http://stackoverflow.com/a/5451245/297710 */ private DateTime fromAllDayMillis(long millis) { String msgLog = "millis=" + millis; DateTime fixed; try { DateTime utcDate = new DateTime(millis, DateTimeZone.UTC); LocalDateTime ldt = new LocalDateTime() .withYear(utcDate.getYear()) .withMonthOfYear(utcDate.getMonthOfYear()) .withDayOfMonth(utcDate.getDayOfMonth()) .withMillisOfDay(0); int hour = 0; while (zone.isLocalDateTimeGap(ldt)) { Log.v("fixTimeOfAllDayEvent", "Local Date Time Gap: " + ldt + "; " + msgLog); ldt = ldt.withHourOfDay(++hour); } fixed = ldt.toDateTime(zone); msgLog += " -> " + fixed; if (BuildConfig.DEBUG) { Log.v("fixTimeOfAllDayEvent", msgLog); } } catch (org.joda.time.IllegalInstantException e) { throw new org.joda.time.IllegalInstantException(msgLog + " caused by: " + e); } return fixed; }
public static DateTime newDateTime(LocalDate localDate, int hour, int minute, int second) { final DateTimeZone zone = DateTimeSourceUtil.timeZone(); LocalDateTime localDateTime = new LocalDateTime(zone) .withYear(localDate.getYear()) .withMonthOfYear(localDate.getMonthOfYear()) .withDayOfMonth(localDate.getDayOfMonth()) .withHourOfDay(hour) .withMinuteOfHour(minute) .withSecondOfMinute(second) .withMillisOfSecond(0); if (zone.isLocalDateTimeGap(localDateTime)) { localDateTime = localDateTime.withHourOfDay(hour + 1); } return localDateTime.toDateTime(); }
/** * from http://stackoverflow.com/a/5451245/297710 */ private void reproducedTimeZoneOffsetTransitionException() { final DateTimeZone dateTimeZone = DateTimeZone.forID("CET"); LocalDateTime localDateTime = new LocalDateTime(dateTimeZone) .withYear(2011) .withMonthOfYear(3) .withDayOfMonth(27) .withHourOfDay(2); // this is just here to illustrate I'm solving the problem; // don't need in operational code try { DateTime myDateBroken = localDateTime.toDateTime(dateTimeZone); fail("No exception for " + localDateTime + " -> " + myDateBroken); } catch (IllegalArgumentException iae) { Log.v(TAG, "Sure enough, invalid instant due to time zone offset transition: " + localDateTime); } if (dateTimeZone.isLocalDateTimeGap(localDateTime)) { localDateTime = localDateTime.withHourOfDay(3); } DateTime myDate = localDateTime.toDateTime(dateTimeZone); Log.v(TAG, "No problem with this date: " + myDate); }
private boolean matches(HistoKey k, Map<String, Object> map, QueryParams query) { if (query.getHorizon() != null && !query.getHorizon().equals(k.getHorizon())) { return false; } if (query.getDayTimeFrom() != null && query.getDayTimeTo() != null) { LocalDateTime check = new DateTime(k.getDateTime()).toLocalDateTime(); LocalDateTime from = new DateTime(query.getDayTimeFrom()).toLocalDateTime(); LocalDateTime to = new DateTime(query.getDayTimeTo()).toLocalDateTime(); LocalDateTime start = new DateTime(k.getDateTime()).toLocalDateTime().withHourOfDay(from.getHourOfDay()) .withMinuteOfHour(from.getMinuteOfHour()).withSecondOfMinute(from.getSecondOfMinute()); LocalDateTime end = new DateTime(k.getDateTime()).toLocalDateTime().withHourOfDay(to.getHourOfDay()) .withMinuteOfHour(to.getMinuteOfHour()).withSecondOfMinute(to.getSecondOfMinute()); if (check.isAfter(end) || check.isBefore(start)) { return false; } } if (query.getForecastTime() >= 0) { if (k.getForecastDistance() != query.getForecastTime()) { return false; } } return true; }