1

I'm working on an Android project, using a JSON file already encoded.

So I've got this:

{
  "type": "Feature",
  "properties": {
    "mag": 1.29,
    "place": "10km SSW of Idyllwild, CA",
    "time": 1388620296020,
    "updated": 1457728844428
  }
}

And I want to convert time in year, day, hours and seconds. I know there are many topics talking about my problem but I already tried them without success.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
WizzardCat
  • 11
  • 1
  • 3
  • 1
    `I already tried them without success` show what you've tried, so we can help fixing it. Untill you show, we can't believe that you've tried. – Vladyslav Matviienko Jun 20 '17 at 08:55
  • 1
    Not quite sure if I have understood you: Do you want to express the time difference **between your `time`-variable and now-timestamp** in years, days, hours, minutes, seconds? – Meno Hochschild Jun 20 '17 at 15:56
  • Possible duplicate of [convert epoch time to date](https://stackoverflow.com/questions/7740972/convert-epoch-time-to-date) – Ole V.V. Jun 21 '17 at 09:25
  • The linked question has milliseconds rather than seoncds, but the conversion should be trivial; in [my answer there](https://stackoverflow.com/a/44502808/5772882), for example, just use `Instant.ofEpochSecond()` instead of `Instant.ofEpochMilli()`. – Ole V.V. Jun 21 '17 at 09:30
  • Agree with @VladMatvienko that a better starting point for assisting you would be in what way the other questions didn’t help. As your questions stands there’s hardly anything we can add here that hasn’t been said before, so you just risk getting another 5 answers that you can try without success. – Ole V.V. Jun 21 '17 at 09:34

5 Answers5

1

In Android, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes, together with ThreeTenABP (more on how to use it here).

This API provides a good way to work with dates, much better than the outdated Date and Calendar (The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and they're being replaced by the new APIs).

To get a date from a timestamp value (assuming that 1388620296020 is the number of milliseconds from unix epoch), you can use the org.threeten.bp.Instant class:

// create the UTC instant from 1388620296020
Instant instant = Instant.ofEpochMilli(1388620296020L);
System.out.println(instant); // 2014-01-01T23:51:36.020Z

The output is 2014-01-01T23:51:36.020Z, because Instant is always in UTC. If you want to convert this to a date/time in another timezone, you can use the org.threeten.bp.ZoneId class and create a org.threeten.bp.ZonedDateTime:

ZonedDateTime z = instant.atZone(ZoneId.of("Europe/Paris"));
System.out.println(z); // 2014-01-02T00:51:36.020+01:00[Europe/Paris]

The output will be 2014-01-02T00:51:36.020+01:00[Europe/Paris] (the same UTC instant converted to Paris timezone).

Please note that the API avoids the use of 3-letter timezones names (like ECT or CST), because they are ambiguous and not standard. Always prefer to use the full names (like Europe/Paris or America/Los_Angeles, defined by IANA database) - you can get all the available names by calling ZoneId.getAvailableZoneIds().

If you want to print the date in a different format, you can use a org.threeten.bp.format.DateTimeFormatter (please refer to javadoc to see all the possible formats):

ZonedDateTime z = instant.atZone(ZoneId.of("Europe/Paris"));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS Z");
System.out.println(fmt.format(z)); // 02/01/2014 00:51:36.020 +0100
1

You have integer overflow. Just use the following (notice "L" after 1000 constant):

 Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......

see this : https://stackoverflow.com/a/5246444/7161543

0

Can be something like ;

long jsondate = 1388620296020L;
Date dt = new Date (jsondate); 
SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(sd.format(dt));
Ozan
  • 1,191
  • 2
  • 16
  • 31
0

try this

long uptime = 1388620296020;

long days = TimeUnit.MILLISECONDS
.toDays(uptime);
uptime -= TimeUnit.DAYS.toMillis(days);

long hours = TimeUnit.MILLISECONDS
.toHours(uptime);
uptime -= TimeUnit.HOURS.toMillis(hours);

long minutes = TimeUnit.MILLISECONDS
.toMinutes(uptime);
 uptime -= TimeUnit.MINUTES.toMillis(minutes);

long seconds = TimeUnit.MILLISECONDS
.toSeconds(uptime);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

This one is shorter:

    long lg=1388620296020l;
    Date date = new Date(lg);
virusivv
  • 337
  • 2
  • 5
  • 17