user3835327
user3835327

Reputation: 578

Date "DAY" auto increment by 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

Upvotes: 1

Views: 1136

Answers (4)

Chris Martin
Chris Martin

Reputation: 30756

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

Upvotes: 0

micha
micha

Reputation: 49612

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);

Upvotes: 0

Darshan Lila
Darshan Lila

Reputation: 5868

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.

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

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

Upvotes: 1

Related Questions