Convert GMT to IST in java?

I have a GMT field in which the user enters the time to convert to IST (for example, in the 18 hour field, the 30 minute field in the am / pm session field). I need to get these inputs and convert to IST in java ???

+3
source share
4 answers

This is very simple and obvious if you understand that the time zone only relates to a date formatted as string labels - seconds / milliseconds (of which it java.util.Dateis just a shell) are always implicitly UTC (which is correctly called GMT). And the conversion between such a timestamp and a string always uses the time zone, both ways.

Here 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);
+15
source

, - . -

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

, "America/Los_Angeles" PST. , corrsesponding IST , - !

: , Joda-Time . java.time.

+3

TL;DR

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

java.time.

UTC LocalDate .

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

LocalTime .

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

offset-from-UTC , UTC ZoneOffset.UTC, OffsetDateTime.

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

ZoneId, ZonedDateTime . IST ? ?

continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland. 3-4 , EST IST, , (!).

ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );

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 [/]


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+3

, .

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);
0

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


All Articles