Tsunaze
Tsunaze

Reputation: 3254

Get date by utc/gmt in Android

I want to display the time in some Country, and the TimeZone is GMT+4.

private void loadWeather(){
    TimeZone tz = TimeZone.getTimeZone("GMT+0400");
    Calendar cal = Calendar.getInstance(tz);
    Date date = cal.getTime();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.getDefault());
    String myDate = df.format(date);
    tv_time.setText(myDate);
}

I've tried this, but it gives me my time, and not the other one

Upvotes: 2

Views: 851

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

I want to display the time in some Country, and the TimeZone is GMT+4.

GMT+4 is not a time zone. A time zone is represented as Region/City e.g. Europe/London. Check the List of tz database time zones for more examples. GMT+4 means time zone offset i.e. 4 hours ahead of UTC and therefore, in order to get the equivalent date-time at UTC, one has to subtract 4 hours from the date-time at GMT+4.

GMT+4 is not the standard way to represent time zone offset

The standard format is +/-HH:mm:ss or Z which refers to +00:00 offset. In most cases, you will see +/-HH:mm e.g. +06:00. Check this Wikipedia link to learn more about it.

java.time

The java.util date-time API and their corresponding parsing/formatting type, SimpleDateFormat are outdated and error-prone. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time, the modern date-time API.

Solution using java.time

To represent a date-time with time zone offset, the Java 8+ standard library provides java.time.OffsetDateTime.

Demo:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

class Main {
    public static void main(String[] args) {
        OffsetDateTime now = OffsetDateTime.now(ZoneOffset.of("+04:00"));
        System.out.println(now);

        // The corresponding date-time at UTC
        System.out.println(now.toInstant());
        // Alternatively
        System.out.println(now.withOffsetSameInstant(ZoneOffset.UTC));
    }
}

The output from a sample run:

2023-02-04T14:08:58.657721+04:00
2023-02-04T10:08:58.657721Z
2023-02-04T10:08:58.657721Z

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 2

Hamid Shatu
Hamid Shatu

Reputation: 9700

Use SimpleDateFormat as below and set the TimeZone to the SimpleDateFormat object...I think, you will get the problem right.

    Calendar calendar = Calendar.getInstance();

    SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT+0400"));

    String date = dateFormatGmt.format(calendar.getTime());

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500595

The problem is that you're specifying the time zone just on the Calendar - which is only used to get the current instant in time, which doesn't depend on the time zone. You need to specify it on the format instead, so that it's applied when creating an appropriate text representation of that instant:

private void loadWeather() {
    Date date = new Date(); // This is enough; it uses the current instant.
    DateFormat df = DateFormat.getDateTimeInstance(
        DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    df.setTimeZone(TimeZone.getTimeZone("GMT+0400"));
    String myDate = df.format(date);
    tv_time.setText(myDate);
}

Or to inline even more:

private void loadWeather() {
    DateFormat df = DateFormat.getDateTimeInstance(
        DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    df.setTimeZone(TimeZone.getTimeZone("GMT+0400"));
    tv_time.setText(df.format(new Date()));
}

(This is assuming you really do want the short date/time format using the current locale.)

Upvotes: 3

Related Questions