String s= new java.util.Date().toString();
我正在使用java 7,我想在这个日期前一周打印我已经使用了日历类但它总是给我IST 1970并且没有给出正确的答案我希望那个日期成为字符串所以请帮助我这个。我知道这个问题是重复的,但他们无法解决我的疑问?
答案 0 :(得分:1)
请勿使用Date
,请使用Calendar
:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());
答案 1 :(得分:0)
如果您想要字符串格式的日期,请使用Dateformat
。以下是完整的代码,
Date givenDate= new Date();
DateFormat dateFormatter=new SimpleDateFormat("dd-MM-yy");
Calendar c=Calendar.getInstance();
c.setTime(givenDate);
//This will print whole week after given date
for(int i=1;i<8;i++){
c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormatter.format(c.getTime()));
}