Java Time Zone Parsing SimpleDateFormat

I'm curious how Java SimpleDateFormat decides when to increase / decrease the time elapsed based on the set time zone.

Let's say I have a date 04/04/2013. Then I set the time zone to one far from me (I am in GMT-5). Let's say I use GMT + 8.

I'm calling

SimpleDateFormat df = new SimpleDateFormat( "M/d/yy" ); df.setTimeZone( TimeZone.getTimeZone( "GMT+8" ) ); df.parse( endDate ) // this returns 06/**03**/2013 //endDate is just a String 

He returns 03/03/2013. Why does it reduce it?

Edit: Basically, I ask what is the benchmark that Java uses to cancel my date to 6/3 if I set it to GMT + 8. There is some kind of logic that says I'm not in this current hour belt, so let it change that. But since I am passing the string, I do not see where it could be.

I assume by default, if I do not provide the time zone in the string, by default it will be GMT.

+4
source share
1 answer

You are in GMT-5, and you are parsing a line representing the moment in the GMT + 8 time zone.

So, the date 06/04/2013 is actually 06/04/2013 00:00 GMT+8 . To get the date in GMT, you have to subtract 8 hours: 06/03/2013 16:00 GMT . And to get the date in GMT-5, you have to subtract another 5 hours: 06/03/2013 11:00 GMT-5 .

All these lines represent different representations of the same moment.

+2
source

Source: https://habr.com/ru/post/1490799/


All Articles