Posted by: piman007 | 11-12-2007

Compare Dates

/*
 * Dates.java
 *
 * Created on December 11, 2007, 11:19 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.kcs.iservice.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 *
 * @author Piman Vajalobol
 */
public class Dates {
   
    /** Creates a new instance of Dates */
    public Dates() {
    }
    public int compare(String strDate1,String strDate2,String strFormat){
        int intCompare = 0;
        /*
         * if equals return 0
         * if before ( < ) return -1
         * if after ( > ) retrurn 1
         * before(-1) —— equals(0) —– after(1)
         */
        DateFormat df = new SimpleDateFormat(strFormat);
        try {
            // Get Date 1
            Date d1 = df.parse(strDate1);
            // Get Date 2
            Date d2 = df.parse(strDate2);           
            if (d1.equals(d2)){
              intCompare = 0;
            }else if (d1.before(d2)){
              intCompare = -1;
            }else{
              intCompare = 1;
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return intCompare;
    }   
}

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

หมวดหมู่

%d bloggers like this: