0

I'm currently building an android app for notes. What I want is to able to save and display the date at which the note was created or saved.

Right now I'm doing it with an SQLite Database. I just created a row and stored the current date in a custom format with SimpleDateFormat. I can easily save and display it.

What I want is, that if the note is created today and we see it in our ListView in the MainActivity the date should say "Today, HH:mm" instead of the full day. Same for "Yesterday, HH:mm". Maybe Id also like if it would say "Monday, HH:mm" if were still just some days away from the date the note was created or saved.

I think I could get it to work with many if-Statements, but it would be really much work and I don't know if it would be that efficient.

Is there an easier way to do it?

Thanks in advance.

  • See: [Daylight saving time and time zone best practices](http://stackoverflow.com/q/2532729/642706) – Basil Bourque Jul 02 '15 at 21:20
  • possible duplicate of [Best way to work with dates in Android SQLite](http://stackoverflow.com/questions/7363112/best-way-to-work-with-dates-in-android-sqlite). And [this](http://stackoverflow.com/q/4272908/642706). And [this](http://stackoverflow.com/q/7957065/642706). And [many more](http://stackoverflow.com/search?q=sqlite+date). Please **search StackOverflow** before posting. – Basil Bourque Jul 02 '15 at 21:22

1 Answers1

0

First of all: do not store dates as formatted strings, NEVER. Second: yes, you'll need those ifs. Third: no, they're not much work, there'll only be three of them! One for today, one for yesterday, one for the last week and the others will fallback to the default. Tip: use SimpleDateFormat for formatting dates.

gfpacheco
  • 2,831
  • 2
  • 33
  • 50
  • If I save them as Strings I can perfectly use them. Why shouldnt I save them as Stirings and how should I save them then? –  Jul 02 '15 at 20:01
  • 1
    Take a look at [this](http://stackoverflow.com/a/13694823/1732725) for a great explanation – gfpacheco Jul 02 '15 at 20:07
  • I tried it you way. But I can't imagine any of the ifs. How should my if for today look like? I hope you dont mind helping me. Thanks! –  Jul 02 '15 at 22:48
  • Your stored date is an integer, you just have to create the integer for the begining of the day and check if the stored one is greater than the one you just created – gfpacheco Jul 02 '15 at 23:17
  • How do I create a long for the begining of the day? –  Jul 02 '15 at 23:19
  • Use [this](http://stackoverflow.com/a/12679472/1732725) to get the start of a date then call `getTimeInMillis()` to get the long – gfpacheco Jul 03 '15 at 00:50