java 比较Android中的两个日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14337649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:55:00  来源:igfitidea点击:

Compare two datetime in Android

javaandroiddatetime

提问by Saeed Hashemi

I want to compare date1(2013/1/21 21:22:30) with date2(now) in one method

我想用一种方法比较 date1(2013/1/21 21:22:30) 和 date2(now)

I want to get how much date,hours,minutes and second Remaining form Date 1

我想获得多少日期,小时,分钟和秒剩余形式日期 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;
}

how can create this method ?

如何创建这种方法?

回答by RadAl

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

回答by PermGenError

you can use Date.compareTo()method.

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

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

回答by ashish.n

this might help you...follow it carefully..

这可能会帮助你......仔细遵循它......

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