4

I have a GMT field in which the user enter a time to be converted to IST (for eg: in hour field 18, minute field 30, in session field am/pm). I need to get those inputs and convert to IST in java???

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
harishtps
  • 1,439
  • 7
  • 20
  • 35
  • 1
    IST = Indian/Irish/Israel/Iran Standard Time? – Daniel Kutik Feb 24 '11 at 13:09
  • @harishtps You missed the point of the comment by Kutik. The 3-4 letter abbreviations are *not* actual time zones, are not standardized, and are not even unique! Your `IST` is used in at least four different places on earth. Avoid these pseudo-zones. A [real time zone is named](https://en.m.wikipedia.org/wiki/List_of_tz_database_time_zones) in a `continent/region` format such as `Asia/Kolkata`. – Basil Bourque Sep 20 '17 at 14:44

5 Answers5

18

This is very easy and obvious if you realize that the timezone is only relevant for a date formatted as String - second/millisecond timestamps (of which java.util.Date is merely a wrapper) are always implicitly UTC (what GMT is properly called). And converting between such a timestamp and a string always uses a timezone, both ways.

So this is what you need to do:

    DateFormat utcFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat indianFormat = new SimpleDateFormat(patternString);
    indianFormat .setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    Date timestamp = utcFormat.parse(inputString);
    String output = indianFormat.format(timestamp);
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

tl;dr

OffsetDateTime.of( 
    LocalDate.now( ZoneOffset.UTC ) , 
    LocalTime.of( 18 , 30 ), 
    ZoneOffset.UTC 
).atZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) )

Details

The modern approach uses the java.time classes.

Get the current date in UTC as a LocalDate without time-of-day and without time zone or offset.

LocalDate localDate = LocalDate.now( ZoneOffset.UTC );

Specify the time per user inputs as a LocalTime without a date and without a time zone or offset.

LocalTime localTime = LocalTime.of( 18 , 30 );

Put them together with an offset-from-UTC of zero, UTC itself as the constant ZoneOffset.UTC, to get an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.of( localDate , localTime, ZoneOffset.UTC );

Apply a time zone as a ZoneId to get a ZonedDateTime for India time. Or by IST did you mean Irish Standard Time? Iran Standard 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" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );

See this code live at IdeOne.com.

localDate.toString(): 2017-02-13

localTime.toString(): 18:30

odt.toString(): 2017-02-13T18:30Z

zdt.toString(): 2017-02-14T00:00+05:30[Asia/Kolkata]


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
3

Well, joda-time is easier. Try something like this

DateTime dt = new DateTime(<year>,<month>,<day>, <hour>,<minute>, <second>, <millisecond>);
DateTime dtIST = dt.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("IST");

Note here that the use of the three letter abbreviation is deprecated and that time zones should be referred to like "America/Los_Angeles" refers to PST.I haven't the time to get the corrsesponding for IST right now but something should be left as an exercise to the reader!

UPDATE: As Basil Bourque states in the comments, Joda-Time is in maintenance mode. Use java.time instead.

Erik
  • 2,013
  • 11
  • 17
  • The reason that the three-letter form is deprecated is that it is ambiguous (e.g., I know for sure that BST stands for both British Summer Time – i.e., GMT+DST – and for Brazilian Standard Time). The “continent/city” form has none of those problems. – Donal Fellows Feb 24 '11 at 14:39
  • 2
    Update: The [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), and advises migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Sep 20 '17 at 14:37
0

When I add the below code, it worked for me.

DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
                            utcFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
                            DateFormat indianFormat = new SimpleDateFormat("dd-HH-mm");
                            utcFormat.setTimeZone(TimeZone.getTimeZone("IST"));
                            Date timestamp = utcFormat.parse("2019-04-26-19-00");
                            String istTime = indianFormat.format(timestamp);
felix Antony
  • 1,450
  • 14
  • 28
0

If you'r looking for Indian TimeZone do this

"GMT+5:30"

val sdf = SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
sdf.timeZone = TimeZone.getTimeZone("GMT+5:30")
Adarsh Vijayan P
  • 2,964
  • 2
  • 16
  • 27