How to install TimeZone in android

Do it here, but still not translated into desired:

String dtc = "2014-04-02T07:59:02.111Z";
            SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date date = null;
        try {
            date = readDate.parse(dtc);
            Log.d("myLog", "date "+date);
        } catch (ParseException e) {
            Log.d("myLog", "dateExcep " + e);
        }

        SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm"); 
        writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
        String s = writeDate.format(date);

The output of the variable "s" still shows the time 07:59:02, and I want to do it 4 hours ahead, and this is 11:59:02

+4
source share
3 answers

You need to instruct the read-formatter to interpret the input as UTC (GMT - remember that Z stands for UTC in ISO-8601 format):

String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
readDate.setTimeZone(TimeZone.getTimeZone("GMT")); // missing line
Date date = readDate.parse(dtc);
SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm");
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String s = writeDate.format(date);

Then you will get:

04/02/2014, 11.59

+14
source

Joda time

Doing work with a date is much simpler and simpler with the Joda-Time library than with the obviously problematic java.util.Date and. Calendar classes.

Timezone

, , , .

Joda-Time 2.3.

:

...

String input = "2014-04-02T07:59:02.111Z";
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Dubai" );
DateTime dateTimeDubai = new DateTime( input, timeZone ); (a) Parse, (b) Adjust time zone.
DateTime dateTimeUtc = dateTimeDubai.withZone( DateTimeZone.UTC );

...

System.out.println( "input: " + input );
System.out.println( "dateTimeDubai: " + dateTimeDubai );
System.out.println( "dateTimeUtc: " + dateTimeUtc );

...

input: 2014-04-02T07:59:02.111Z
dateTimeDubai: 2014-04-02T11:59:02.111+04:00
dateTimeUtc: 2014-04-02T07:59:02.111Z
+2

you can take any template you want, for example, I have a date + time zone. therefore you can install according to you. here is my code.

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

here I set the date format.

String currentDate = dateFormat.format(currentLocalTime);
 String localTime = date.format(currentLocalTime);// to get the time zone i have used this.
  String newtime=currentDate+localTime;// and then i have apeend this now print log you will get the desire result.
0
source

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


All Articles