我得到当前的日期(格式为12/31/1999,即MM / DD / YYYY)作为使用下面的code:
I am getting the current date (in format 12/31/1999 i.e. mm/dd/yyyy) as using the below code:
Textview txtViewData;
txtViewDate.setText("Today is " +
android.text.format.DateFormat.getDateFormat(this).format(new Date()));
和我有格式的另一个日期:2010-08-25(如YYYY / MM / DD),
and I am having another date in format as: 2010-08-25 (i.e. yyyy/mm/dd) ,
所以我想找到天数日期之间的区别,我怎么觉得天差?
so I want to find the difference between date in number of days, how do I find difference in days?
(换句话说,我想找到的区别当前日期 - YYYY / MM / DD格式的日期)
(In other words, I want to find the difference between CURRENT DATE - yyyy/mm/dd formatted date)
推荐答案
不是一个真正的可靠的方法,用更好的 JodaTime
Not really a reliable method, better of using JodaTime
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
下面是一个近似值......
Here's an approximation...
long days = diff / (24 * 60 * 60 * 1000);
从字符串解析日期,你可以使用
To Parse the date from a string, you could use
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()
在它的子字符串,以获得他们的数值。
Although, since you're sure of the date format...
You Could also do Integer.parseInt()
on it's Substrings to obtain their numeric values.
谁能还告诉我如何告诉用户离开他回来图书的天数 - 在Android的程序(JAVA) - 任何帮助将AP preciated
Can anyone further tell me how to tell user the number of days left for him to return books - in android programming (Java) - any help will be appreciated.
相关推荐
最新文章