Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在android中比较两个日期_Java_Android_Date_Calendar - Fatal编程技术网

Java 无法在android中比较两个日期

Java 无法在android中比较两个日期,java,android,date,calendar,Java,Android,Date,Calendar,我想将两个日期与浏览器历史进行比较。。。 我看过太多的帖子,但没有得到任何帮助 我的代码如下: private static String calculateDate() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR

我想将两个日期与浏览器历史进行比较。。。 我看过太多的帖子,但没有得到任何帮助

我的代码如下:

 private static String calculateDate()
{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, -10);
    return simpleDateFormat.format(new Date(calendar.getTimeInMillis()));
}
private static String today()
{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR,0);
    return simpleDateFormat.format(new Date(calendar.getTimeInMillis()));
}

public void getBHistory()
{
    long startdates = 0;
    long enddates = 0;
    Date endDate = null;
    Date startDate=null;

    try
    {
        startDate = (Date)new SimpleDateFormat("yyyy-MM-dd")
                .parse(calculateDate());
        endDate = (Date)new SimpleDateFormat("yyyy-MM-dd")
                .parse(today());
        startdates = startDate.getTime();
        enddates = endDate.getTime();
    } catch (ParseException e)
    {
        e.printStackTrace();
    }

    // 0 = history, 1 = bookmark
    String sel = Browser.BookmarkColumns.BOOKMARK + " = 0" + " AND "
            + Browser.BookmarkColumns.DATE + " BETWEEN ? AND ?";
    Cursor mCur = m_oContext.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, sel,
            new String[]{
                    "" + startdates, "" + enddates
            }, null);
    mCur.moveToFirst();
    String title = "";
    String date_time = "";
    if (mCur.moveToFirst() && mCur.getCount() > 0)
    {
        while (!mCur.isAfterLast())
        {

            title = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.TITLE));
            date_time = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.DATE));
            SimpleDateFormat simpleDate= new SimpleDateFormat("yyyy-MM-dd");
            String curDate=simpleDate.format(new Date(Long.parseLong(date_time)));

            Toast.makeText(m_oContext,"History Time : "+curDate,Toast.LENGTH_SHORT).show();
            Toast.makeText(m_oContext,"Limit Time : "+calculateDate(),Toast.LENGTH_SHORT).show();
            //TODO: Compare these two dates here

            mCur.moveToNext();
        }
    }

} 
如果历史日期早于十天前,我想这样做,然后通知用户。
任何形式的帮助都将不胜感激,谢谢

日历是可比较的,所以您可以使用compare to。我会把curDate做成日历。如果curDate早于calculatedDate(您已将其设置为10天前),则
(curDate.compareTo(calculatedDate)<0)
将为真

您可以使用 在()之前 或 在()之后
要将您计算的日期与今天的日期进行比较,我在比较一周前的日期时遇到了一个问题,并搜索了答案,这对我很有帮助:。-最后一个答案是关于
NavigableSet

public boolean isHDateEarlier(String historyDate){

      String[] historySplitStrings= historyDate.split("-");
      String[] tenDaysEarlierStrings = calculateDate().split("-");

      int historyYear = Integer.parseInt(historySplitStrings[0]);
      int daysYear = Integer.parseInt(tenDaysEarlierStrings [0]);
      int historyMonth = Integer.parseInt(historySplitStrings[1]);
      int daysMonth = Integer.parseInt(tenDaysEarlierStrings [1]);
      int historyDay = Integer.parseInt(historySplitStrings[2]);
      int daysDay = Integer.parseInt(tenDaysEarlierStrings [2]);


if(historyYear  < daysYear ){//check year
      return true;
}

    if(historyMonth  < daysMonth  &&    
          historyYear   <= daysYear ){//check month
          return true;
    }



  if(historyDay < daysDay && 
        historyYear <= daysYear && 
        historyMonth <= daysMonth){//check day
          return true;
  }

return false;
}
尝试使用
NavigableSet
,例如
TreeSet
,并将日期放入列表中。 与
较低
较高

tl相比;博士 java.time 您使用的是麻烦的旧日期时间类,现在已被java.time类取代

时区 您的代码在确定日期(如“今天”)时忽略了时区这一关键问题

示例代码 该类表示一个仅限日期的值,不包含一天中的时间和时区

时区对于确定日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。例如,中午夜后几分钟是新的一天,而中仍然是“昨天”

您的输入字符串是标准格式的。默认情况下,java.time类在解析/生成字符串时使用格式。因此,无需指定格式化模式

LocalDate target = LocalDate.parse( "2016-01-02" );
你说边界是十天前的。使用
加号
减号
方法确定未来/过去的日期

LocalDate tenDaysAgo = today.minusDays( 10 );
使用
compareTo
equals
isBefore
isAfter
方法进行比较

Boolean alertUser = target.isBefore( tenDaysAgo );
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到java.time

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释

大部分java.time功能都在中向后移植到java 6和7,并进一步适应于中(请参阅)


该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的类,例如、、和。

Calendar cal1=Calendar.getInstance();Calendar cal2=Calendar.getInstance();试试{cal1.setTime(simpleDate.parse(curDate));}catch(ParseException e){e.printStackTrace();}试试{cal2.setTime(simpleDate.parse(calculateDate());}catch(ParseException print e){e.stacktrace()}if(cal1.compareTo(cal2)“什么都没有发生”是什么意思?如果您的日期等于或在计算日期之后,If语句将为false。日期在计算日期之前也是一个提示,因为您已经有了日历对象,我将使用它们,而不是让您的方法返回字符串。这样您就可以避免额外的解析步骤来获取日历对象好的,您可以打印您正在与日志进行比较的两个日期吗?这样我们就可以看到发生了什么。Calendar cal1=Calendar.getInstance();Calendar cal2=Calendar.getInstance();try{cal1.setTime(simpleDate.parse(curDate));cal2.setTime(simpleDate.parse))(calculateDate());}catch(ParseException e){e.printStackTrace();}if(cal1.before(cal2)){Toast.makeText(m_oContext,“Notify”,Toast.LENGTH_SHORT).show()}请参考此帖子,如果得到任何帮助,请更新我们如何将LocalDate target = LocalDate.parse( "2016-01-02" );
LocalDate tenDaysAgo = today.minusDays( 10 );
Boolean alertUser = target.isBefore( tenDaysAgo );