Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to format date with strings like today, yesterday, tomorrow etc

Tags:

I have a date textview. And my textview holds a date string like 2011.09.17. Well I still do want to have that but I also want to add some more user friendly info for some specific dates like today or yesterday. For example if today is 2011.09.17 I want my textview to have value of yesterday instead 2011.09.16 and today instead 2011.09.17.

well I already managed to do this :), but in the ugly way :(. I do this with a lot of 'if->than' which is really uglly and what ever I want to add some new rule like if the date is older than one year I want to put string like last year or so.... I really need to add ugly logic.

My question is is there a nicer way to do this ? is there something like design pattern for this ? What is the recommended way to do this ? I am sure that many people have encounter this kind of problems

if there some bather approach than thousand of ifs ? if not thanks any way at least I will stop searching for bather solution

any suggestions , snippet or so will be appreciated

Thanks

like image 716
Lukap Avatar asked Sep 18 '11 14:09

Lukap


People also ask

How do you format date and day?

Many people get confused about how to write dates with commas, so here is a rule of thumb: in the month-day-year format (used in the United States), place commas after the day and year. In the day-month-year format (used in the UK and other countries), do not use commas at all. On May 13th, 2007 Daniel was born.

What date format should be used?

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD.

How do I display a date in a specific format?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.


2 Answers

You could try getRelativeDateTimeString in DateUtils http://developer.android.com/reference/android/text/format/DateUtils.html

like image 127
Christopher Souvey Avatar answered Sep 30 '22 17:09

Christopher Souvey


I usually use this handy java library for Relative Time formatting. Prety Time Library

like image 21
Ye Myat Min Avatar answered Sep 30 '22 19:09

Ye Myat Min