0

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()]);
joan
  • 131
  • 2
  • 4
  • 12
  • I dont get what you intend to do. Something like http://stackoverflow.com/questions/26246030/how-to-use-jdk-gregoriancalendar-object-dates-with-joda ??? – GhostCat Oct 13 '16 at 18:49
  • @GhostCat - Kind of..existing code in my application is using GregorianCalendar API and with that month value index starts from 0 and so for October it represents 09 as month value. But for LocalDate ,month value is 10 for October. I want to match month values to be same in LocalDate and GregorianCalendar API. – joan Oct 13 '16 at 18:57
  • Any GregorianCalendar date that is properly formatted using DateTimeFormatter will show 10 in a numeric representation of October. If you use some plain format, you'll have to add 1. – laune Oct 13 '16 at 18:58

1 Answers1

0

You assume incorrectly

You misunderstand the [java.util.GregorianCalendar][1] and java.util.Calendar class. When generating formatted strings to present the value of a date-time, it of course reports months January-December as 1-12, with 10 for October. So the 13th of October will indeed present as 10/13/2016, not 09/13/2016 as you erroneously claim.

The problem comes with methods for getting and setting the month where it bizarrely uses index zero-based counting 0-11. One of many reasons to avoid the troublesome legacy date-time classes. To quote the class doc…

java.util.Calendar.MONTH

…The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

java.time

FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

The code below uses java.time classes. Concepts are similar to Joda-Time as Joda-Time provided the inspiration for java.time.

Converting

Your Question is quite convoluted and confusing.

Converting properly may solve your problems. Try to do the bulk of your work in java.time classes, and minimize your use of the old Date & Calendar classes. To convert to and fro, look to new methods added to etc old classes. See my Question, Convert java.util.Date to what “java.time” type? for more discussion.

ZonedDateTime zdt = myGregorianCalendar.toZonedDateTime();
LocalDate ld = zdt.toLocalDate();
Month m = ld.toMonth(); // `Month` enum object, not a number.
int monthNumber = m.getValue();  // Month number 1-12 for Jan-Dec.

Or more simply:

int monthNumber = myGregorianCalendar.toZonedDateTime().getMonthValue() ;

Either way, you will get a sane 1-12 number of January-December.

Going the other direction.

GregorianCalendar gc = java.util.GregorianCalendar.from( myZonedDateTime );

Notice that I am not manipulating the month field at all on either object, GregorianCalendar or ZonedDateTime.

Tip: Rather than pass around a mere integer to represent a month, instead pass around Month enum objects. This makes your code more self-documenting, ensures a range of valid values, and provides type-safety.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154