0

I am facing strange issue when converting IST to GMT. The conversion works fine round the clock except during 5:30PM and 6:30PM. During this time instead of converting the time to 12:00:00 it is converted as 00:00:00

3 Answers3

0

OK, so I guess that IST means Indian Standard Time; i.e. GMT + 5:30.

5:30pm IST would therefore be 12noon GMT which is 12:00:00 using a conventional 24 hour clock. (Or "railway time" as they used to call it.)

Without any further input from you, my best guess is that either you've got some bug in your time handling code (e.g. the input time is not what you think it is), or there is something strange with the locale setting that is affecting the way that the time is displayed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I'm using the 24 hour clock in this answer to keep things clear.

Are you certain you are converting from 17:30 IST? If you were actually converting 05:30 (ie five thirty in the morning) that would be precisely midnight GMT. In a 24 hour clock midnight is 00:00, so that would all add up.

That wouldn't however explain a conversion from 06:30 though, which would be 01:30 GMT.

Rich
  • 15,602
  • 15
  • 79
  • 126
0

tl;dr

ZonedDateTime.of( 2017 , 1 , 23 , 17 , 30 , 0 , 0 , ZoneId.of( "Asia/Kolkata" ) ) 
             .toInstant()

2017-01-23T12:00:00Z

Not enough information

Impossible to answer this Question precisely without seeing the problematic code. But I can show you correct modern code, using the java.time classes.

Using java.time

By “IST” did you mean India Standard Time, Ireland Standard Time, or Iran Standard Time? Given the example data, I am guessing India time.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Asia/Kolkata" );

LocalDate localDate = LocalDate.of( 2017 , 1 , 23 );
LocalTime localTime = LocalTime.of( 17 , 30 ); // 5:30 PM.

ZonedDateTime zdt = ZonedDateTime.of( localDate , localTime , z );

To view the same moment through the lens of UTC, extract an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = zdt.toInstant();

See this code run live at IdeOne.com.

At this date, India time is five and a half hours ahead of UTC. So adjusting 5:30 PM in the afternoon in Kolkata to UTC means subtracting 5 hours and 30 minutes which takes us back to noon in UTC. So you see in the results.

zdt.toString(): 2017-01-23T17:30+05:30[Asia/Kolkata]

instant.toString(): 2017-01-23T12:00:00Z


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154