Reputation: 2648
I Get the following Date from my SQLite database which get pulled down as a string in the following format '03-07-2015'.
I want this format to display like this 'Fri 3rd July 2015' I am doing this using SimpleDateFormat, I then need to pass this into an ArrayList, I cannot quite get this to work, any advice to what I am doing wrong would be much appreciated.
String dayNumberSuffix = getDayOfMonthSuffix(3);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd'" + dayNumberSuffix + "' MMMM yyyy");
String d = sdf.format(cursor.getString(1));
dates.add(d); // ArrayList<String>
String getDayOfMonthSuffix(final int n) {
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
I have tried a few different variations, but I believe the one above is closest.
Upvotes: 0
Views: 242
Reputation: 157447
you need to parse the date in the parse 03-07-2015
, that you get from your database and format it:
String dayNumberSuffix = getDayOfMonthSuffix(3);
SimpleDateFormat parseSDF = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd'" + dayNumberSuffix + "' MMMM yyyy");
Date date = parseSDF.parse(cursor.getString(1));
String d = sdf.format(date);
dates.add(d); // ArrayList<String>
should do it
Upvotes: 2