使用今天,昨天,明天等字符串格式化日期的正确方法 [英] Right way to format date with strings like today, yesterday, tomorrow etc
问题描述
我已经设法做到了:),但是在丑陋的方式:(我这样做很多'if->比'这真的很丑陋,甚至我想添加一些新规则,如果日期大于一年我想放置字符串像去年左右....我真的需要添加丑陋的逻辑。
我的问题是有更好的方法来做到这一点吗?这样做的设计模式是什么?推荐的方法是什么?我相信有很多人遇到这种问题,如果有一些沐浴法比一千个ifs ?如果不是谢谢任何方式,至少我将停止寻找沐浴解决方案
任何建议,片段等将不胜感激
谢谢
我通常使用这个方便的java库为Relative Tim e格式化。 http://ocpsoft.com/prettytime/
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
I usually use this handy java library for Relative Time formatting. http://ocpsoft.com/prettytime/
这篇关于使用今天,昨天,明天等字符串格式化日期的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!