2012-07-26 46 views
1

在我的应用程序中,我有2个字段,1是日期,另一个是数据库表中的recc(是一个整数)。在android中的日期retreival

现在,我将解释我的要求:

考虑假设用户输入的10.It意味着,从今天的日期到+10 date.I开始得到了10日当天的日期(26-07-2012)和RECC从今天的日期开始。但是我想要的是从今天开始的第10天,这意味着它肯定也会在下个月也会去(26-07-2012 ---- 5-08-2012),但是我必须知道日期的计数它属于这个特定的月份,也就是说在26-07-2012 ---- 5-08-2012之间,这个月内会有多少天。我想我已经清楚地解释了我的问题,如果不是,我准备好了给予更多解释。提前感谢。

日期值:

EditText date=(EditText)findViewById(R.id.startdateexp);  
      String startdate=date.getText().toString(); 

回答

0

您可以通过以下方式执行此操作: 1.从数据库获取日期。通过

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    try { 
    date = iso8601Format.parse(dateTime); 
    } catch (ParseException e) { 
    Log.e(TAG, "Parsing ISO8601 datetime failed", e); 
    } 
    Calendar cal=Calendar.getInstance(); 
    cal.setTime(date); 
    int currentDay= cal.get(Calendar.DAY_OF_MONTH); 

计算最后一天每月:

int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
0

如果您使用Date类可以通过转换为毫秒的天数偏移它来创建一个新的日期:

Date datePlusRecc = new Date(oldDate.getTime() + recc*86400000); // 86400000 = 24*60*60*1000 

注意,这仅当recc相对较小时(<约20)才可用,否则乘法将溢出。

0

只需使用java.util.Date类以毫秒的日期相结合。

在for循环中添加一天一个版本,以毫秒为单位,并将其转换回日期。将日期从日期对象中取出并与当前月份进行比较。一旦这个月是新的一个月,你就有当月的总天数。

Date currentDate = new Date(currentMillis); 
long countingMillis = currentMillis; 
int daysInSameMonth = 0; 
while(true){ 
    daysInSameMonth++; // if the actual date schould not count move this line at the button of the while 
    countingMillis += 1000*60*60*24; 
    Date dt = new Date(countingMillis); 
    if(currentDate.getMonth() != dt.getMonth()) 
     break; 
} 
+0

感谢RaphMclee..When我使用我们的代码它显示currentMillis不能

现在,通过下面的方法来获取一个月天之日起解析成变量 – 2012-07-26 06:19:05

+0

嗨凯德阿德萨拉...当我使用上面的代码,它显示currentMillis不能解析成一个变量。我该怎么办.. – 2012-07-26 06:28:46

+0

在这里,你必须将你从数据库检索到毫秒这是currentMillis。 – 2012-07-26 06:39:18