我使用以下代码获取当前日期(格式为 12/31/1999,即 mm/dd/yyyy):
Textview txtViewData;
txtViewDate.setText("Today is " +
android.text.format.DateFormat.getDateFormat(this).format(new Date()));
我的另一个日期格式为:2010-08-25(即 yyyy/mm/dd),
所以我想找出日期之间的天数差异,如何找到天数差异?
(换句话说,我想找出两者之间的区别当前日期 - yyyy/mm/dd 格式的日期)
不是一个真正可靠的方法,最好使用JodaTime http://joda-time.sourceforge.net/
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 1985);
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
这是一个近似值...
long days = diff / (24 * 60 * 60 * 1000);
要从字符串中解析日期,您可以使用
String strThatDay = "1985/08/25";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
d = formatter.parse(strThatDay);//catch exception
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....
不过,既然您确定日期格式......
你也可以这样做Integer.parseInt()
在它的子字符串上获取它们的数值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)