JDK-8038490 : Using IsoFields.WEEK_BASED_YEAR with ZonedDateTime throws ClassCastException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8
  • Priority: P3
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-03-27
  • Updated: 2014-04-18
  • Resolved: 2014-04-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_minorResolved
Related Reports
Duplicate :  
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]
    }

}



Comments
Duplicate of 8040058.
18-04-2014

Duplicate of 8040058.
18-04-2014