Home > Blockchain >  convert utc datetime into diffrent timezone datetime
convert utc datetime into diffrent timezone datetime

Time:12-19

Below program gives output: Sat Dec 18 15:53:33 GMT 05:30 2021 but i am expecting it should give time by adding 5:30 in time , i need to covert this Sat Dec 18 15:53:33 into "Asia/Kolkata" timezone value. how to do it!

 Date date1 = null;

        try {
            SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//.parse(p.getCreatedDate());
            isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
            date1=isoFormat.parse("2021-12-18 15:53:33"); 
            System.out.println(date1);

            Log.e("------------xxx1------",date1.toString());//prints // Sat Dec 18 15:53:33 GMT 05:30 2021

        } catch (Exception e) {
            e.printStackTrace();
        }

CodePudding user response:

 isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
 date1=isoFormat.parse("2021-12-18 15:53:33"); 

You've set the formatter to a particular timezone, so strings that do not specify a timezone are interpreted relative to the timezone of the formatter.

 System.out.println(date1);

And then you format it according to the timezone of the Date, which is the local timezone. So I conclude your local timezone is in fact GMT 05:30. You parsed a time relative to the same timezone you used to print it.


Given these particular Java classes, which are not necessarily the best way (see the answer from Basil Bourque), here's what I think you're trying to achieve.

    // parse time as if in Z timezone
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date1 = isoFormat.parse("2021-12-18 15:53:33 Z"); 

    // print date as if in some other timezone
    SimpleDateFormat locFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    locFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    System.out.println(locFormat.format(date1));

Output is

    2021-12-18 21:23:33  0530

You might need to explicitly set the isoFormat timezone to GMT, though I don't think so.

CodePudding user response:

Avoid legacy classes

Never use SimpleDateFormat, TimeZone, Date, Calendar, etc.

Use only java.time classes.

java.time

Your input string nearly complies with ISO 8601 standard format. Replace SPACE within T to fully comply.

String input = "2021-12-18 15:53:33".replace( " " , "T" ) ; 

Your input has no indicator of time zone. So parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

Specify your desired time zone.

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

Place your date-with-time into the context of a time zone.

ZonedDateTime zdt = ldt.atZone( z ) ;

Now you have determined a moment, a point in the time line.

Generate text representing that moment.

String output = zdt.toString() ;

To see that same moment in UTC, an offset of zero hours-minutes-seconds from the prime meridian, extract an Instant.

Instant instant = zdt.toInstant() ;
String output2= instant.toString() ;
  •  Tags:  
  • java
  • Related