Java Code Examples for org.joda.time.DateTime#withHourOfDay()

The following examples show how to use org.joda.time.DateTime#withHourOfDay() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestLenientChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_setHourOfDay() {
    Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
    DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
    assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
    dt = dt.withHourOfDay(24);
    assertEquals("2007-01-02T00:00:00.000Z", dt.toString());
    dt = dt.withHourOfDay(-1);
    assertEquals("2007-01-01T23:00:00.000Z", dt.toString());
}
 
Example 2
Source File: TestLenientChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_setHourOfDay() {
    Chronology zone = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
    DateTime dt = new DateTime(2007, 1, 1, 0, 0 ,0, 0, zone);
    assertEquals("2007-01-01T00:00:00.000Z", dt.toString());
    dt = dt.withHourOfDay(24);
    assertEquals("2007-01-02T00:00:00.000Z", dt.toString());
    dt = dt.withHourOfDay(-1);
    assertEquals("2007-01-01T23:00:00.000Z", dt.toString());
}
 
Example 3
Source File: TestDateTimeFunctionsBase.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test
public void testTruncateTimestamp()
{
    DateTime result = TIMESTAMP;
    result = result.withMillisOfSecond(0);
    assertFunction("date_trunc('second', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = result.withSecondOfMinute(0);
    assertFunction("date_trunc('minute', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = result.withMinuteOfHour(0);
    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));

    result = result.withDayOfMonth(20);
    assertFunction("date_trunc('week', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = result.withDayOfMonth(1);
    assertFunction("date_trunc('month', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = result.withMonthOfYear(7);
    assertFunction("date_trunc('quarter', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = result.withMonthOfYear(1);
    assertFunction("date_trunc('year', " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(result, session));

    result = WEIRD_TIMESTAMP;
    result = result.withMillisOfSecond(0);
    assertFunction("date_trunc('second', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));

    result = result.withSecondOfMinute(0);
    assertFunction("date_trunc('minute', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));

    result = result.withMinuteOfHour(0);
    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));

    result = result.withDayOfMonth(20);
    assertFunction("date_trunc('week', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));

    result = result.withDayOfMonth(1);
    assertFunction("date_trunc('month', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));

    result = result.withMonthOfYear(7);
    assertFunction("date_trunc('quarter', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));

    result = result.withMonthOfYear(1);
    assertFunction("date_trunc('year', " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(result));
}
 
Example 4
Source File: DateTruncTransformFunctionTest.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
private void testDateTrunc(Schema schema)
    throws Exception {

  DateTime result = TIMESTAMP;
  result = result.withMillisOfSecond(0);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "second", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withSecondOfMinute(0);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "minute", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withMinuteOfHour(0);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "hour", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withHourOfDay(0);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "day", UTC_TIME_ZONE.getID(), result.getMillis());

  // ISO8601 week begins on Monday. For this timestamp (2001-08-22), 20th is the Monday of that week
  result = result.withDayOfMonth(20);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "week", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withDayOfMonth(1);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "month", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withMonthOfYear(7);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "quarter", UTC_TIME_ZONE.getID(), result.getMillis());

  result = result.withMonthOfYear(1);
  testDateTruncHelper(schema, TIMESTAMP_ISO8601_STRING, "year", UTC_TIME_ZONE.getID(), result.getMillis());

  result = WEIRD_TIMESTAMP;
  result = result.withMillisOfSecond(0);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "second", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withSecondOfMinute(0);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "minute", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withMinuteOfHour(0);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "hour", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withHourOfDay(0);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "day", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withDayOfMonth(20);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "week", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withDayOfMonth(1);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "month", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withMonthOfYear(7);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "quarter", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());

  result = result.withMonthOfYear(1);
  testDateTruncHelper(schema, WEIRD_TIMESTAMP_ISO8601_STRING, "year", WEIRD_DATE_TIME_ZONE.getID(),
      result.getMillis());
}