要写一个类,传入“日期”、“相差天数”两个参数,返回前几天或前后天,日期格式为“yyyy-mm-dd”
譬如public String setDate("2008-04-29",-1),那么应该返回“2008-04-28”
public String setDate("2008-04-29",2) ,那么应该返回“2008-05-01”
请高手帮忙,谢谢!
譬如public String setDate("2008-04-29",-1),那么应该返回“2008-04-28”
public String setDate("2008-04-29",2) ,那么应该返回“2008-05-01”
请高手帮忙,谢谢!
然后 g.add(Calendar.DATE,-1); 就是减一天,具体查询JDK
i got it ,
thanks
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
System.out.println(sdf.format(c.getTime()));
}}
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;public class Test { Date now;
SimpleDateFormat h;
public Test(Date date,int m)
{
h = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String n = h.format(date);
String myString = DateFormat.getDateInstance().format(date);
System.out.println ("格式化后:"+myString);
Timestamp time = Timestamp.valueOf(n);
//在天数上加(减)天数
long l = time.getTime()+24*60*60*m*1000;
time.setTime(l);
System.out.println("计算后的日期:"+time);
}
public static void main(String args[])
{
new Test(new Date(),-2);
}}
eg:DateUtils.addMonths(currentDate, 1)
那个currantDate是当前的日期,1就表示月份加一
datetime = "2008-06-16";
days = -20;
String rDate="";
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date d=df.parse(datetime);
//System.out.println("今天的日期:"+df.format(d));
//System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
//System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
rDate=df.format(new Date(d.getTime() + days * 24 * 60 * 60 * 1000));
System.out.println(datetime+"----->"+rDate);
return rDate;
}