UBA_MobileTeam
UBA_MobileTeam

Reputation: 591

Get Device time in UTC format regardless time zone

How i can get the local time of the device and convert it to global UTC format for my country ?

Upvotes: 2

Views: 4388

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79015

Existing answers are correct and they provided the right direction in Nov 2012 (when the question was asked). In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the error-prone java.util date-time API and their corresponding parsing/formatting type, SimpleDateFormat, and since then it has been strongly recommended to switch to java.time, the modern date-time API.

Solution using java.time:

You can simply use Instant#now to get the current moment independent of the timezone (i.e. at UTC). Note that the java.time API is based on ISO 8601 standards.

In case, you need the current date-time in a specific timezone, you can use ZonedDateTime#now(ZoneId), which you can also format in a desired format.

Demo:

class Main {
    public static void main(String[] args) {
        // Current moment
        System.out.println(Instant.now());

        // Current date-time in a specific timezone e.g. America/New_York
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(zdt);

        // Output in a custom format e.g. EEE MMM dd, uuuu 'at' hh:mm:ss a
        String formattedStr = zdt.format(DateTimeFormatter.ofPattern("EEE MMM dd, uuuu 'at' hh:mm:ss a", Locale.ENGLISH));
        System.out.println(formattedStr);
    }
}

Online Demo

Note: Never use a DateTimeFormatter for custom formats, and a SimpleDateFormat without a Locale.

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

Upvotes: 2

Xander
Xander

Reputation: 5587

Not sure what you want to do, but if you want the date and time in normal format you can do it this way:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Calendar cal = Calendar.getInstance();
String dateAndTime = dateFormat.format(cal.getTime());

String dateAndTime will be something like 2012/01/01 11:13, according to the date & time the device was set to, so it shows the same time as the device's clock. You can play around with the format a little bit by changing "yyyy/MM/dd HH:mm" to whatever you like.

Hope this helps!

UPDATE: To get the UTC time do it this way:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAndTimeUTC = dateFormat.format(new Date());

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500075

Don't get the local time at all - just get the UTC value to start with, e.g.

long millis = System.currentTimeMillis();

Or:

Date date = new Date();

If you need to format this as a string, use SimpleDateFormat but remember to set the time zone appropriately. For example:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                               Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/Utc"));
String text = format.format(new Date());

(It's not clear what you mean by "global UTC format for my country" - UTC simply is global, and it's not a format, it's a time zone.)

Upvotes: 5

Related Questions