使用今天,昨天,明天等字符串格式化日期的正确方法 [英] Right way to format date with strings like today, yesterday, tomorrow etc

查看:126
本文介绍了使用今天,昨天,明天等字符串格式化日期的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期文字视图。而我的textview拥有如2011.09.17所示的日期字符串。那么我还是想要这样做,但是我也想在今天或昨天的某些特定日期添加更多的用户友好的信息。例如,如果今天是2011.09.17,我希望我的textview的值为昨天,而不是2011.09.16,而今天是2011.09.17。



我已经设法做到了:),但是在丑陋的方式:(我这样做很多'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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆