Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle date parsing for 2 digits year #140

Merged
merged 1 commit into from
Mar 3, 2023
Merged

Conversation

megaxayda
Copy link
Collaborator

@megaxayda megaxayda commented Mar 2, 2023

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:

  • Birth Date: Start date will be 99 years ago from the present year e.g. 2023 - 99 = 1924. It means the lowest year will be 1924 and highest will be 2023.
240101 -> 01/01/1924
230101 -> 01/01/2023
  • Expiry Date: Start date will be 49 years ago from the present year e.g. 2023-49 = 1974.
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

@megaxayda megaxayda requested review from jeremi and nicholemnl March 2, 2023 04:48
@megaxayda megaxayda self-assigned this Mar 2, 2023
@megaxayda megaxayda added the bug Something isn't working label Mar 2, 2023
@megaxayda megaxayda requested a review from haklyray March 2, 2023 04:49
@megaxayda megaxayda merged commit e51d9e5 into develop Mar 3, 2023
@megaxayda megaxayda deleted the fix-parsing-yymmdd branch March 3, 2023 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants