Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    private static final int JANUARY = 1;
    private static final int FEBRUARY = 2;
    private static final int MARCH = 3;
    private static final int APRIL = 4;
    private static final int MAY = 5;
    private static final int JUNE = 6;
    private static final int JULY = 7;
    private static final int AUGUST = 8;
    private static final int SEPTEMBER = 9;
    private static final int OCTOBER = 10;
    private static final int NOVEMBER = 11;
    private static final int DECEMBER = 12;

    public static boolean isLegalID(String idNumber) {
        boolean result = idNumber.matches("[0-9]{17}[0-9X]");
        if (result) {
            int year = Integer.parseInt(idNumber.substring(6, 10));
            int month = Integer.parseInt(idNumber.substring(10, 12));
            int date = Integer.parseInt(idNumber.substring(12, 14));

            switch (month) {
            case FEBRUARY:
                result = (date >= 1) && (year % 4 == 0 ? date <= 29 : date <= 28);
                break;
            case JANUARY:
            case MARCH:
            case MAY:
            case JULY:
            case AUGUST:
            case OCTOBER:
            case DECEMBER:
                result = (date >= 1) && (date <= 31);
                break;
            case APRIL:
            case JUNE:
            case SEPTEMBER:
            case NOVEMBER:
                result = (date >= 1) && (date <= 30);
                break;
            default:
                result = false;
                break;
            }
        }
        return result;
    }
}