I am working on existing java application. My requirement is to pass a month value to a service. Before in my code they were using GregorianCalendar to represent the dates.Currently I modified the part of code to use org.joda.time.LocalDate. I know that for GregorianCalendar date index starts from zero and so for today it shows as 09/13/2016 (represents 09 for October). But with my LocalDate representation it is 10/13/2016. How can I change from my code to match values of the existing code (i.e.,09/13/2016 to represent october 13 2016).
Below is my code.
MyForm.java
private Date myDate;
public java.util.Date getMyDate()
{
return this.myDate;
}
public void setDate()
{
int day = getMyDay();
int month = getMyMonth();
GregorianCalendar gc = new GregorianCalendar();
// Advance to next year if the month selected is before current month. This
// only happens at year end.
if (gc.get(Calendar.MONTH) > month)
{
gc.add(Calendar.YEAR, 1);
}
gc.set(Calendar.DATE, day);
gc.set(Calendar.MONTH, month);
this.myDate = gc.getTime();
}
MyAction.java
Below code represents my String[] which holds date values.
List<LocalDate> localDatesList = service.getMyDates();
final List<String> tempDatesList = new ArrayList<>(localDatesList.size());
for (final LocalDate date : localDatesList) {
tempDatesList.add(date.toString());
}
final String[] formattedDates = tempDatesList.toArray(new String[localDatesList.size()]);
//represents the dates as 2016-10-14 2016-10-15 .
Please advice what would be the best possible way to overcome this issue. Which one would be the best option to modify. Can we handle this month mismatch from LocalDate and GregorianCalendar , so that when we compare the objects created from LocalDate and GregorianCalendar have the same month value.
--EDIT--
I want to convert my below List<LocalDate>
to match with the GregorianCalendar. I want my String[] to hold the values which match with GregorianCalendar date values(i.e.,9 to represent the month October).Please advice to I need to convert LocalDate to GregorianCalendar...
List<LocalDate> localDatesList = service.getMyDates();
final List<String> tempDatesList = new ArrayList<>(localDatesList.size());
for (final LocalDate date : localDatesList) {
tempDatesList.add(date.toString());
}
final String[] formattedDates = tempDatesList.toArray(new String[localDatesList.size()]);