-2

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.

1 Answers1

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