0

I want Current Date in GMT wise Timezone.I used following code.

 public static Date getGMTDate(String dateFormat) throws ParseException
   {
        SimpleDateFormat dateFormatGmt = new SimpleDateFormat(dateFormat);
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));

        // Local time zone
        SimpleDateFormat dateFormatLocal = new SimpleDateFormat(dateFormat);

        // Time in GMT
        return dateFormatLocal.parse(dateFormatGmt.format(new Date()));
   }

//call above function.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = getGMTDate("yyyy-MM-dd HH:mm:ss");

when i will change my device date then time is display in GMT formate but Date is not display in GMT timezone.its display of device's date. but I want Current GMT Date.

dipali
  • 10,966
  • 5
  • 25
  • 51

3 Answers3

5

This may works

        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(dateFormatGmt.format(new Date())+"");

Specify the format, and you will get it in GMT!

EDIT: You can also check This

Community
  • 1
  • 1
1

I changed your method as below

public static String getGMTDate() {
    DateFormat dateFmt = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
    dateFmt.setTimeZone(TimeZone.getTimeZone("GMT"));

    // Time in GMT
    return dateFmt.format(new Date());
}

And got the following date (which is same as what you wanted I believe)

07-18 10:41:54.525: D/test(6996): GMT Date is: **Jul 18, 2013 10:41:03 AM**

So simply call this method and it will return the GMT date in string format.

EDIT 1:

you are setting GMT time zone and not GMT Date. You are not trying to understand your code. When you called new Data() it returned your device date, and then you formatted it for the corresponding GMT date and time. So if your change your device date to 16th July then your code (or even my code) will return the GMT equivalent of 16th July time. If you want 18th July GMT time even when your device's date is 16th July 2000, then I don't think you can do it without getting it from some network entity.

EDIT 2:

You should look at this SO reference to a similar kind of problem and it may serve you well.

Community
  • 1
  • 1
Rajeev
  • 1,374
  • 1
  • 11
  • 18
  • please change your android device date and then call your above method.is it getting same output after changing date in android device?please check and give me some solution.this method is display only time in GMT. – dipali Jul 18 '13 at 10:49
  • if you change your device date then how will you get 18th July 2013? `new Date()` returns a date from the device itself. It doesn't get it from network. – Rajeev Jul 18 '13 at 11:01
  • but i want current GMT Date.in my app,i am using GMT Date .i am not using device date.please say how to find GMT current date in android? – dipali Jul 18 '13 at 11:10
  • i have edited my answer to include another SO thread which is trying to do the same thing. You should have a look at that and try using it for your purpose. Apart from this I can't suggest anything else at the moment. – Rajeev Jul 19 '13 at 08:51
1

Output: 2016-08-01 14:37:48 UTC

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Works fine.

Ramdhas
  • 1,765
  • 1
  • 18
  • 26