比较Android中的两个日期时间



我想在一个方法中比较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;
}
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));

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

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

这可能会帮助你…

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);

相关内容

  • 没有找到相关文章

最新更新