i have a situation where the user has to select a date while creating a request and that request will be valid upto 90 days from the date he selected.How would i implement this logic in my code? I tried but i guess i had no logic.
Asked
Active
Viewed 1,047 times
0
-
You might also consider using [JodaTime, for example](http://stackoverflow.com/questions/21842934/how-to-add-days-to-java-simple-date-format/21842959#21842959) or [Java 8's Date/Time API, for example](http://stackoverflow.com/questions/29529488/roll-over-the-month-of-dates-using-localdate-in-java/29529532#29529532) – MadProgrammer May 11 '16 at 09:47
2 Answers
2
To prase date in format specified "yyyy-MM-dd" you need SimpleDateFormat object and once you have calender object , you can add 90 days to calendar using add() method of calendar. Also you need not need SimpleDateFormat to increment calendar details:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy" ); //Not Required
Date selectedDate=null;
try {
selectedDate=dateFormat.parse("10-11-1986");// replace it with selected date
} catch (ParseException e) {
}
Calendar cal = Calendar.getInstance();
cal.setTime(selectedDate);
System.out.println(" Selected Date: "+cal.getTime());
cal.add( Calendar.DATE,90 );
System.out.println("Date after 90 days: "+cal.getTime());

Dark Knight
- 8,218
- 4
- 39
- 58
-
date format ("dd-MM-yyyy") will come in picture if you are parsing date using parse method. If you are using current date then date format is not required. Alternatively SimpleDateFormat dateFormat = new SimpleDateFormat( "dd-MM-yyyy" ); will work – Dark Knight May 11 '16 at 09:16
-
issue is i am using datepicker ie ......................and in the backend i bind it with @Column(name = "qdate") @DateTimeFormat(pattern = "dd-MM-yyyy") Date quotationDate; ...........................it is the requirement ,i need to save the date in this format only – Mir Majid Hussain May 11 '16 at 09:31
-
@MirMajidHussain Kindly check modified code, hope this will help you – Dark Knight May 11 '16 at 09:42
-1
You can use the methods setMonth() and getMonth() of Date api.
Date date = new Date();
date.setMonth(date.getMonth()+3);

Dark Knight
- 8,218
- 4
- 39
- 58

Alessandro Dal Gobbo
- 116
- 7
-
As of JDK version 1.1, replaced by Calendar.set(Calendar.MONTH, int month). – Christoph-Tobias Schenke May 11 '16 at 08:26
-