I am building sunshine app using volley library. I successfully parse data but I am unable to format date. I am getting date from api in millis. I use original code method of sunshine to format date but still getting date as 18 Jan 1970. JSON field dt: 1484856000 I want to convert it in original date and then in string like today, tomorrow.
Asked
Active
Viewed 1,058 times
-2
-
1This looks like second not ms, so multiply by 1000. Then use SimpleDateFormat to format as reuired – Scary Wombat Jan 20 '17 at 05:24
-
[Check this](http://stackoverflow.com/questions/7953725/how-to-convert-milliseconds-to-date-format-in-android) – Bhavin Patel Jan 20 '17 at 05:25
1 Answers
0
Check http://developer.android.com/reference/java/text/DateFormat.html
public String getFormattedDate(Context context, long smsTimeInMilis) {
Calendar smsTime = Calendar.getInstance();
smsTime.setTimeInMillis(smsTimeInMilis);
Calendar now = Calendar.getInstance();
final String timeFormatString = "h:mm aa";
final String dateTimeFormatString = "EEEE, MMMM d, h:mm aa";
final long HOURS = 60 * 60 * 60;
if (now.get(Calendar.DATE) == smsTime.get(Calendar.DATE) ) {
return "Today " + DateFormat.format(timeFormatString, smsTime);
} else if (now.get(Calendar.DATE) - smsTime.get(Calendar.DATE) == 1 ){
return "Yesterday " + DateFormat.format(timeFormatString, smsTime);
} else if (now.get(Calendar.YEAR) == smsTime.get(Calendar.YEAR)) {
return DateFormat.format(dateTimeFormatString, smsTime).toString();
} else {
return DateFormat.format("MMMM dd yyyy, h:mm aa", smsTime).toString();
}
}

Rajesh Koshti
- 572
- 1
- 7
- 25