0

I am attempting to validate an expiry date for a given payment card (in my Android App). The user enters the expiry date using this month picker: https://github.com/premkumarroyal/MonthAndYearPicker

The date is stored in my EditText in MM/yy format. I now want to ensure that the date is not before todays date. To do so, i am trying to find out the last day of the month chosen and comparing it to today's date to see if the chosen day is before todays date (hence expired).

I was wondering how I can do this? Any help would be appreciated.

cardExpiryDate.getText().toString() 

Gets the date selected by the user and converts into a string so its in the following format -> MM/yy

EDIT: It isn't a duplicate of the suggested link as that link compares two dates (which include days) whereas I have to convert a string in MM/yy form to dd/MM/yy (so that dd is the last date of that month) and then compare it to today's date.. I hope that made sense.

Akshat S
  • 1
  • 3

2 Answers2

0

I ended up fixing this question by comparing just the months of the user input date and todays date instead of comparing the entire date.

Akshat S
  • 1
  • 3
0

YearMonth class

Parse your string input as a YearMonth object.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/uu" ) ;
YearMonth expiry = YearMonth.parse( input , f ) ;

Capture today’s date.

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

Get the year-month of today.

YearMonth currentYearMonth = YearMonth.from( today ) ;
Boolean isExpired = currentYearMonth.isAfter( expiry )  ;

Notice that we don’t care about the last day of month you mentioned in the Question.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154