Bruno Monteiro
Bruno Monteiro

Reputation: 17

Problems with Date using Calendar - Java

I have a problem when I try use Calendar. Look:

public static Calendar novaData(Calendar originDate, Integer amountMonth) {
    System.out.println("Origin date: " + originDate.getTime() + " - Add " + amountMonth + " Months");
    Calendar date = new GregorianCalendar(originDate.getTimeZone());
    date.add(Calendar.MONTH, amountMonth);
    System.out.println(date.getTime());
    return date;
}

Now, the console: enter image description here

In this method, I'll set a date and the amount of month I'm going to add to that date. The problem is there and the day is coming like today (18) and not day 1 as I passed. The expected result is May / 01/2019 - Jun / 01/2019 - Jul / 01/2019

Upvotes: 0

Views: 1363

Answers (2)

Anonymous
Anonymous

Reputation: 86379

java.time

    LocalDate originDate = LocalDate.of(2019, Month.MAY, 1);
    for (int amountMonth = 1; amountMonth <= 3; amountMonth++) {
        System.out.println("Origin date: " + originDate + " - Add " + amountMonth + " Months");
        System.out.println(originDate.plusMonths(amountMonth));
    }

Output is:

Origin date: 2019-05-01 - Add 1 Months
2019-06-01
Origin date: 2019-05-01 - Add 2 Months
2019-07-01
Origin date: 2019-05-01 - Add 3 Months
2019-08-01

I gather from your desired output that you are only interested in the date, not the time of day nor the time zone. If so, the LocalDate class is the correct class to use.

If you are only interested in the month, not the day of month, use YearMonth instead of LocalDate. The code will be the same except for the declaration and initialization:

    YearMonth originDate = YearMonth.of(2019, Month.MAY);
Origin date: 2019-05 - Add 1 Months
2019-06
(etc.)

Don’t use Calendar and GregorianCalendar. Those classes are poorly designed and long outdated. On top of that they carry with each object a time of day, a time zone, a week scheme and more, all of which I don’t think you need and that will only risk confusing those that read your code.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Upvotes: 1

KevinO
KevinO

Reputation: 4403

First, if at all possible consider using the newer Java Date objects. See, e.g., this blog.

However, the specific issue of the OP is that the new GregorianCalendar call will create based upon the current date/time of the user. It is not using the specified date/time of the originDate.

One way to adjust the code would be:

public static Calendar novaData(Calendar originDate, Integer amountMonth) {
    System.out.println("Origin date: " + originDate.getTime() + " - Add " + amountMonth + " Months");
    Calendar date = new GregorianCalendar();
    date.setTime(originDate.getTime());
    date.setTimeZone(originDate.getTimeZone());

    date.add(Calendar.MONTH, amountMonth);
    System.out.println(date.getTime());

    return date;
}

Based upon the test input, the output moves the month forward. See it here

Origin date: Sat May 18 18:01:21 GMT 2019 - Add 0 Months
Sat May 18 18:01:21 GMT 2019
Origin date: Sat May 18 18:01:21 GMT 2019 - Add 1 Months
Tue Jun 18 18:01:21 GMT 2019
Origin date: Sat May 18 18:01:21 GMT 2019 - Add 2 Months
Thu Jul 18 18:01:21 GMT 2019

Please note that issues with leap years, negative amounts, etc. are not fully considered.

Upvotes: 0

Related Questions