1

How can i make my date "DAY" only auto increment by 1 every time I get the date value. Example my current expire date is 20141031, after increment it will changed to 20141101.

SimpleDateFormat timestampFormat  = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat timestampFormat2 = new SimpleDateFormat("dd-MM-yyyy");

String EXPDATE          = common.setNullToString((String) session.getAttribute("SES_EXPDATE"));  // 31- 10-2014

String tempEXPDATE = timestampFormat.format(timestampFormat2.parse(EXPDATE)); //20141031
int intExpdate = Integer.parseInt(tempEXPDATE.substring(6,8)); //30
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user3835327
  • 570
  • 1
  • 5
  • 19
  • 5
    Use [Calendar.add](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) – Jens Aug 15 '14 at 07:54
  • Use [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) or [threetenbp](http://www.threeten.org/threetenbp/apidocs/org/threeten/bp/package-summary.html). – Chris Martin Aug 15 '14 at 07:58
  • Thanks guys, problemd solved :) – user3835327 Aug 15 '14 at 08:18
  • possible duplicate of [How can I increment a date by one day in Java?](http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java) – Basil Bourque Aug 16 '14 at 03:02

4 Answers4

1

This is very easy with Calendar. You can use add()

SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMdd");
Calendar calendar=Calendar.getInstance();
calendar.setTime(sdf.parse("20141031"));
calendar.add(Calendar.DATE,1); // add one day to current date
System.out.println(sdf.format(calendar.getTime()));

Out put:

20141101
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

The following code will help you

     Calendar cal = Calendar.getInstance();
     Date date=timestampFormat.parse(tempEXPDATE);
     cal.setTime(date);
     cal.add(Calendar.DATE, 1);
     String newDate=timestampFormat.format(date);

Note that I have picked timestampFormat and tempEXPDATE from your code.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • Awesome... it's working !! thousand of thanks to you !! but i changed the String newDate=timestampFormat.format(date); to java.util.Date d1 = cal.getTime(); EXPDATE = timestampFormat.format(d1); – user3835327 Aug 15 '14 at 08:16
0

If you are using Java 8 you can use the new Date/Time API. With Java 8 you can simply use the plusDays() method:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dateTime = LocalDateTime.parse(yourDateString, formatter);
LocalDateTime plusOneDay = dateTime.plusDays(1);
micha
  • 47,774
  • 16
  • 73
  • 80
0

Java 8:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Java 7 with threetenbp:

import org.threeten.bp.LocalDate;
import org.threeten.bp.format.DateTimeFormatter;

DateTimeFormatter.ofPattern("yyyyMMdd").format(
    LocalDate.parse(
        "31-10-2014",
        DateTimeFormatter.ofPattern("dd-MM-yyyy")
    ).plusDays(1)
)
// 20141101
Chris Martin
  • 30,334
  • 10
  • 78
  • 137