2013-10-24 27 views
1

我想检查当前日期是否等于(或包含)某个定义的日期间隔。我有这样的事情:ADK Java检查日期是否等于定义的日期间隔

 Calendar c1 = Calendar.getInstance(); 
     SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); 

     Date lowerBound = null; 
     Date upperBound = null; 
     try { 
      lowerBound = sdf1.parse("29-10-2013"); 
      upperBound = sdf1.parse("30-12-2013"); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     TextView txtdate1 = (TextView) findViewById(R.id.textView1); 

     if (c1.getTime().before(upperBound) && c1.getTime().after(lowerBound)) { 
      txtdate1.setText("Is up to date"); 
     } else { 
      txtdate1.setText("Is not up to date"); 
     } 

回答

0

你可以使用的功能beforeafter检查,如果你的日期是在一定范围内。

Calendar c1 = Calendar.getInstance(); 
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); 

    Date lowerBound = sdf1.parse("20-10-2013"); 
    Date upperBound = sdf1.parse("30-12-2013"); 

    TextView txtdate1 = (TextView) findViewById(R.id.textView1); 

    if (c1.getTime().before(upperBound) && c1.getTime().after(lowerBound)) { 
     txtdate1.setText("Is up to date"); 
    } else { 
     txtdate1.setText("Is not up to date"); 
    } 

很酷的事情是,如果数据是在某些定义的范围通过创建预定义功能,你可以定义一些函数,将检查。例如,参见this

0

不使用字符串表示法。而是使用Calendar对象来执行前后操作。

Calendar c1 = Calendar.getInstance(); 

Calendar min = \\ create your min calendar 
Calendar max = \\ create your max calendar. 

if (c1.after(min) && c1.before(max)) { 
相关问题