newlogic / smartscanner-core

ID scanning Android app and library. Supports MRZ, NFC, Barcodes, and ID PASS Lite cards.
Apache License 2.0
70 stars 19 forks source link

Handle date parsing for 2 digits year #140

Closed megaxayda closed 1 year ago

megaxayda commented 1 year ago

Issue

Date parsing for 2 digits when using SimpleDateFormat returns unexpected birth date Example: yyMMdd 420101 -> dd/MM/yyyy 01/01/2042

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Solution:

Add a starting year by using SimpleDateFormat.set2DigitYearStart, the exact year will be calculated be a threshold number.

Example:

240101 -> 01/01/1924
230101 -> 01/01/2023
740101 -> 01/01/1974
730101 -> 01/01/2073

You can quick check the logic here in this online editor link

Changes

  1. Add new func stringToDate2DigitsYear to handle 2 digits year.
  2. Add threshold number for birth date and expiry date.
  3. Add new param to where it's using the default format yyMMdd