private DateTime parseDateTime(final String scheduleDate, final String scheduleTime) { // scheduleTime: 12,00,pm,PDT final String[] parts = scheduleTime.split(",", -1); int hour = Integer.parseInt(parts[0]); final int minutes = Integer.parseInt(parts[1]); final boolean isPm = parts[2].equalsIgnoreCase("pm"); final DateTimeZone timezone = parts[3].equals("UTC") ? DateTimeZone.UTC : DateTimeZone.getDefault(); // scheduleDate: 02/10/2013 DateTime day = null; if (scheduleDate == null || scheduleDate.trim().length() == 0) { day = new LocalDateTime().toDateTime(); } else { day = DateTimeFormat.forPattern("MM/dd/yyyy") .withZone(timezone).parseDateTime(scheduleDate); } hour %= 12; if (isPm) { hour += 12; } final DateTime firstSchedTime = day.withHourOfDay(hour).withMinuteOfHour(minutes).withSecondOfMinute(0); return firstSchedTime; }
case HOURS: _dateTimeTruncate = (dateTime) -> _outputDateTimeFormatter .print(dateTime.withHourOfDay((dateTime.getHourOfDay() / sz) * sz).hourOfDay().roundFloorCopy()); break; case DAYS:
case MINUTES: case SECONDS: dt = dt.withHourOfDay(0); dt = dt.withMinuteOfHour(0); dt = dt.withSecondOfMinute(0);
private DateTime extractAndUpdateTime(DateTime inputDateTime) { DateTime resultDateTime = inputDateTime.withSecondOfMinute(0).withMillisOfSecond(0); if (dateTime.equals("") || dateTime.contains("now")) return resultDateTime; int hour = 0; int minute = 0; Pattern p = Pattern.compile("(\\d{1,2}):(\\d{2})([a|p]m)?(.*)"); Matcher m = p.matcher(dateTime); if (m.matches()) { hour = Integer.parseInt(m.group(1)); minute = Integer.parseInt(m.group(2)); String middayModifier = m.group(3); if (middayModifier != null && middayModifier.equals("pm")) hour = (hour + 12) % 24; dateTime = m.group(4); } if (dateTime.contains("noon")) { hour = 12; dateTime = dateTime.replace("noon", ""); } else if (dateTime.contains("teatime")) { hour = 16; dateTime = dateTime.replace("teatime", ""); } else if (dateTime.contains("midnight")) dateTime = dateTime.replace("midnight", ""); return resultDateTime.withHourOfDay(hour).withMinuteOfHour(minute); }
assertFunction("date_trunc('hour', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session)); result = result.withHourOfDay(0); assertFunction("date_trunc('day', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session)); assertFunction("date_trunc('hour', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result)); result = result.withHourOfDay(0); assertFunction("date_trunc('day', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));
@Test public void testHourMinuteKeywords() { String noonTimestamp = "noon"; String teatimeTimestamp = "teatime"; String midnightTimestamp = "midnight"; Assert.assertEquals(DateTimeParser.parse(noonTimestamp), referenceDateTime().withHourOfDay(12).withMinuteOfHour(0)); Assert.assertEquals(DateTimeParser.parse(teatimeTimestamp), referenceDateTime().withHourOfDay(16).withMinuteOfHour(0)); Assert.assertEquals(DateTimeParser.parse(midnightTimestamp), referenceDateTime().withHourOfDay(0).withMinuteOfHour(0)); }
private static DateTime referenceDateTime() { return new DateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0); }
@Test public void testRegularHourMinute() { String hourMinuteTimestamp = "12:24"; String hourMinuteWithAm = "9:13am"; String hourMinuteWithPm = "09:13pm"; Assert.assertEquals(DateTimeParser.parse(hourMinuteTimestamp), referenceDateTime().withHourOfDay(12).withMinuteOfHour(24)); Assert.assertEquals(DateTimeParser.parse(hourMinuteWithAm), referenceDateTime().withHourOfDay(9).withMinuteOfHour(13)); Assert.assertEquals(DateTimeParser.parse(hourMinuteWithPm), referenceDateTime().withHourOfDay(21).withMinuteOfHour(13)); }
@Test public void testComplexFormats() { testFormat("12:24 yesterday", nowDateTime().minusDays(1).withHourOfDay(12).withMinuteOfHour(24)); testFormat("12:24 tomorrow", nowDateTime().plusDays(1).withHourOfDay(12).withMinuteOfHour(24)); testFormat("12:24 today", nowDateTime().withHourOfDay(12).withMinuteOfHour(24)); testFormat("noon 12/30/2014", nowDateTime().withDate(2014, 12, 30).withHourOfDay(12).withMinuteOfHour(0)); int currentYear = referenceDateTime().getYear(); testFormat("15:45 12/30/14", new DateTime(2014, 12, 30, 15, 45, 0, 0)); testFormat("teatime 12/30/2014", new DateTime(2014, 12, 30, 16, 0, 0, 0)); testFormat("midnight Jul 30", new DateTime(currentYear, 07, 30, 0, 0, 0, 0)); testFormat("Jul 30, 2013", new DateTime(2013, 07, 30, 0, 0, 0, 0)); testFormat("Jul 30", new DateTime(currentYear, 07, 30, 0, 0, 0, 0)); testFormat("20141230", new DateTime(2014, 12, 30, 0, 0, 0, 0)); }
DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd/MM/yyyy" ); // Usually I specify a Locale as well. But in this case, no need (no names of days or months). DateTimeZone customerTimeZone = DateTimeZone.UTC; String input = "25/05/2014"; DateTime customerDateTime = formatter.withZone( customerTimeZone ).parseDateTime( input ); DateTime customerDateTimeAtFive = customerDateTime.withHourOfDay( 5 ); // Using customerTimeZone.
DateTime dt = new DateTime(DateTimeZone.forID("US/Eastern")); DateTime target = dt .withHourOfDay(15) .withMinuteOfHour(0) .withSecondOfMinute(0) .withMillisOfSecond(0); if (target.isBefore(dt)) { target = target.plusDays(1); } System.out.println(target.getMillis() - dt.getMillis());
private Interval extractSearchInterval(final Entity contextEntity) { Date from = contextEntity.getDateField(BalanceContextFields.FROM_DATE); Date to = contextEntity.getDateField(BalanceContextFields.TO_DATE); DateTime toMidnight = new DateTime(to).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59); return new Interval(new DateTime(from), toMidnight); }
/** * Parse the to date query param. */ private DateTime parseFromDate(String fromDate) { // Default to the last 30 days DateTime defaultFrom = new DateTime().withZone(DateTimeZone.UTC).minusDays(30).withHourOfDay(0) .withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0); return parseDate(fromDate, defaultFrom, true); }
/** * Parse the to date query param. */ private DateTime parseFromDate(String fromDate) { // Default to the last 30 days DateTime defaultFrom = new DateTime().withZone(DateTimeZone.UTC).minusDays(30).withHourOfDay(0) .withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0); return parseDate(fromDate, defaultFrom, true); }
private Optional<DateTime> getShiftEndDate(DateTime day, Entity shift) { Shift shiftFirst = new Shift(shift); List<TimeRange> ranges = shiftFirst.findWorkTimeAt(day.toLocalDate()); if (ranges.isEmpty()) { return Optional.empty(); } LocalTime startTime = ranges.get(0).getTo(); DateTime startShitTime = day; startShitTime = startShitTime.withHourOfDay(startTime.getHourOfDay()); startShitTime = startShitTime.withMinuteOfHour(startTime.getMinuteOfHour()); return Optional.of(startShitTime); }
private Optional<DateTime> getShiftStartDate(DateTime day, Entity shift) { Shift shiftFirst = new Shift(shift); List<TimeRange> ranges = shiftFirst.findWorkTimeAt(day.toLocalDate()); if (ranges.isEmpty()) { return Optional.empty(); } LocalTime startTime = ranges.get(0).getFrom(); DateTime startShitTime = day; startShitTime = startShitTime.withHourOfDay(startTime.getHourOfDay()); startShitTime = startShitTime.withMinuteOfHour(startTime.getMinuteOfHour()); return Optional.of(startShitTime); }
@Future public DateTime getEndTime() { return endDate.toDateTime().withHourOfDay(endHourMinute.getHourOfDay()).withMinuteOfHour(endHourMinute.getMinuteOfHour()); }
private DateTime randomTime(DateTime dateTime, Period period) { return dateTime.withHourOfDay(hours(period)) .withMinuteOfHour(minutes()) .withSecondOfMinute(seconds()); }
private long[] getMinCountsForHour(String name, int year, int month, int day, int hour) { DateTime dt = new DateTime().withYear(year).withMonthOfYear(month).withDayOfMonth(day).withHourOfDay(hour); AggregateKeyGenerator akg = new AggregateKeyGenerator(REPO_PREFIX, name, dt); return convertToArray(getEntries(akg.getHourKey()), 60, false); }
@Future @FutureWithYearLimit() public DateTime getStartTime() { return startDate.toDateTime().withHourOfDay(startHourMinute.getHourOfDay()).withMinuteOfHour(startHourMinute.getMinuteOfHour()); }