Search code examples
javaandroiddatesimpledateformatepoch

Wrong Date Format shown in device from JSON API


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

Solution

  • 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;
    }