• Post Reply Bookmark Topic Watch Topic
  • New Topic

converting from local(IST) to UTC Date without setting default timezone  RSS feed

 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my java program i am trying to convert local(IST) to UTC timezone , if i set java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("UTC")) as utc it is working but i want without setting default value.




OutPut:-
CurrentDate ==============>Tue Feb 09 18:56:42 IST 2016
TIME AFTER SETTING ZONE :Tue Feb 09 18:56:42 IST 2016

But i want in UTC...Could you please help me out here.....

 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i dont want in string ,i want in Date or atleast set UTC time in calender

Calendar cal = Calendar.getInstance();
cal.setTime();

 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, get rid of Calendar and Date. They're old and clunky and don't work very well. Rewrite the code using the java.time package.

you should create a ZonedDateTime for your current time zone, and then use the withZoneSameInstant() method to change the time zone, while keeping the same instant in time.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and welcome to CodeRanch!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again I have corrected the code tags in yout post and doesn't it look better.
 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks,

Could you please provide me

Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();

Here currentdate is in IST format ,please tell me how to pass this date to zonedatetime and get utc value from it...

I have tried this way i coouldnt succeed,


LocalDateTime ldt1 = LocalDateTime.ofInstant(currentDate.toInstant(), ZoneId.systemDefault());

System.out.println("Currenttime : " + ldt1);
ZoneId utc = ZoneId.of("UTC");
ZonedDateTime zdt = ZonedDateTime.of(ldt1,utc);
zdt = zdt.withZoneSameInstant(utc);
System.out.println("Time After UTC Coversion : " + zdt);


Ouput :-

Currenttime : 2016-02-11T15:26:10.877
Time After UTC Coversion : 2016-02-11T15:26:10.877Z[UTC]

Here i am getting same values.......

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you persisting in using Calendar and Date?
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the current time at a specific timezone, use ZonedDateTime.now(ZoneId).
 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Date value i am extracting from java.sql.Timestamp().
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to use Date, the data in it refer to a time independent of all timezones.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anu mogal wrote:Actually Date value i am extracting from java.sql.Timestamp().


So call the Timestamp's toLocalDateTime method instead and start working with the java.time classes as previously described in this thread.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know it had that method; it also has toinstant.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know either until last week when I upgraded my code base to use java.time and had to interface to a variety of legacy code including JDBC.
 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LocalDateTime ldt = SQLTimestamp.toLocalDateTime();
trace("ldt ======> "+ldt);
ZoneId utc = ZoneId.of("UTC");
ZonedDateTime zdt = ZonedDateTime.of(ldt,utc);
trace("zdt ======> "+zdt);
zdt = zdt.withZoneSameInstant(utc);
trace("zdt ======> "+zdt);

OutPut:-

ldt ======> 2016-02-05T02:15:16.020
zdt ======> 2016-02-05T02:15:16.020Z[UTC]
zdt ======> 2016-02-05T02:15:16.020Z[UTC]

See here time is not converting into utc after calling both the ZonedDateTime.of(ldt,utc) and zdt.withZoneSameInstant(utc) methods. Still it is in system zone only .
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because LocalDateTime does not store time zone information. "2016-02-05T02:15:16.020" does not mean "2016-02-05T02:15:16.020Z[IST]" just because your system is in that time zone. You need to tell it first, but instead of using IST you're using UTC.

Note that if you already have a LocalDateTime, you can easily create a ZonedDateTime by calling atZone().

You will want to do something like this:

Now that you have an unambiguous point in time, you can do the actual conversion.
 
anu mogal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much ..it is working for timestamp..

could you please let me know how can Iachieve same thing for

java.sql.Time ???
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. A time's offset depends not only on the time zone, but also the time of year. Without a date, you can't say how much the time should be offset.

For instance, most countries in Europe have daylight saving time during the summer, while most countries in Asia don't. Not just that, but a lot of countries used daylight saving time in the past, so it also depends on what year it is.

You can add a fixed offset to a time to get an OffsetTime, but it's not guaranteed to be correct for a particular time-zone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!