WizzardCat
WizzardCat

Reputation: 11

Android : Convert seconds to year, day, hours, minutes, seconds

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.

Upvotes: 1

Views: 4362

Answers (5)

Nilesh Vadaliya
Nilesh Vadaliya

Reputation: 11

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

Upvotes: 1

user7605325
user7605325

Reputation:

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

Upvotes: 1

virusivv
virusivv

Reputation: 347

This one is shorter:

    long lg=1388620296020l;
    Date date = new Date(lg);

Upvotes: 0

Ozan
Ozan

Reputation: 1201

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));

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

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);

Upvotes: 0

Related Questions