-4
String s= new java.util.Date().toString();

I am using java 7, I want to print one week ahead of this date I have used the calendar class but it always gives me IST 1970 and does not give correct answer and I want that date into string so please help me in this. I know this question is repeated but they cant solve my query?

Eric B
  • 217
  • 1
  • 14
  • 1
    So the code has a bug. We can't find it without seeing the code. – JB Nizet Mar 29 '17 at 19:03
  • 2
    If you stuck with Java 7 that doesn't mean you need to suffer from util.Date and Calendar not-so-perfect APIs. Your code will be better with joda-time or threeten backport libraries. That would be something like LocalDate.now().plusDays(7); – Mikhail Antonov Mar 29 '17 at 19:03
  • Post your code. – PM 77-1 Mar 29 '17 at 19:04
  • also it might be helping if you posted your question along with some interpunction it makes your question better readable – MC Emperor Mar 29 '17 at 19:09
  • Much of the java.time functionality built into Java 8 and later is back-ported to Java 6 and Java 7 in the [ThreeTen-Extra](http://www.threeten.org/threeten-extra/) project. – Basil Bourque Mar 29 '17 at 21:57

2 Answers2

1

Don't use Date, use Calendar:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());
Oneiros
  • 4,328
  • 6
  • 40
  • 69
0

If you want date in string format then use Dateformat . Below is the complete code ,

    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()));
    }
Bharat DEVre
  • 539
  • 3
  • 13