-2

I was showing my JSON date in activity layout but it gives me this kind of date format "1547458358000". How to change the date format into YYYY-MM-dd k:mm:s?

The appAdded is date came from JSON API.

From the result of testing. The toast message I received is API TIME shows as 1547458358000 while outDatedAPI shown as null.

 try {
    SimpleDateFormat DateformatAPKAPIInstalled = new SimpleDateFormat("YYYY-MM-dd k:mm:s");
        APITime = DateformatAPKAPIInstalled.parse(appAdded);
        SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd k:mm:s");
         outputDateAPI = outputFormat.format(APITime);
        Toast.makeText(FirstPageActivity.this, "DATE KO" dateAPKUpdated + appAdded + APITime, Toast.LENGTH_LONG).show();


    } catch (ParseException e) {
        e.printStackTrace();
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
No Name
  • 472
  • 1
  • 6
  • 19
  • that is timestamp you need to convert it to appropriate date. – karan Jan 24 '19 at 06:46
  • enlighten me for the mistake I made in code instead of giving negative feedback. thankyou. – No Name Jan 24 '19 at 06:47
  • "1547458358000" seems to be the milliseconds since midnight on 1 Jan 1970. – Henry Jan 24 '19 at 06:47
  • @KaranMer how to convert to appropriate date? – No Name Jan 24 '19 at 06:48
  • Do a google search for converting time stamp to date for the enlightment – Vivek Mishra Jan 24 '19 at 06:51
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 25 '19 at 11:37
  • 2
    Possible duplicate of [Convert timestamp in milliseconds to string formatted time in Java](https://stackoverflow.com/questions/4142313/convert-timestamp-in-milliseconds-to-string-formatted-time-in-java). Or more precisely duplicate of [Convert Epoch seconds to date and time format in Java](https://stackoverflow.com/questions/8262333/convert-epoch-seconds-to-date-and-time-format-in-java). – Ole V.V. Jan 25 '19 at 11:39

1 Answers1

0

You can pass long timestamp and can convert it to date using below code

private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("yyyy-MM-dd", cal).toString();
    return date;
}
karan
  • 8,637
  • 3
  • 41
  • 78