Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yyyy");

    public static String formatExpirationDate(String text) {
        try {
            switch (text.length()) {
            case 1:
                int digit = Integer.parseInt(text);

                if (digit < 2) {
                    return text;
                } else {
                    return "0" + text + "/";
                }
            case 2:
                int month = Integer.parseInt(text);
                if (month > 12 || month < 1) {
                    // Invalid digit
                    return text.substring(0, 1);
                } else {
                    return text + "/";
                }
            case 3:
                if (text.substring(2, 3).equalsIgnoreCase("/")) {
                    return text;
                } else {
                    text = text.substring(0, 2) + "/" + text.substring(2, 3);
                }
            case 4:
                Calendar now = getCurrentExpDate();
                String currentYearStr = String.valueOf(now.get(Calendar.YEAR));

                int yearDigit = Integer.parseInt(text.substring(3, 4));
                int currentYearDigit = Integer.parseInt(currentYearStr.substring(2, 3));
                if (yearDigit < currentYearDigit) {
                    // Less than current year invalid
                    return text.substring(0, 3);
                } else {
                    return text;
                }
            case 5:
                // always make the year in the current century
                Calendar now2 = getCurrentExpDate();
                String currentYearStr2 = String.valueOf(now2.get(Calendar.YEAR));
                String yearStr = text.substring(0, 3) + currentYearStr2.substring(0, 2) + text.substring(3, 5);
                Date expiry = simpleDateFormat.parse(yearStr);
                if (expiry.before(now2.getTime())) {
                    // Invalid exp date
                    return text.substring(0, 4);
                } else {
                    return text;
                }
            default:
                if (text.length() > 5) {
                    return text.substring(0, 5);
                } else {
                    return text;
                }
            }

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // If an exception is thrown we clear out the text
        return "";
    }

    private static Calendar getCurrentExpDate() {
        Calendar now = Calendar.getInstance();
        now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), 0, 0, 0);
        return now;
    }
}