2

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

UBA_MobileTeam
  • 561
  • 2
  • 7
  • 10

3 Answers3

5

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It's not clear who that will return to me the UTC time for my country,the result of the below code returns the current time without consider the time zone : long dateInMillis = System.currentTimeMillis(); String format = "yyyy-MM-dd HH:mm:ss"; final SimpleDateFormat sdf = new SimpleDateFormat(format); String dateString = sdf.format(new Date(dateInMillis)); – UBA_MobileTeam Nov 09 '12 at 16:10
  • 2
    @user1812264: There's no such thing as "the UTC time for my country" - UTC is global. – Jon Skeet Nov 09 '12 at 16:10
  • @user1812264: Just `new Date()` will give the current time in UTC, but yo haven't set the time zone in your `SimpleDateFormat`, so it's defaulting to the local one. You've got to explicitly set it to UTC. `Date` itself isn't aware of time zone. – Jon Skeet Nov 09 '12 at 16:13
  • erm thought I was an expert on time zones and I always thought GMT is a time zone, not UTC? The only thing that makes me insecure of what I otherwise considered was The Truth is that Jon Skeet with more than half a million credits on Stackoverflow says something else! But seriously, please clarify Jon Skeet. – Martin Andersson Aug 16 '13 at 10:36
  • @MartinAndersson: Well, the time zone London observes is normally known as "Europe/London", which varies between GMT and BST. UTC is usually regarded as a time zone in its own right. GMT is always UTC+0 (as far as I'm aware), but it wouldn't be appropriate to talk about being in a time zone of "GMT" in my view. UTC is a much more reasonable approach... it's less UK-centric, to start with. – Jon Skeet Aug 16 '13 at 11:09
2

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

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());
Xander
  • 5,487
  • 14
  • 49
  • 77
  • Except that will be in the local time zone, not UTC. – Jon Skeet Nov 09 '12 at 16:14
  • Yeah, but `user1812264` asks for the time in his country. – Xander Nov 09 '12 at 16:16
  • Well, he asked for "global UTC format for my country" - it's not clear, but given the title, I don't believe he actually wants local time. – Jon Skeet Nov 09 '12 at 16:17
  • Thank you all for your feedbacks but the case as below : i need to make a HTTP request with UTC time regardless my current time and time zone since the server deals with UTC time for all requests . – UBA_MobileTeam Nov 11 '12 at 14:46
  • @UBA_MobileTeam In that case, you can do it with `.setTimeZone()`. See the updated answer – Xander Nov 12 '12 at 11:55