Android的/ Java的 - 日日差 [英] Android/Java - Date Difference in days

查看:132
本文介绍了Android的/ Java的 - 日日差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到当前的日期(格式为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.

这篇关于Android的/ Java的 - 日日差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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