Search code examples
javaandroiddateandroid-sqliteactiveandroid

Saving dates to sqlite database with Active Android


I have a simple class Event that I am saving into a SQlite database wtih active android like so:

Event.java

@Table(name = "events", id = "_id")
public class Event extends Model {

    @Column (name = "EventLocation")
    private String mEventLocation;

    @Column (name = "EventDate")
    private String mEventDate;
}

AddEventActivity.java:

mSubmitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       String eventLocation = mEventLocation.getText().toString();
       String eventDate = mEventDate.getText().toString();
       Event event = new Event(eventLocation,eventDate);
       event.save();

etc. At the moment, I am saving the date as well as Start/End times as strings. But I'm adding a new feature in my app where I would like to compare the current date/time with the dates of my ArrayList of objects and then return the next Event that will happen later that day or whatever.

What is the most efficient way to do this? I need to be able to sort my ArrayList of events by date with a Comparable or Comparator, then compare them to the current date, so I have tried parsing the strings to a Date object and with SimpleDateFormatter, but as they are Strings this doesn't really work. How can I save the dates to SQLite with Active Android? All examples of saving I'm finding are Strings.

I'm fairly new to Java/Android. Many thanks.


Solution

  • 1) Sorting the List

    Make your Event class implement Comparable. And in the compareTo() method, make comparison using the Date.

    Make sure the type of dateTime is Date in your Event class.

    public class Event extends Model implements Comparable{
    
    ...
    
    
        @Override
        public int compareTo(MyObject o) {
            return getDateTime().compareTo(o.getDateTime());
        }
    }
    

    OR, If you dont want to change you model, create Comparator on the fly

    Collections.sort(myList, new Comparator<Event>() {
      public int compare(Event o1, Event o2) {
          if (o1.getDateTime() == null || o2.getDateTime() == null)
            return 0;
          return o1.getDateTime().compareTo(o2.getDateTime());
      }
    });
    

    2) Storing Date to ActiveAndroid

    ActiveAndroid supports serializing Date fields automatically. It is stored internally as a timestamp (INTEGER) in milliseconds.

    @Column(name = "timestamp", index = true)
    private Date timestamp; 
    
    //and the date will be serialized to SQLite. You can parse strings into a Date object using SimpleDateFormat:
    
    public void setDateFromString(String date) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy");
        sf.setLenient(true);
        this.timestamp = sf.parse(date);
    } 
    

    Or you can create a Util method to convert String to Date which returns Date:

    public Date getDateFromString(String selectedDate) throws ParseException{
        DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
        Date date = format.parse(selectedDate);
        return date;
    }
    

    3) Comparing the date to the List of Event Objects.

    Then finally call the function to find dates later than the specified date from the List

    public static List<Event> findRecent(Date newerThan) {
        return new Select().from(Event.class).where("timestamp > ?", newerThan.getTimeInMillis()).execute();
    }
    

    Hope it helps!