I have a set of helper methods that am currently using to format a date received from an API.
public static String ParseDate (String dateR) {
SimpleDateFormat full_date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
try {
full_date.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = full_date.parse(dateR);
Calendar c = Calendar.getInstance();
c.setTime(date);
String day = GetDay(c.get(Calendar.DAY_OF_WEEK));
int hr = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
return day + ", " + hr+ ":" + min;
} catch (Exception e) {
System.err.println("Error" + e);
}
return dateR;
}
private static String GetDay(int d)
{
switch (d)
{
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default:
return "";
}
}
I am calling these like this:
String str = "2013-09-11T12:50:00Z";
System.out.println(ParseDate(str));
It works perfectly. However, I would like to improve its functionality. If the day of the week is not this current week, I would like to show the date rather than showing the day of the week. It would be wrong to show day of week when it is not a day from the current week.
Any help will be appreciated. Thank you
[UPDATE]:
For example: When date is :
2013-09-11T12:50:00Z
I get this output:Wednesday, 18:20
This is wrong. A simpledate
like24th May, 2013
will be better in this case.
This means that I need to be able to test whether the date falls in this week or exceeds then, should the format the date based on that. I hope am clear:)