0

I am getting a time from API like this "00:00" and I want to add Device GMT time to received time from API.

I am using following code for getting GMT Time of device

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
            Locale.US);
    Date currentLocalTime = calendar.getTime();
    DateFormat date = new SimpleDateFormat("dd:MM:yyyy HH:mm z");
    String localTime = date.format(currentLocalTime);
    Log.v("GMT time:", localTime + "");

Is there any inbuilt method to add GMT time to a specific time?

Przemek
  • 7,111
  • 3
  • 43
  • 52
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • Please define adding time to a time. For me semantically impossible. Normally you can only add a duration to a time. Well, I just speculate: Do you mean applying timezone offsets on a local time (UTC-time + timezone-offset = local-time)? – Meno Hochschild May 05 '14 at 16:14

2 Answers2

0

Maybe you can try something like this

 public static void main(final String[] args) throws Exception {   
        Date date = new Date();   
        DateFormat localDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);   
        DateFormat gmtDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);   
        DateFormat nyDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);   

        gmtDf.setTimeZone(TimeZone.getTimeZone("GMT"));   
        nyDf.setTimeZone(TimeZone.getTimeZone("America/New_York"));   

        System.out.println("local : " + localDf.format(date));   
        System.out.println("GMT   : " + gmtDf.format(date));   
        System.out.println("NY    : " + nyDf.format(date));   
    }   
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • It is Okay, But problem is how to add GMT time to any specific time. – Ashish Tiwari May 05 '14 at 11:24
  • I am not sure what you mean by adding GMT time. can you provide an usecase? – Hirak May 05 '14 at 11:29
  • I am receiving some time from API like "12:40". I need to get GMT time of device and then add it to received time from API. For example I got the GMT time +05:30 and I have to add it with received time i.e. 12:40. GMT time could be different based on user's device and locale. – Ashish Tiwari May 05 '14 at 11:46
0

You can try like this:

TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");
date.setTimeZone(gmtTime);
String localTime = date.format(currentLocalTime);

Date and time conversion to some other Timezone in java

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
  • Its not working. I got the GMT Time of device but I am unable to add it with a specific time. For ex. I got GMT time +05:30 and now I want to add it in a specific time that is "11:10". How can I do this. In other side if GMT time of device is -04:00 then how can I add this to a specific time? – Ashish Tiwari May 05 '14 at 11:16