I have the following Java code working on Netbeans. However I have two issues left to figure out.
1) The output for dates between is always 1.
2) I can't get the format for the date to show the day of the week
notes: I can't use Joda Time and I am aware that using scripts has been bad for 10+ years. I am not concerned with daylight savings or leap year. I have researched the subject for days and understand how many times this subject has been brought up.
Here is what I have so far. Any help is appreciated.
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM dd yyyy");
String date1 =request.getParameter("firstdate");
String date2 =request.getParameter("seconddate");
int answer = date2.compareTo(date1);
out.println(date1);
out.println(date2);
out.println(" " + answer + " days");
I have tried using:
Days days = Days.daysBetween(new DateTime(date1), new DateTime(date2));
int daysBetweenDates = days.getDays();
and also
Period period = new Period(date1, date2);
I am out of ideas and am thinking that something is messed up with what I have done or included(not included). Any help is much appreciated.
UPDATE WITH FIX
In the end the issue I was having was that the date selected by a user was being stored as a string. I just changed it to the following and everything fell in place:
<%
String date1 =request.getParameter("firstdate");
String date2 =request.getParameter("seconddate");
SimpleDateFormat dateformat = new SimpleDateFormat ("E yyyy.MM.dd"); //SDF to display output with day of week
// This piece here made everything work fine
Date displaydate1=new Date(date1);
Date displaydate2=new Date(date2);
int differenceInDays = (int) ((displaydate2.getTime() - displaydate1.getTime())/(1000*60*60*24));//common method to calculate number of days
out.println("Between " +dateformat.format(displaydate1)+ " and " +dateformat.format(displaydate2)+ " there are " +differenceInDays+ " days");
%>