Coder Social home page Coder Social logo

Comments (4)

pswargam avatar pswargam commented on August 28, 2024 2

Hi @divanmostert ,

System.out.println(timezone) will print some of the internal values on the underlying instance (in this case sun.util.calendar.ZoneInfo), but it will not include all the internal fields that are use to do a date calculation. We have been doing some testing and both versions will print the correct date, assuming an offset of two hours for Africa/Windhoek.

The difference you are seeing comes from JDK-8212970, where how the internal representation of timezones changed, from using the rearguard formatting to use the vanguard formatting of the timezone database. This causes ZoneInfo fields like offset to now have different values (compensated on the values of transitions, a fields that is not displayed when printing the timezone).

In any case, java.util.TimeZone is part of the specification, but sun.util.calendar.ZoneInfo is not, which means its internal behavior is subject to change. For that reason, usage of methods like toString on TimeZone is discouraged (as it will call http://hg.openjdk.java.net/jdk-updates/jdk11u/file/571c180c5106/src/java.base/share/classes/sun/util/calendar/ZoneInfo.java#l546)

Additionally, please consider using the newer java.time api with java.time.ZoneId and java.time.ZonedDateTime.

Thanks,
Prashanth

from corretto-11.

pswargam avatar pswargam commented on August 28, 2024

Hi @divanmostert ,

Thank you for reporting this issue. We will investigate and get back to you.

Thanks,
Prashanth

from corretto-11.

davecurrie avatar davecurrie commented on August 28, 2024

Doesn't look like a bug, closing this issue.

from corretto-11.

dmostert-gresham avatar dmostert-gresham commented on August 28, 2024

@davecurrie I revisited this after many moons and tracked down where I think the problem is. It is with the SimpleDateFormat class which I used for string representation.

Running the below code will result in the time also being showed as updated when in fact it was not:

`public static void main(String[] args) {
    testToString(2099);
    testToString(2020);
}

public static void testToString(int year) {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    c.set(Calendar.YEAR, 2000);
    c.set(Calendar.MONTH, 11);
    c.set(Calendar.DAY_OF_MONTH, 31);
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.AM_PM, 1);

    java.util.Date gDate = c.getTime();

    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    ((SimpleDateFormat) formatter).applyPattern("MMMMMM dd, yyyy HH:mm:ss a z");

    Calendar gregorianCalendar = new GregorianCalendar();

    gregorianCalendar.setTimeInMillis(gDate.getTime());
    gregorianCalendar.set(Calendar.YEAR, year);
    System.out.println(formatter.format(gregorianCalendar.getTime()) + "("+ gregorianCalendar.getTime()+")");
}`

Result is:

December 31, 2099 13:00:00 PM GMT(Thu Dec 31 14:00:00 CAT 2099)
December 31, 2020 12:00:00 PM GMT(Thu Dec 31 14:00:00 CAST 2020

from corretto-11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.