-1

I am currently working on an issue where DateTime.addMonths(iStartDateH, durationAsInt) is adding an extra day. It uses GeorgianCalendar internally. We are using Java 5 currently in this project Eg: For 24 months

    ExpirationDate=DateTime.addMonths(currentDate, 24)
    CurrentDate= 01/02/2021 (dd/mm/yyyy format)
    ExpirationDate= 02/02/2023
public static ErrorCode addMonths(DateHolder dateH, int numMonths) {
    try {
        Calendar c = new GregorianCalendar();
        c.setTime(dateH.value);
        c.add(Calendar.MONTH, numMonths);
        dateH.value = c.getTime();
        return ErrorCode.successCN;
    }
    catch (Exception e) {
        IlMessage msg = new IlMessage(Msg.exceptionCaughtCN, e);
        IlSession.getSession().getMessageStack().push(msg);
        return ErrorCode.errorCN;
    }
}

I tried checking the complete date/time difference and its coming as 730.773935185185185 Please help with the same.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • What is this `DateTime` that you are talking about? `java.time.LocalDateTime`? `java.util.Date`? Can you give its fully qualified name? – Sweeper Feb 03 '21 at 03:35
  • How are determing that the difference is 730.773935185185185? Is `DateHolder` your own class or a class from some 3rd party library? Which one? – Ole V.V. Feb 03 '21 at 07:06
  • 1
    Java 5? Poor people. Any chance you could move to a newer Java version? – MC Emperor Feb 03 '21 at 10:44
  • 1
    @SugandhaMahajan You have to share your `DateHolder` class with us. Without it, we can't tell what the problem may be. I tested the addition of the months with the `GregorianCalendar`, and I cannot reproduce the anomaly you are observing. – MC Emperor Feb 03 '21 at 11:01
  • Again, you should upgrade to a newer Java version. Java 5 was released [in 2004](https://en.wikipedia.org/wiki/Java_(programming_language)#Versions), and was declared end-of-life [in 2009](https://en.wikipedia.org/wiki/Java_version_history#Java_5_updates), which is already a long, long time ago. Of course, it may be the case that you're not the one to make that decision. But still... – MC Emperor Feb 03 '21 at 11:25
  • Further, I see your `DateHolder` class contains a `public` field, which is generally [discouraged](https://stackoverflow.com/questions/23209663/when-is-using-public-fields-acceptable). Also, I think you should consider using [immutable objects](https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html). – MC Emperor Feb 03 '21 at 11:25

2 Answers2

1

I am using Java 8 and I tried the code below and it worked just fine for me (for testing purposes I set the date as Feb 1 as from your example.

    public static void main(String...pStrings) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate currentDate = LocalDate.of(2021, 2, 1); //LocalDate.now();
        System.out.println("Original Date -" +currentDate.format(formatter));
        
        LocalDate newDate = currentDate.plusMonths(24);
        System.out.println("updated date - " + newDate.format(formatter));
    }

I received the output: -

Original Date -01/02/2021
updated date - 01/02/2023
Fabio Mendes Soares
  • 1,357
  • 5
  • 20
  • 30
bhaskar
  • 21
  • 3
1
  1. Note that m is for minutes. For a month, you need to use M.
  2. The implementation of your class, DateHolder seems to have a problem. There is no such problem with java.util date-time API for this requirement.

Demo:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse("01/02/2021"));
        System.out.println(calendar.getTime());
        int numMonths = 24;
        calendar.add(Calendar.MONTH, numMonths);
        System.out.println(calendar.getTime());
    }
}

Output:

Mon Feb 01 00:00:00 GMT 2021
Wed Feb 01 00:00:00 GMT 2023
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110