Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

public class Main {

    public static int compareDate(String aDate, String bDate) {

        String[] strDate;
        GregorianCalendar aCal = new GregorianCalendar();
        GregorianCalendar bCal = new GregorianCalendar();
        if (aDate.indexOf("/") != -1) {
            strDate = aDate.split("/");
            aCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                    Integer.parseInt(strDate[2].trim()));
        } else if (aDate.indexOf("-") != -1) {
            strDate = aDate.split("-");
            aCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                    Integer.parseInt(strDate[2].trim()));
        } else if (aDate.length() == 8) {
            aCal.set(Integer.parseInt(aDate.substring(0, 4)), Integer.parseInt(aDate.substring(4, 6)) - 1,
                    Integer.parseInt(aDate.substring(6, 8)));
        } else if (aDate.length() == 10) {
            aCal.set(Integer.parseInt(aDate.substring(0, 4)), Integer.parseInt(aDate.substring(5, 7)) - 1,
                    Integer.parseInt(aDate.substring(8, 10)));
        }

        if (bDate.indexOf("/") != -1) {
            strDate = bDate.split("/");
            bCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                    Integer.parseInt(strDate[2].trim()));
        } else if (bDate.indexOf("-") != -1) {
            strDate = bDate.split("-");
            bCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                    Integer.parseInt(strDate[2].trim()));
        } else if (bDate.length() == 8) {
            bCal.set(Integer.parseInt(bDate.substring(0, 4)), Integer.parseInt(bDate.substring(4, 6)) - 1,
                    Integer.parseInt(bDate.substring(6, 8)));
        } else if (bDate.length() == 10) {
            bCal.set(Integer.parseInt(bDate.substring(0, 4)), Integer.parseInt(bDate.substring(5, 7)) - 1,
                    Integer.parseInt(bDate.substring(8, 10)));
        }

        return aCal.compareTo(bCal);
    }
}