I need to print date like today,Yesterday,2 days ago like that for that i have done
I am getting date like : String date1 = "Thu Nov 13 19:01:25 GMT+05:30 2014";
calling like str=get_userTime(date1);
private String get_userTime(String usertime) {
Date d = null;
// String datee = "Thu Nov 13 19:01:25 GMT+05:30 2014";
String datee = usertime;
SimpleDateFormat inputFormat = new SimpleDateFormat(
"EE MMM dd HH:mm:ss zz yyy");
try {
d = inputFormat.parse(datee);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(outputFormat1.format(d));
op = outputFormat1.format(d);
op=formatToYesterdayOrToday(op);
return op;
}
and this is another function to get yesterday/today i have use this Link
public static String formatToYesterdayOrToday(String date) {
date=op;
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date);
DateTime today = new DateTime();
DateTime yesterday = today.minusDays(1);
DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("hh:mma");
if (dateTime.toLocalDate().equals(today.toLocalDate())) {
return "Today " + timeFormatter.print(dateTime);
} else if (dateTime.toLocalDate().equals(yesterday.toLocalDate())) {
return "Yesterday " + timeFormatter.print(dateTime);
} else {
return date;
}
}
But getting Error:
11-21 13:12:10.626: E/AndroidRuntime(20654): java.lang.IllegalArgumentException: Invalid format: "11/21/2014"
11-21 13:12:10.626: E/AndroidRuntime(20654): at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:871)
On
DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
.parseDateTime(date); //In (formatToYesterdayOrToday())
I have used Joda-time.jar