Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8038490

Using IsoFields.WEEK_BASED_YEAR with ZonedDateTime throws ClassCastException

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: tbd_minor
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 : Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Running this program :

      public class DateTimeTest {

          public static void main(String[] args) {
              ZonedDateTime time = ZonedDateTime.of(2012, 1, 1, 15, 3, 0, 0, ZoneId.of("Europe/Brussels"));
              ZonedDateTime result = time.with(IsoFields.WEEK_BASED_YEAR, time.getYear()).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
              System.out.println(result);
              // expected 2012-01-02T15:03+01:00[Europe/Brussels]
          }

      }

      yields the following exception :

      Exception in thread "main" java.lang.ClassCastException: java.time.LocalDate cannot be cast to java.time.ZonedDateTime
      at java.time.ZonedDateTime.with(ZonedDateTime.java:1311)
      at DateTimeTest.main(DateTimeTest.java:10)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

      However I do not use LocalDate

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run :

      public class DateTimeTest {

          public static void main(String[] args) {
              ZonedDateTime time = ZonedDateTime.of(2012, 1, 1, 15, 3, 0, 0, ZoneId.of("Europe/Brussels"));
              ZonedDateTime result = time.with(IsoFields.WEEK_BASED_YEAR, time.getYear()).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
              System.out.println(result);
          }

      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected console output :

      2012-01-02T15:03+01:00[Europe/Brussels]

      ACTUAL -
      Exception in thread "main" java.lang.ClassCastException: java.time.LocalDate cannot be cast to java.time.ZonedDateTime
      at java.time.ZonedDateTime.with(ZonedDateTime.java:1311)
      at DateTimeTest.main(DateTimeTest.java:10)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.ZoneId;
      import java.time.ZonedDateTime;
      import java.time.temporal.ChronoField;
      import java.time.temporal.IsoFields;

      public class DateTimeTest {

          public static void main(String[] args) {
              ZonedDateTime time = ZonedDateTime.of(2012, 1, 1, 15, 3, 0, 0, ZoneId.of("Europe/Brussels"));
              ZonedDateTime result = time.with(IsoFields.WEEK_BASED_YEAR, time.getYear()).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
              System.out.println(result);
              // expected 2012-01-02T15:03+01:00[Europe/Brussels]
          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.time.LocalDate;
      import java.time.LocalTime;
      import java.time.ZoneId;
      import java.time.ZonedDateTime;
      import java.time.temporal.ChronoField;
      import java.time.temporal.IsoFields;

      public class DateTimeTest {

          public static void main(String[] args) {
              ZonedDateTime time = ZonedDateTime.of(2012, 1, 1, 15, 3, 0, 0, ZoneId.of("Europe/Brussels"));
              LocalDate local = LocalDate.from(time);
              LocalDate first = local.with(IsoFields.WEEK_BASED_YEAR, local.getYear()).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
              ZonedDateTime result = ZonedDateTime.of(first, LocalTime.from(time), time.getZone());
              System.out.println(result);
              // expected 2012-01-02T15:03+01:00[Europe/Brussels]
          }

      }


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rriggs Roger Riggs
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: