0

I'm getting a long from a server that I have to parse into a date. I'm using a calendar to do so.

Thing is that the long came transformed from the server (it have the user local time), but I get it as a default GMT and I also transform it into local time.

So, it transforms twice. Since I get it right, how can I show it without changing it to local (seems to do it by default)? My code:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));           
calendar.setTimeInMillis(dateLong);
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format1.format(cal.getTime());
Jaime Alcántara Arnela
  • 2,062
  • 5
  • 25
  • 56

3 Answers3

1

Instead of using Calendar use SimpleDateFormat. the following code shows me the correct results.

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    String result = df.format(dateLong);
    System.out.println(result);
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
1

The other answers already provide solutions with Calendar and SimpleDateFormat. I'd just like to add another approach.

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

In Android (if you're ok about adding a dependency to your project - and in this case it's totally worth it, IMO), you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it here).

First you can use a org.threeten.bp.Instant to convert the millis value to a corresponding UTC instant. Then you use a org.threeten.bp.format.DateTimeFormatter to define the format you want the date. I also use a org.threeten.bp.ZoneOffset to indicate that the formatter should use the date in UTC:

long dateLong = System.currentTimeMillis();
// convert long millis value to Instant
Instant instant = Instant.ofEpochMilli(dateLong);
// create formatter in UTC
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
    .withZone(ZoneOffset.UTC);
// format it
System.out.println(fmt.format(instant));

The output will be something like:

13/09/2017 11:28:02

0

Sorry, I misunderstood I guess you have time in milliseconds. Well, from there:

Date dateCreated = new Date(timeInMilliseconds);

And then when you create the Calendar, just set Timezone after set time, because setTimeInMillis is overriding your previous TimeZone set when you are creating the instance

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new java.util.Date().getTime());
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));

That's it

  • I don't understand, the string result of this is: 2017-09-13T12:44:20Z – Jaime Alcántara Arnela Sep 13 '17 at 09:55
  • I think I missunderstood. So, you have a Date object in milliseconds (that's why you say in Long value) and then what do you want to do? Because the Date is already created with Timezone value when you create a new Date(timeInMilliseconds) – nineunderground Sep 13 '17 at 10:09
  • I get the long from a server, but I think the pass it converted to the local timezone of the user. However, I get it as GMT and convert it again to local timezone of the user. So, it has 2h more than it has to. Since I get it converted from the server, how can I show it without convert it again (by default the calendar timezone is the user timezone, so it converts the date). I've tried with Calendar.getInstance(TimeZone.getTimeZone("GMT")), it didn't work. – Jaime Alcántara Arnela Sep 13 '17 at 10:17
  • Didn't work but they gived me the answer up there. I has to change the dateFormater timezone. Thanks anyway. – Jaime Alcántara Arnela Sep 13 '17 at 10:51