-1

I thank you for possible answers. The problem is, I work with a legacy system with Java 1.4. In a registration form you have the following fields: 'Period' in the mm / yyyy format 'Expiration Day' Concatenate the day with the period and parse for Date.

I need to handle the months with 29 for February and the months of 31 days. Putting 'Expiration Day' = 31, when it is February the parse plays for 03/01/2021 and in the months when it is not 31 the parse plays for the first day of the following month. I need that for these situations the parse takes the last day of the month and not the following month. I have already researched and did not see how to do it by parse itself.

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataDebito = df.parse(31 + "/" + 02 + "/" + 2021); //February is not 31 and I need you to parse it for 2/28/2021 or 2/29/2021 if it was a leap year.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
marcioasan
  • 11
  • 2
  • Olá, porque minha dúvida foi fechada solicitando mais esclarecimentos? Detalhei a situação e expus o que fiz e o objetivo que quero atingir, mais claro que isso não sei. Por favor, esclareça o que precisa ser esclarecido. – marcioasan Apr 09 '21 at 17:50
  • Welcome to Stack Overflow. This is an English language web site, no matter if you or I find that good or not. So your question needs to be clear to readers that understand only English. Why don’t you just use David’s link? It leads you to the Portuguese language Stack Overflow. I am sure your question will be welcome there. – Ole V.V. Apr 09 '21 at 18:27
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 09 '21 at 18:29
  • Ok guys, thanks. I can't use LocalDate due the system is a legacy systemand uses Java 1.4, I can´t change it. – marcioasan Apr 09 '21 at 18:41
  • 1.4? That’s old. I was going to suggest `IsoChronology.INSTANCE.resolveDate(new HashMap<>(Map.of(ChronoField.YEAR, 2021L, ChronoField.MONTH_OF_YEAR, 2L, ChronoField.DAY_OF_MONTH, 31L)), ResolverStyle.SMART)`, but that won’t help you on 1.4. – Ole V.V. Apr 09 '21 at 18:44
  • I had registered for the stackoverflow in Portuguese, I didn't understand why it was for the stackoverflow in English. – marcioasan Apr 09 '21 at 18:45

1 Answers1

0

I will present solutions for different Java versions.

Java 8 and later: java.time

I recommend that you use java.time, the modern Java date and time API, for your date work.

    DateTimeFormatter expirationParser = DateTimeFormatter.ofPattern("MM/uuuu");
     
    String expirationString = "02/2021";
    
    LocalDate expirationDay = YearMonth.parse(expirationString, expirationParser)
            .atEndOfMonth();

    System.out.println(expirationDay);

Output:

2021-02-28

Java 6 and 7: java.time through ThreeTen Backport

Code is the same as above. java.time has been backported to Java 6 and 7 in the ThreeTen Backport project. Link is at the bottom.

Java 1.5: Joda-Time

    DateTimeFormatter expirationParser = DateTimeFormat.forPattern("MM/yyyy");
    
    String expirationString = "02/2021";

    LocalDate expirationDay = YearMonth.parse(expirationString, expirationParser)
            .plusMonths(1) // following month
            .toLocalDate(1) // day 1 of month
            .minusDays(1); // last day of expiration month
    
    System.out.println(expirationDay);

2021-02-28

Java 1.1 through 1.4: Calendar

    DateFormat expirationFormat = new SimpleDateFormat("MM/yyyy");
    
    String expirationString = "02/2021";

    Date expirationMonth = expirationFormat.parse(expirationString);
    Calendar expirationCalendar = Calendar.getInstance();
    expirationCalendar.setTime(expirationMonth);
    expirationCalendar.add(Calendar.MONTH, 1); // 1st of following month
    expirationCalendar.add(Calendar.DATE, -1); // Last day of expiration month

    System.out.println(expirationCalendar.getTime());

Sun Feb 28 00:00:00 CET 2021

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161