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)
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)
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" ) );
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 ) ;
}
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);
}
}`
`