I am working on an android app at school. I am currently building a notepad. I would like to know if there are any ways to get the date and store it into the sqlite database as a date format and then, when I am retrieving it from the database and write it into my textview, I could display hh::mm if the note was written/modified today, or dd/mm/yy if it were (let’s say) written yesterday. To be more clear:
- I write a note
- I get the date
- I insert everything into my database table
- Retrieve the data into a ListView
- for each note, I want to display the date in the right format,( if the date was the same day, just display the hh:mm, yesterday if it was written yesterday, else display dd/mm/yy
Also, which way is better to store the date into database, as Date format? I searched online and it appears that there are several ways to do it, which one is the best?
Date can be converted to
longvalue (number of milliseconds passed from Jan, 1 1970 00:00:00 UTC+0000).It surely is the most convenient presentation of date:
System.currentTimeMillis()returns current date in this format;AlarmManager;SharedPreferences.Calendarclass which is standard for date operations in Java, also supports this format – it hassetTimeInMillis()andgetTimeInMillis()functions.For more details, refer to the documentation on
Calendarclass.As for different date formats, check out the
SimpleDateFormatclass.