1

I have a current date in Java like below:

String currentDate = CoreUtil.parseDate(new Date());

This returns the date for today in the form 2019-03-26.

I declared another date so that it should automatically add 7 days to the current date like below:

String defaultendDate=CoreUtil.parseDate(new Date()); + 7 days //example

So the defaultEnddate should be 2019-04-03

How would I accomplish this as I don't want to use any simple date formatter?

Also, I would like to store the date as it is in String for reasons and secondly, I only want date, not the time. I am not using Java 8 as well, so I can't really use LocalDate library here.

Daredevil
  • 1,672
  • 3
  • 18
  • 47

4 Answers4

4

LocalDate is perfect for this job:

LocalDate.now().plusDays(7);

You can get your string representation with

.format(DateTimeFormatter.ISO_DATE);

If you're not able to use Java 8, then you have a few options:

  • Use the ThreeTen-Backport, which backports most functionality of the Java 8 JSR-310 API, normally available in the java.time package. See here for details. This package is available in Maven Central.

  • You can also use Joda Time. The peculiar thing is that these two projects have almost the same layout of their websites.

  • If you're otherwise not able to use ThreeTen-Backport or Joda Time, you can use this:

    Calendar c = Gregorian​Calendar.getInstance();
    c.add(Calendar.DATE, 7);
    
    String s = new Simple​Date​Format("yyyy-MM-dd")
        .format(c.getTime());
    

    Warning
    Many things are wrong with the old Date and Time API, see here. Use this only if you have no other option.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

Use Calendar.

 Calendar cal = Calendar.getInstance();
 cal.add(Calendar.DAY_OF_MONTH, 7);
 Date defaultEndDate = cal.getTime();
vavasthi
  • 922
  • 5
  • 14
1

Something like

LocalDate.now().plusWeeks(1);

would also do the cause.

Please, bare in mind using Java 8 Date/Time API for any operations with dates and times. as it addresses shortcomings of old Date and Calendar regarding thread safety, code design, time-zone logic and other.

UPDATE:

If you must use old Date/Time API, following code would suffice:

Date date = new Date();
Calendar calendar = Calendar.getInstance();  
calendar.setTime(date);  
calendar.add(Calendar.DAY_OF_MONTH, 7);
System.out.println("Adding seven days: " + calendar.getTime());
date = calendar.getTime();
//your code
String currentDate = CoreUtil.parseDate(new Date());
MS90
  • 1,219
  • 1
  • 8
  • 17
0
*
String dt = new SimpleDateFormat("yyyy.MM.dd").format(new Date());  // Start date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 7);  // number of days to add
        dt = sdf.format(c.getTime());  // dt is now the new date
        System.out.println(dt);

*
Use java.util.Date try this one
Kamal Kumar
  • 194
  • 12
  • How does this return `YYYY-mm-DD`? – Daredevil Mar 26 '19 at 07:43
  • SimpleDateFormat is used to format and parse dates in Java. You can create an instance of SimpleDateFormat with a date-time pattern like yyyy-MM-dd HH:mm:ss, and then use that instance to format and parse dates to/from string – Kamal Kumar Mar 26 '19 at 07:49