比较Android中的两个日期时间

时间:2013-01-15 12:22:46

标签: java android datetime

我想在一个方法中将date1(2013/1/21 21:22:30)与date2(now)进行比较

我想得到多少日期,小时,分钟和第二个剩余表单日期1

String mDate,mHour,mMinute,mSecond;

DateComparedateTime(String Date1)
{
 // compare  Date1 with now 
   mDate = Difference date with Date1 and Now;
   mHour = Difference hour with Date1 and Now;
   mMinute = Difference minute with Date1 and Now;
   mSecond = Difference second with Date1 and Now;
}

如何创建此方法?

3 个答案:

答案 0 :(得分:8)

Calendar currentDate = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


    String dateNow = sdf.format(currentDate.getTime());
      Date today =  new Date(dateNow);
      Date finalDay = null;
      finalDay = (Date) sdf.parse("MM/dd/yyyy");
    int numberOfDays=(int)((finalDay.getTime()-today.getTime())/(3600*24*1000));

答案 1 :(得分:4)

您可以使用Date.compareTo()方法。

DateComparedateTime(String date1)
{
    System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date1).compareTo(new Date()))
 }

答案 2 :(得分:3)

这可能对你有所帮助......仔细跟进..

Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
System.out.println("calender time in millies" + c.getTimeInMillis());
System.out.println("calender time in millies" + c.getTime());
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(c.getTimeInMillis());
System.out.println("time in seconds :: " + diffInSec);