-4

We have a requirement of converting date string "ddMMM" into "yyyy-MM-dd".

For example: "30SEP" -->"2019-09-30" and "15JAN" --> "2020-01-15" (if the date is in past for current year then take next years date)

Java_Prof
  • 49
  • 1
  • 3
  • 1
    @Evan Mhm, looks just like one of these "do my work for me" questions from newbies. I'm a bit tired of that. And IMHO answering does not help... – Robert Apr 22 '19 at 21:32

3 Answers3

1

DateTimeFormatter is a good tool for parsing date and time, but it requires a year as input. This takes the current year, but then adds a year if the date outputted is before now.

    String dateString = "29FEB";
    DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("ddMMMyyyy").toFormatter();
    LocalDate dateOfInterest = LocalDate.parse( dateString + LocalDate.now().getYear(), dateTimeFormatter );
    if( dateOfInterest.isBefore( LocalDate.now() ) )
        dateOfInterest = LocalDate.parse( dateString + LocalDate.now().plusYears(1).getYear(), dateTimeFormatter );

Then the LocalDate can be converted to string in the format you are looking for as follows.

dateOfInterest.format( DateTimeFormatter.ofPattern( "yyy-MM-dd" ) );
Evan
  • 2,441
  • 23
  • 36
  • 1
    I suspect this code fails for the 29th of February. But I’ve not yet verified that problem. – Basil Bourque Apr 23 '19 at 00:07
  • You are correct, the result for 29FEB when in year 2019 returns 2020-02-28 as originally written. Updating accordingly. Wasn't aware of MonthDay, so thanks for showing me something new in your answer. – Evan Apr 23 '19 at 15:04
1

MonthDay

The Answer by Evan is good, but I would suggest parsing your input for what it is: a month-day. There’s a class for that! See MonthDay.

DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern( "ddMMM" ).toFormatter() ;
MonthDay md = MonthDay.parse( input , f ) ;

Now determine a date. For that you need a time zone. For any given moment, the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;

Compare.

LocalDate target = md.atYear( today.getYear() ) ;
if( target.isBefore( today ) ) {
    target.plusYears( 1 ) ;
} 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Upvoting since I learned something new (MonthDay class), but I believe you'll need to handle the same 29th of February issue? What I observed is that Java will turn your local date of 29 Feb 2019 into 28 Feb 2019. So replacing target.plusYears( 1 ) with md.atYear( today.plusYears(1).getYear() ) might be a more robust solution. – Evan Apr 23 '19 at 15:12
  • Java_prof, my answer using LocalDate.now() without zone works if the computer running this code is in the zone you want to infer, because that overload uses the system time zone. But if not, Basil's code where he explicitly sets the zone would be necessary. – Evan Apr 23 '19 at 15:16
  • I recommend *always* passing explicitly a time zone. Even if you want the JVM’s current default time zone, make a call to `ZoneId.systemDefault`. Otherwise the code implicitly relying on the default is ambiguous, as so many programmers are either ignorant of time zone issues or forget about accounting for time zone issues. – Basil Bourque Apr 23 '19 at 18:10
-1

You can take input of only Date and Month as a string array. Get current date using Now you have both values... compare both and add the year to the date.

import java.util.Calendar;
import java.util.Scanner;

public class DateCalendar {
    public static void main(String ar[]){
        String[] monthName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"};
        int count = 0;
        int presentMonth =0;
        int userMonth = 0;
        int finalYear = 0;


        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Date :");
        int userdate = sc.nextInt();
        System.out.println("Enter Month :");
        String usrMonth = sc.next();

        Calendar now = Calendar.getInstance();
        int presentDate = now.get(Calendar.DATE);
        presentMonth = (now.get(Calendar.MONTH))+1;

        //Convert user input Month String to int
        count = 0;
        while(count<12){
            if(monthName[count].equals(usrMonth)){
                userMonth = count + 1;
                // array starts from 0
                break;
            }
            else{
                count++;
            }
        }

        //convert to final year and 
        if(userMonth<presentMonth){
            finalYear =  (now.get(Calendar.YEAR))+1;
        }
        else if(userMonth>presentMonth){
            System.out.println("THe userMonth is : "+userMonth+" and presentMonth is : "+presentMonth);
            finalYear = now.get((Calendar.YEAR));
        }
        else if(userMonth==presentMonth){
            if(userdate>=presentDate){
                finalYear = now.get((Calendar.YEAR));
            }
            else if(userdate<presentDate){
                finalYear =  (now.get(Calendar.YEAR))+1;
            }
        }

        System.out.println("THe final year is : "+finalYear);
        System.out.println("THe final month is "+monthName[userMonth-1]);
        System.out.println("the final date is : "+userdate);
    }
}`

`