String pickupdate = pdate.getText().toString().trim();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(pickupdate));
} catch (ParseException e){
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 3);
expirydate= sdf.format(c.getTime());
Asked
Active
Viewed 87 times
0

akarnokd
- 69,132
- 14
- 157
- 192

Shahzad Ali
- 3
- 3
-
Works for me. Tried with `pickupdate = "06-20-2022";` and got `expirydate` as `06-23-2022`. – akarnokd Jun 15 '22 at 21:12
-
2What was the value of `pickupdate` at the start? And was a stack trace printed? – Dawood ibn Kareem Jun 15 '22 at 21:13
-
1Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Jun 16 '22 at 15:08
-
@akarnokd yes this works with a fixed string date but when I do it with input from the user through android calender it add 3 days on system current date instead of adding on entered date – Shahzad Ali Jun 16 '22 at 19:16
-
As others have pointed out, you probably get a date string with invalid format, which crashes `parse`. You ignored the crash and since `Calendar` never gets updated, it will operate with the default current date and time. – akarnokd Jun 16 '22 at 19:19
1 Answers
0
tl;dr
LocalDate
.parse(
"06-20-2022" ,
DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
.plusDays( 3 )
Details
You are using terrible date-time classes that are now legacy, supplanted years ago by the modern java.time classes.
Parse user text input.
String input = "06-20-2022" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
To detect faulty input, trap for DateTimeParseException
by wrapping in a try catch
.
Add three days.
LocalDate threeDaysLater = ld.plusDays( 3 ) ;
The java.time classes are bundled with Java 8+ and Android 26+. For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring”, or alternatively, ThreeTenABP.

Basil Bourque
- 303,325
- 100
- 852
- 1,154
-
hey, thanks for the help my app is getting crashed on LocalDate ld = LocalDate.parse( input , f ) ; with below error E/AndroidRuntime: FATAL EXCEPTION: main Process: com.shahzad.feedthepoor, PID: 30977 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shahzad.feedthepoor/com.shahzad.feedthepoor.Donate_Food}: java.time.format.DateTimeParseException: Text '' could not be parsed at index 0 – Shahzad Ali Jun 16 '22 at 19:11
-
1
-
@ShahzadAli (A) What does that error message tell you at the very end, after the last COLON character in your Comment? Read out loud to yourself. (B) An exception is *not* a crash. Be very careful with your terms when describing a programming problem. If you focus on thinking in precise terms, you’ll see the problem and the solution. – Basil Bourque Jun 16 '22 at 19:40
-
@ShahzadAli Your exception message means that you were trying to parse the empty string into a `LocalDate`. You can’t do that, and trying to throws a `DateTimeParseException`. You first need to figure out what you want your program to do in this situation. Next use a `try`-`catch` construct and catch that exception. In the `try` part add the three days and all. In the `catch` part do the error handling that you want for empty and other invalid strings. – Ole V.V. Jun 17 '22 at 21:16