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

The following examples show how to use org.joda.time.DateTime#withDayOfMonth() . 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: TestDateTimeFunctionsBase.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testTruncateDate()
{
    DateTime result = DATE;
    assertFunction("date_trunc('day', " + DATE_LITERAL + ")", DateType.DATE, toDate(result));

    result = result.withDayOfMonth(20);
    assertFunction("date_trunc('week', " + DATE_LITERAL + ")", DateType.DATE, toDate(result));

    result = result.withDayOfMonth(1);
    assertFunction("date_trunc('month', " + DATE_LITERAL + ")", DateType.DATE, toDate(result));

    result = result.withMonthOfYear(7);
    assertFunction("date_trunc('quarter', " + DATE_LITERAL + ")", DateType.DATE, toDate(result));

    result = result.withMonthOfYear(1);
    assertFunction("date_trunc('year', " + DATE_LITERAL + ")", DateType.DATE, toDate(result));
}
 
Example 2
Source File: BiMonthlyPeriodFilter.java    From dhis2-android-datacapture with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static DateTime fixEndDate(DateTime endDate) {
    if(endDate==null) {
        return null;
    }
    int month = endDate.getMonthOfYear();
    if (month <= 2) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.FEBRUARY);
    } else if (month <= 4) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.APRIL);
    } else if (month <= 6) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.JUNE);
    } else if (month <= 8) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.AUGUST);
    } else if (month <= 10) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.OCTOBER);
    } else if (month <= 12) {
        endDate = endDate.withYear(endDate.getYear() + 1).withMonthOfYear(
                DateTimeConstants.JANUARY);
    }
    return endDate.withDayOfMonth(endDate.dayOfMonth().getMaximumValue());
}
 
Example 3
Source File: DateGroup.java    From the-app with Apache License 2.0 5 votes vote down vote up
private static DateTime nowWithGranularity(Choice choice) {
    DateTime now = DateTime.now().withTimeAtStartOfDay();
    switch (choice) {
        case YEAR:
            return now.withDayOfMonth(1).withMonthOfYear(1);
        case MONTH:
            return now.withDayOfMonth(1);
        case DAY: // fall through
        default:
            return now;
    }
}
 
Example 4
Source File: DateGroup.java    From AppStash with Apache License 2.0 5 votes vote down vote up
private static DateTime nowWithGranularity(Choice choice) {
    DateTime now = DateTime.now().withTimeAtStartOfDay();
    switch (choice) {
        case YEAR:
            return now.withDayOfMonth(1).withMonthOfYear(1);
        case MONTH:
            return now.withDayOfMonth(1);
        case DAY: // fall through
        default:
            return now;
    }
}
 
Example 5
Source File: TestLenientChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_setDayOfMonth() {
    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.withDayOfMonth(32);
    assertEquals("2007-02-01T00:00:00.000Z", dt.toString());
    dt = dt.withDayOfMonth(0);
    assertEquals("2007-01-31T00:00:00.000Z", dt.toString());
}
 
Example 6
Source File: TestLenientChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void test_setDayOfMonth() {
    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.withDayOfMonth(32);
    assertEquals("2007-02-01T00:00:00.000Z", dt.toString());
    dt = dt.withDayOfMonth(0);
    assertEquals("2007-01-31T00:00:00.000Z", dt.toString());
}
 
Example 7
Source File: SixMonthlyPeriodFilter.java    From dhis2-android-datacapture with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static DateTime fixEndDate(DateTime endDate) {
    if(endDate==null) {
        return null;
    }
    int month = endDate.getMonthOfYear();
    if (month <= 6) {
        endDate = endDate.withMonthOfYear(DateTimeConstants.JULY);
        endDate = endDate.withDayOfMonth(endDate.dayOfMonth().getMinimumValue());
    } else if (month <= 12) {
        endDate = endDate.withYear(endDate.getYear() + 1).withMonthOfYear(
                DateTimeConstants.JANUARY);
        endDate = endDate.withDayOfYear(endDate.dayOfYear().getMaximumValue());
    }
    return endDate;
}
 
Example 8
Source File: GanttDiagram.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void calculateFirstAndLastInstantInMonthlyMode(YearMonthDay begin) {
    if (begin == null) {
        throw new IllegalArgumentException();
    }
    DateTime beginDateTime = begin.toDateTimeAtMidnight();
    beginDateTime = (beginDateTime.getDayOfMonth() != 1) ? beginDateTime.withDayOfMonth(1) : beginDateTime;
    setFirstInstant(beginDateTime);
    setLastInstant(beginDateTime.plusMonths(1).minusDays(1));
}
 
Example 9
Source File: GanttDiagram.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int getMonthsDaysSize() {
    int result = 0;
    for (DateTime month : getMonths()) {
        DateTime firstDayOfMonth = (month.getDayOfMonth() != 1) ? month.withDayOfMonth(1) : month;
        DateTime lastDayOfMonth = firstDayOfMonth.plusMonths(1).minusDays(1);
        int monthNumberOfDays = Days.daysBetween(firstDayOfMonth, lastDayOfMonth).getDays() + 1;
        result += monthNumberOfDays;
    }
    return result;
}
 
Example 10
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 11
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());
}