日期增量问题

时间:2012-03-28 18:14:19

标签: java blackberry java-me

我可以在黑莓应用程序中增加和减少日期。

当我在弹出屏幕中更改某些数据并单击下一步时,问题仍然存在,但是没有增加,但是长值与增量值相同。

Calendar calendar = Calendar.getInstance(); 
String dateFormat = compareDate; //mar 28,2012-compare date value
String m = dateFormat.substring(0, 3); 
String dd = dateFormat.substring(4, 6); 
String y = dateFormat.substring(7, 11);
dateFormat = dd + " " + m + " " + y; // 28 mar 2012

long dateLong = HttpDateParser.parse(dateFormat);
long ctimeMinus50Days = dateLong + 1L * ((long) DateTimeUtilities.ONEDAY); 
calendar.setTime ( new Date(ctimeMinus50Days) ); 
System.out.println("ctimeMinus50Days" + ctimeMinus50Days);

Date d = calendar.getTime();
SimpleDateFormat sd1Exactform = new SimpleDateFormat("MMM dd,yyyy"); 
sd1Exactform.format(d);

if (dateCurrent != null) {  //static value so making null before assigning new value
    dateCurrent = null; 
}
dateCurrent = sd1Exactform.format(d);

ctimeMinus50Days值适用于增量,但是当我看到dateCurrent输出时,它只是旧日期,即使长值显示增加的数据。

2 个答案:

答案 0 :(得分:3)

您可以使用此课程

public class DateUtilities {
    static Calendar calendar = Calendar.getInstance();
    public static long getPreviousDate(long currentDate){
        calendar.setTime(new Date(currentDate));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
        return calendar.getTime().getTime();
    }
    public static long getNextDate(long currentDate){
        calendar.setTime(new Date(currentDate));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
        return calendar.getTime().getTime();
    }
}

答案 1 :(得分:-1)

Calendar类公开了添加/减去日期元素的添加方法。这是一个将一天添加到由字符串表示的日期的示例:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {

public static void main(String[] args) throws Exception{
    String dateCurrent = "Mar 1,2012";
    SimpleDateFormat sdExactform = new SimpleDateFormat("MMM d,yyyy");
    Date date = sdExactform.parse(dateCurrent);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DATE, 1);
    System.out.println(calendar.toString());
    }

}