views:

90

answers:

3

Long time reader, first time poster.

In Android, capturing date from datepicker and storing as string in sqlite. Sorting by date doesn't work because they're strings (unless I'm doing it wrong.

I've googled this issue for ~5 days, and it looks like there should be a way to capture the date from the date picker, convert it to a Long, store it in sqlite as a Long, select and sort on the Long date value, then convert the Long back to a "mm/dd/yyyy" string for display. I've tried various combinations of parse statements, Date, FormatDate, etc. with no luck at all.

My actual application flow would be: On activity start, get today's date and display it in button which calls the datepicker. Capture new date from datepicker (if one is entered), save it as a long to sqlite. On opening an activity showing a listview of records, select from sqlite with orderby on date (Long), convert Long to "mm/dd/yyyy" string for display in ListView.

If someone could point me at a code sample, it would be greatly appreciated - thanks!

Evan

A: 

All Date objects in java are just Long values behind the scenes. You can get the Long value from a java Date object using getTime(), storing the resulting long value, and then initializing a new Date object using that long value in the constructor. Since the DatePicker gives interfaces to Day, Month and Year you should use the java Calendar class as an interim by creating a new Calendar object, setting the day, month and year before extracting a Date object and then a long value from the Date object.

I would say something along these lines:

DatePicker dp = blah blah;
Calendar c = Calendar.getInstance();

c.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
c.set(Calendar.MONTH, dp.getMonth());;
c.set(Calendar.YEAR, dp.getYear());

long l = c.getTime().getTime();

From here you can serialize your long value. Obviously then to reverse the process you pull the long value out, construct a Date object with the long value and then use a Calendar.setTime(Date) call before using the Calendar.get function to get the values to use on the DatePicker.init function, or utilizing a SimpleDateFormat object to get it prepped for display in a format like you described.

MattC
+1  A: 

I'd just use a date as I think Calendar objects are a bit heavy for this task. Get your values from your DatePicker :

public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }

Use the integers provide to the DatePicker to create a Date:

Date date = new Date(mYear, mMonth, mDay);

Then store and sort by the long value : date.getTime().

Alternatively, if you really must use strings for sorting dates, format them in yyyy/mm/dd format to ensure proper sorting, even then you need to watch out. I've just done some tests with SimpleDateFormat and it parses inappropriate date strings e.g. 2010/09/31 is interpreted as 2010/10/01. When checking stuff like this I would always recommend running up some JUnit tests.

John J Smith
Sort by your long value. When displaying on screen, use this to convert from long to a String: DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); Date d = new Date(myLong * 1000L); String myDateFormmated = df.format(d);
Andrew
An addendum to my answer. Remember that the Date constructor shown above is deprecated so for portability reasons you may want to use MattC's answer using the Calendar object.
John J Smith
+1  A: 

Hours of hair-pulling later, the solution is sort of a mix of the answers given, and some other things:

// variables declared
private int mYear;
private int mMonth;
private int mDay;

// datepicker declared and listened for
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

// converting the datestring from the picker to a long:
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, mDay);
c.set(Calendar.MONTH, mMonth);
c.set(Calendar.YEAR, mYear);
Long lDate = c.getTime().getTime();

//The formatting that worked on the trip back from long to string 
// (I spent hours with SimpleDateFormat strings, years that were off by 1500, etc.):
String DateFormatted = DateFormat.getDateFormat(getApplicationContext()).format(helper.getDate(c)); // helper.getDate(c) is just the passing back of the Long date from my SELECT statement

New to the forum - I tried to vote up as useful, but I don't have enough reputation points yet.

EBM
Note that Calendar's month index starts at 0.
BalusC