2

How do I get the next date(2014/03/21) given the current date(2014/03/20) in Java?

Code:

 public static String getNextDate(String  curDate) {
         String nextDate="";
        try {
           //here logic to get nextDate
        } catch (Exception e) {
            return nextDate;
        }
        return nextDate;
    }
Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
java baba
  • 2,199
  • 13
  • 33
  • 45
  • 1
    Use SimpleDateFormatter to convert to Date and then Calendar and its setter to increment. – Leos Literak Mar 20 '14 at 11:07
  • You mean next day, not next date (which makes no sense). Next time please do some research before posting a question. This has been answered several times here. – m0skit0 Mar 20 '14 at 11:07
  • 1
    @javababa Maybe this will help you: http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java It is the same question (Look at Dave's answer) – m1o2 Mar 20 '14 at 11:07
  • You'll need to use Calendar. Parse curDate into a Date, and use Calendar.add() to increment Calendar.DAY_OF_MONTH. – sp00m Mar 20 '14 at 11:08
  • similar question http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – Owen Cao Mar 20 '14 at 11:09

4 Answers4

15

Use SimpleDateFormat to get a Date-object from your string representation, then use Calendar for arithmetics followed by SimpleDateformat to convert the Date-object back to a string representation. (And handle the Exceptions I didn't do)

public static String getNextDate(String  curDate) {
  final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  final Date date = format.parse(curDate);
  final Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  calendar.add(Calendar.DAY_OF_YEAR, 1);
  return format.format(calendar.getTime()); 
}
SCI
  • 546
  • 3
  • 6
  • But be sure to check the result or include error checking in the function to ensure that the curDate string is valid – PMunch Mar 20 '14 at 11:16
  • @PMunch: You can read it in "And handle the Exceptions I didn't do" – SCI Mar 20 '14 at 11:18
0

use java Calendar and you can use to do some date arithmetic such as adding day, months and years

 public static String getNextDate(String curDate) {
        String nextDate = "";
        try {
            Calendar today = Calendar.getInstance();
            DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            Date date = format.parse(curDate);
            today.setTime(date);
            today.add(Calendar.DAY_OF_YEAR, 1);
            nextDate = format.format(today.getTime());
        } catch (Exception e) {
            return nextDate;
        }
        return nextDate;
    }
sheu
  • 284
  • 5
  • 13
0

You can use Joda Time.

public static String getNextDate(String  curDate) {
    String nextDate="";

    try {
        LocalDate date = LocalDate.now();
        date = date.plusDays(1);
        nextDate = date.toString();
    } finally {
        return nextDate;
    }
}
StaNov
  • 332
  • 1
  • 4
  • 17
0

If you want a more native Java way you could split the string using String.split("/") and then add to the date part. However doing this requires that the carry is handled and that you track leap years.

PMunch
  • 1,525
  • 11
  • 20