-1

My problem is that I want to convert the local time to GMT. I am able to convert the actual time and save it to Parse but the problem is that Parse applies its own:

Wed Feb 24 10:00:00 GMT+05:30 2016

This is the date that I am getting after converting to GMT00:00 but the problem is the GMT+05:30 that is actually false as my date is actually in GMT. Now when I put this date in the server it further decreases the time by 5:30 hrs. So how can we change this GMT+05:30 to GMT+00:00

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
pranav shukla
  • 353
  • 4
  • 15
  • Please show your code to see what you have got so far – NiallMitch14 Feb 25 '16 at 16:40
  • I think this is a duplicate of this thread here: http://stackoverflow.com/questions/2055407/conversion-of-local-time-zone-to-gmt-in-java – John Harris Feb 25 '16 at 16:48
  • Exactly what are your inputs and outputs? Do you need a string representation of a date-time value in a certain format? Do you need an object of a certain type? – Basil Bourque Feb 25 '16 at 17:47

2 Answers2

1

We'll start with the current local time.

Calendar cal = Calendar.getInstance(); // automatically produces and instance of the current date/time

As I understand it, your local date is accurate to GMT but your time is 5:30 ahead of actual GMT.

In order to subtract 5:30 from your local time you can perform the following.

cal.add(Calendar.HOUR, -5);
cal.add(Calendar.MINUTE, -30);

System.out.println(cal.getTime());

Conversely, if you would like to add to the time field, you can simply use:

cal.add(Calendar.HOUR, 5); // note I have removed the minus(-) symbol
cal.add(Calendar.MINUTE, 30); // note I have removed the minus(-) symbol

System.out.println(cal.getTime());
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
1

If you are using java 8. you could utilize from the new time library. As you tell it seems what you want, is to have a stamp in UTC which is GMT+00

more about time library here

This line:

System.out.println(Instant.now().toString());

gives

2016-02-25T17:54:55.420Z

Hope it makes thing more clear to you.

Wisienkas
  • 1,602
  • 2
  • 17
  • 22
  • No need for offset. [`Instant`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) is in UTC by definition. `Instant.now().toString()` – Basil Bourque Feb 25 '16 at 17:37