Java Date Compare compareDate(String d1, String d2)

Here you can find the source of compareDate(String d1, String d2)

Description

compare Date

License

Apache License

Declaration

public static int compareDate(String d1, String d2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {

    public static int compareDate(String d1, String d2) {

        if (d1.trim().length() > 10) {
            d1 = d1.split(" ")[0];
        }//from  www. j  av a  2  s  .c o m
        if (d2.trim().length() > 10) {
            d2 = d2.split(" ")[0];
        }
        GregorianCalendar date1 = new GregorianCalendar();
        String[] temp1 = d1.split("-");
        date1.set(Calendar.YEAR, Integer.parseInt(temp1[0]));
        date1.set(Calendar.MONTH,
                Integer.parseInt(temp1[1].substring(0, 1).equals("0") ? temp1[1].substring(1) : temp1[1]) - 1);
        date1.set(Calendar.DATE,
                Integer.parseInt(temp1[2].substring(0, 1).equals("0") ? temp1[2].substring(1) : temp1[2]));

        GregorianCalendar date2 = new GregorianCalendar();
        String[] temp2 = d2.split("-");
        date2.set(Calendar.YEAR, Integer.parseInt(temp2[0]));
        date2.set(Calendar.MONTH,
                Integer.parseInt(temp2[1].substring(0, 1).equals("0") ? temp2[1].substring(1) : temp2[1]) - 1);
        date2.set(Calendar.DATE,
                Integer.parseInt(temp2[2].substring(0, 1).equals("0") ? temp2[2].substring(1) : temp2[2]));

        int result = date1.compareTo(date2);
        return result;
    }
}

Related

  1. compareDate(Date first, Date second)
  2. compareDate(Date sourceDate, Date targetDate)
  3. compareDate(Date start, Date end)
  4. compareDate(String begingDate, String endDate, String format)
  5. compareDate(String d1, String d2)
  6. compareDate(String date)
  7. compareDate(String date0, String date1)
  8. compareDate(String date1, File file)
  9. compareDate(String date1, String date2)