【问题标题】:Compare two datetime in Android [closed]比较Android中的两个日期时间[关闭]
【发布时间】:2012-12-29 13:06:58
【问题描述】:

我想用一种方法比较 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;
}

如何创建这个方法?

【问题讨论】:

标签: java android datetime


【解决方案1】:
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));

【讨论】:

  • 如何获取 numberOfHours 和 numberOfMinuts 和 NumberOfSecond?
  • 'int numberOfDays=(int)((finalDay.getTime()-today.getTime())/(1000));'会给你秒数。 int numberOfDays='(int)((finalDay.getTime()-today.getTime())/(3600*1000));'会给你小时数..
  • 我有一个字符串类型的日期,现在想比较
【解决方案2】:

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

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

【讨论】:

    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      相关资源
      最近更新 更多