Java Code Examples for java.time.ZonedDateTime#withMonth()

The following examples show how to use java.time.ZonedDateTime#withMonth() . 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: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 2
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 3
Source File: TCKZonedDateTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 4
Source File: TCKZonedDateTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 5
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 6
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 7
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 8
Source File: AbstractSiteMapTest.java    From crawler-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testDateParsing() {
    assertNull(AbstractSiteMap.convertToDate("blah"));
    assertNull(AbstractSiteMap.convertToDate(null));

    SimpleDateFormat isoFormatShortDate = new SimpleDateFormat("yyyyMMdd", Locale.ROOT);
    isoFormatShortDate.setTimeZone(TimeZone.getTimeZone("UTC"));

    // For short dates we only check on the year/month/day portion of the
    // result.
    // Time zone UTC is assumed because short dates do not contain a time
    // zone.
    assertEquals("20140101", isoFormatShortDate.format(AbstractSiteMap.convertToDate("2014")));
    assertEquals("20140601", isoFormatShortDate.format(AbstractSiteMap.convertToDate("2014-06")));
    assertEquals("20140603", isoFormatShortDate.format(AbstractSiteMap.convertToDate("2014-06-03")));

    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ROOT);
    isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    // Complete date plus hours and minutes
    // yyyy-MM-dd'T'HH:mm+hh:00
    assertEquals("20140603T103000", isoFormat.format(AbstractSiteMap.convertToDate("2014-06-03T10:30+00:00")));

    // Complete date plus hours, minutes and seconds
    assertEquals("20140603T103045", isoFormat.format(AbstractSiteMap.convertToDate("2014-06-03T10:30:45+00:00")));

    // Negative time zone
    assertEquals("20140603T153045", isoFormat.format(AbstractSiteMap.convertToDate("2014-06-03T10:30:45-05:00")));

    // Complete date plus hours, minutes, seconds and a decimal fraction of
    // a second
    SimpleDateFormat isoFormatWithFractionSeconds = new SimpleDateFormat("yyyyMMdd'T'HHmmss.S", Locale.ROOT);
    isoFormatWithFractionSeconds.setTimeZone(TimeZone.getTimeZone("UTC"));
    assertEquals("20140603T103045.820", isoFormatWithFractionSeconds.format(AbstractSiteMap.convertToDate("2014-06-03T10:30:45.82+00:00")));

    // Date examples given in https://www.w3.org/TR/NOTE-datetime
    ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0), AbstractSiteMap.TIME_ZONE_UTC);
    // YYYY (eg 1997) -- no time zone, see comment above
    zdt = zdt.withYear(1997);
    parseCompareDate(zdt, "1997", "yyyyMMdd");
    // YYYY-MM (eg 1997-07) -- no time zone, see comment above
    zdt = zdt.withMonth(7);
    parseCompareDate(zdt, "1997-07", "yyyyMMdd");
    // YYYY-MM-DD (eg 1997-07-16) -- no time zone, see comment above
    zdt = zdt.withDayOfMonth(16);
    parseCompareDate(zdt, "1997-07-16", "yyyyMMdd");
    // YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
    // one hour less in UTC because of time zone +01:00
    zdt = zdt.withHour(19).withMinute(20).minusHours(1);
    parseCompareDate(zdt, "1997-07-16T19:20+01:00");
    // YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
    zdt = zdt.withSecond(30);
    parseCompareDate(zdt, "1997-07-16T19:20:30+01:00");
    // YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
    zdt = zdt.withNano(450000000);
    parseCompareDate(zdt, "1997-07-16T19:20:30.45+01:00");
}
 
Example 9
Source File: TCKZonedDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 10
Source File: TCKZonedDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 11
Source File: ZonedDateTimeAxis.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Makes datetimes even, in the sense of that years always begin in January,
 * months always begin on the 1st and days always at midnight.
 *
 * @param dateTimes The list of dates.
 * @return The new list of dates.
 */
private List<ZonedDateTime> makeDateTimesEven(final List<ZonedDateTime> dateTimes) {
    // if the dates contain more dates than just the lower and upper bounds, make the dates in between even.
    if (dateTimes.size() > 2) {
        List<ZonedDateTime> evenDateTimes = new ArrayList<>();

        // for each interval, modify the date slightly by a few millis, to make sure they are different days.
        // this is because Axis stores each value and won't update the tick labels, if the value is already known.
        // this happens if you display days and then add a date many years in the future the tick label will still be displayed as day.
        for (int i = 0; i < dateTimes.size(); i++) {
            ZonedDateTime evenDateTime = dateTimes.get(i);
            switch (actualInterval.interval) {
                case YEARS:
                    // if it's not the first or last date (lower and upper bound), make the year begin with first month and let the months begin with first day.
                    if (i != 0 && i != dateTimes.size() - 1) {
                        evenDateTime = evenDateTime.withMonth(0);
                        evenDateTime = evenDateTime.withDayOfMonth(1);
                    }
                    evenDateTime = evenDateTime.withHour(0);
                    evenDateTime = evenDateTime.withMinute(0);
                    evenDateTime = evenDateTime.withSecond(0);
                    evenDateTime = evenDateTime.withNano(6 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case MONTHS:
                    // if it's not the first or last date (lower and upper bound), make the months begin with first day.
                    if (i != 0 && i != dateTimes.size() - 1) {
                        evenDateTime = evenDateTime.withDayOfMonth(1);
                    }
                    evenDateTime = evenDateTime.withHour(0);
                    evenDateTime = evenDateTime.withMinute(0);
                    evenDateTime = evenDateTime.withSecond(0);
                    evenDateTime = evenDateTime.withNano(5 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case WEEKS:
                    // make weeks begin with first day of week?
                    evenDateTime = evenDateTime.withHour(0);
                    evenDateTime = evenDateTime.withMinute(0);
                    evenDateTime = evenDateTime.withSecond(0);
                    evenDateTime = evenDateTime.withNano(4 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case DAYS:
                    evenDateTime = evenDateTime.withHour(0);
                    evenDateTime = evenDateTime.withMinute(0);
                    evenDateTime = evenDateTime.withSecond(0);
                    evenDateTime = evenDateTime.withNano(3 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case HOURS:
                    if (i != 0 && i != dateTimes.size() - 1) {
                        evenDateTime = evenDateTime.withMinute(0);
                        evenDateTime = evenDateTime.withSecond(0);
                    }
                    evenDateTime = evenDateTime.withNano(2 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case MINUTES:
                    if (i != 0 && i != dateTimes.size() - 1) {
                        evenDateTime = evenDateTime.withSecond(0);
                    }
                    evenDateTime = evenDateTime.withNano(1 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                case SECONDS:
                    evenDateTime = evenDateTime.withNano(0 * TemporalConstants.NANOSECONDS_IN_MILLISECOND);
                    break;
                default:
                    break;
            }
            evenDateTimes.add(evenDateTime);
        }

        return evenDateTimes;
    } else {
        return dateTimes;
    }
}
 
Example 12
Source File: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 13
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 14
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 15
Source File: TCKZonedDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 16
Source File: TCKZonedDateTime.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 17
Source File: TCKZonedDateTime.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 18
Source File: TCKZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}
 
Example 19
Source File: TCKZonedDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_noChange() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(6);
    assertEquals(test, base);
}
 
Example 20
Source File: TCKZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_withMonth_normal() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    ZonedDateTime test = base.withMonth(1);
    assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100));
}