1

I have a date in dd/mm (15/07) format, I need to add a single day to this date, so it becomes 16/07.

How can I do this in the easiest way in java?

VatsalSura
  • 926
  • 1
  • 11
  • 27
Mister M
  • 1,539
  • 3
  • 17
  • 37
  • Is just a string or some date typed class? – Ohad Eytan Jul 15 '16 at 09:35
  • it's a string and I need a string with increased date – Mister M Jul 15 '16 at 09:36
  • You should convert it into a DateTime type, so that leap years etc. are dealt with, then back to string. – henrikstroem Jul 15 '16 at 09:38
  • 5
    If you only have day and month, then you should know that the result may not be correct. For example: what should be correct result for `28/02`? Is it `29/02` or `01/03`? Or are the day and month always of "this" year? – Tom Jul 15 '16 at 09:43
  • @Tom is absolutely right, and that is also the reason why a class like `java.time.MonthDay` does not extend `Temporal` meaning does not offer any chance to add a day for example. – Meno Hochschild Jul 15 '16 at 09:48

3 Answers3

2

You can use Calendar.

String dt = "15-07-2016";  
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
1

java.time

The Answer by Goel is correct but outmoded.

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

MonthDay

The java.time classes include the MonthDay class to represent a month+day without year and without time zone.

String input = "15/07";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM" );
MonthDay monthDay = MonthDay.parse( input , formatter );

Cannot increment February 28

As commented by Tom, you cannot reliably increment February 28. In most years you would get March 1 but in leap years you get February 29. This is why the YearMonth class lacks any addDays method.

So you need to (a) assume/supply a year, (b) refuse to increment the one day of February 28, or (c) arbitrarily increment to March 1 from February 28 to ignore any possible 29th.

Let's look at the first option, supplying a year.

To get the current year we need the current date. To get the current date, specify a time zone. For any given moment, the date varies around the globe by zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
int year = today.getYear();

We can apply that year number to change our YearMonth into a LocalDate. From there the LocalDate::plusDays method increments to the next day. From the resulting instance of LocalDate we extract a YearMonth object.

LocalDate ld = monthYear.atYear( year );
LocalDate nextDay = ld.plusDays( 1 );
YearMonth ymNextDay = MonthDay.from( nextDay );
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can do this:

public String addDay(String date) {
    String[] dateSplit = date.split("/");
    String day = "" + (Integer.parseInt(dateSplit[0]) + 1);
    return day + "/" + dateSplit[1];
}

But this isn't really a nice solution, because this doesn't handle month or year swaps (This you can add by yourself using the % operator)

Or you use the SimpleDateFormat like here: How can I increment a date by one day in Java?

Community
  • 1
  • 1
CloudPotato
  • 1,255
  • 1
  • 17
  • 32