manu
manu

Reputation: 77

sort date fields to obtain earliest date

in my database , dates are stored in DD-mm-yyyy format , how can i sort this to obtain the earliest date ?

Cursor c = myDb.query(TABLE, new String[]{"dob"}, null, null, null, null, "dob");

I have selected it to order by dob field but its not ordered ... This is the output for the above query

01-03 17:14:51.595: VERBOSE/ORDER DOB(1431): 01-11-1977
01-03 17:14:51.595: VERBOSE/ORDER DOB(1431): 01-12-1988
01-03 17:14:51.614: VERBOSE/ORDER DOB(1431): 15-01-1977
01-03 17:14:51.656: VERBOSE/ORDER DOB(1431): 31-01-1988

Upvotes: 0

Views: 355

Answers (2)

Sebastiaan van den Broek
Sebastiaan van den Broek

Reputation: 6331

In your case with sqlite and (afaik) it's lack of a date type, I would store the dates in epoch value in UTC time (this is a good practise anyway since you'll be timezone-independent) and then you can just sort by lowest value using ORDER BY.

You can just convert back to a readable date in your Java application (use SimpleDateFormat for example)

Date date = new Date(yourEpochValue);
SimpleDateFormat smf = new SimpleDateFormat("dd-MM-yyyy");
smf.setTimeZone(yourTimeZone);  
String dateString = smf.format(date);

You'd go about storing the value the other way around (if this is something you need to do)

Upvotes: 1

AlexR
AlexR

Reputation: 115328

If you are using SQL database and store dates as type DATE or DATETIME it does not matter how the dates are shown. You can just use the sql order by statement. If my assumptions are wrong please provide more details.

Upvotes: 1

Related Questions