Alok Mysore
Alok Mysore

Reputation: 616

Adding days to date in Java Error

Here's my code. I'm the joda library for managing time on Java.

int dayOffset = 100;
DateFormat dateFormat = new SimpleDateFormat("DD/MM/YYYY");

LocalDate ldate = new LocalDate().plusDays(dayOffset);
Date date = ldate.toDate();

String s = dateFormat.format(date);
System.out.println(s);

I ran this code on 13/01/2015 and I'm getting the output as 113/04/2015 which is obviously not a valid date. What am I doing wrong in this?

Upvotes: 3

Views: 91

Answers (1)

rgettman
rgettman

Reputation: 178263

According to the SimpleDateFormat javadocs, if you use capital DD, which means "day of year", then it makes sense that adding 100 days yields day 113.

You should use dd (lowercase), which means "day of month".

Also, you should use lowercase yyyy for year; YYYY means "week year".

Upvotes: 6

Related Questions