package wenrgise.common.utility;

import java.sql.Date;
import java.util.Calendar;
import java.util.StringTokenizer;

public class DateUtility {
  public final int DATE = 1;
  
  public final int MONTH = 2;
  
  public final int YEAR = 3;
  
  public String getSysDate() {
    Calendar cal = Calendar.getInstance();
    String sysDate = String.valueOf(String.valueOf(String.valueOf(String.valueOf(lPad(String.valueOf(cal.get(5)), 2)).concat(String.valueOf("/"))).concat(String.valueOf(lPad(String.valueOf(cal.get(2) + 1), 2)))).concat(String.valueOf("/"))).concat(String.valueOf(lPad(String.valueOf(cal.get(1)), 4)));
    cal = null;
    return sysDate;
  }
  
  public int dateDiff(String fromDate, String toDate) {
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(get(fromDate, 3)), Integer.parseInt(get(fromDate, 2)) - 1, Integer.parseInt(get(fromDate, 1)));
    long fromDateInMilliSec = cal.getTime().getTime();
    cal.set(Integer.parseInt(get(toDate, 3)), Integer.parseInt(get(toDate, 2)) - 1, Integer.parseInt(get(toDate, 1)));
    long toDateInMilliSec = cal.getTime().getTime();
    cal = null;
    if (fromDateInMilliSec == toDateInMilliSec)
      return 1; 
    int noOfDays = Math.abs((int)((toDateInMilliSec - fromDateInMilliSec) / 86400000L));
    return noOfDays;
  }
  
  public int dateDiff1(String fromDate, String toDate) {
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(get(fromDate, 3)), Integer.parseInt(get(fromDate, 2)) - 1, Integer.parseInt(get(fromDate, 1)));
    long fromDateInMilliSec = cal.getTime().getTime();
    cal.set(Integer.parseInt(get(toDate, 3)), Integer.parseInt(get(toDate, 2)) - 1, Integer.parseInt(get(toDate, 1)));
    long toDateInMilliSec = cal.getTime().getTime();
    cal = null;
    int noOfDays = Math.abs((int)((toDateInMilliSec - fromDateInMilliSec) / 86400000L));
    return noOfDays;
  }
  
  public long dateDiffLong(String fromDate, String toDate) {
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(get(fromDate, 3)), Integer.parseInt(get(fromDate, 2)) - 1, Integer.parseInt(get(fromDate, 1)));
    long fromDateInMilliSec = cal.getTime().getTime();
    cal.set(Integer.parseInt(get(toDate, 3)), Integer.parseInt(get(toDate, 2)) - 1, Integer.parseInt(get(toDate, 1)));
    long toDateInMilliSec = cal.getTime().getTime();
    cal = null;
    long noOfDaysInSec = (toDateInMilliSec - fromDateInMilliSec) / 1000L;
    return noOfDaysInSec;
  }
  
  public int dateGTcurrent(String fromDate, String toDate) {
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(get(fromDate, 3)), Integer.parseInt(get(fromDate, 2)) - 1, Integer.parseInt(get(fromDate, 1)));
    long fromDateInMilliSec = cal.getTime().getTime();
    cal.set(Integer.parseInt(get(toDate, 3)), Integer.parseInt(get(toDate, 2)) - 1, Integer.parseInt(get(toDate, 1)));
    long toDateInMilliSec = cal.getTime().getTime();
    cal = null;
    int noOfDays = (int)((toDateInMilliSec - fromDateInMilliSec) / 86400000L);
    return noOfDays;
  }
  
  public String get(String inDate, int Type) {
    String[] strArr = new String[4];
    String retVal = "";
    strArr = getDateArray(inDate);
    switch (Type) {
      case 3:
        retVal = lPad(strArr[2], 4);
        break;
      case 2:
        retVal = lPad(strArr[1], 2);
        break;
      case 1:
        retVal = lPad(strArr[0], 2);
        break;
    } 
    return retVal;
  }
  
  public String reverseFormat(String tDate) {
    String[] strArr = new String[4];
    strArr = getDateArray(tDate);
    return String.valueOf(String.valueOf(String.valueOf(String.valueOf(lPad(strArr[1], 2)).concat(String.valueOf("/"))).concat(String.valueOf(lPad(strArr[0], 2)))).concat(String.valueOf("/"))).concat(String.valueOf(lPad(strArr[2], 4)));
  }
  
  private String lPad(String inStr, int len) {
    int count = len - inStr.length();
    for (int i = 0; i < count; i++)
      inStr = String.valueOf("0").concat(String.valueOf(inStr)); 
    return inStr;
  }
  
  private String[] getDateArray(String tDate) {
    int j = 0;
    StringTokenizer strTok = new StringTokenizer(tDate, "/");
    String[] strArr = new String[4];
    if (strTok.countTokens() <= 0)
      strTok = new StringTokenizer(tDate, "-"); 
    while (strTok.hasMoreTokens()) {
      strArr[j] = strTok.nextToken();
      j++;
    } 
    return strArr;
  }
  
  public long getDateLong(String tDate) {
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(get(tDate, 3)), Integer.parseInt(get(tDate, 2)) - 1, Integer.parseInt(get(tDate, 1)));
    return cal.getTime().getTime();
  }
  
  public long[] months_between(String fromDate, String toDate) {
    long[] retArr = new long[2];
    retArr[0] = -1L;
    retArr[1] = -1L;
    long noOfDaysInSecs = dateDiffLong(fromDate, toDate);
    double totNoOfDays = (noOfDaysInSecs / 86400L);
    retArr[0] = (long)Math.floor(totNoOfDays / 30);
    retArr[1] = Math.round(totNoOfDays % 30);
    return retArr;
  }
  
  public long[] months_between1(String fromDate, String toDate) {
    long[] retArr = new long[2];
    retArr[0] = -1L;
    retArr[1] = -1L;
    long noOfDaysInSecs = dateDiffLong(fromDate, toDate);
    double totNoOfDays = (noOfDaysInSecs / 86400L);
    retArr[0] = (long)Math.floor(totNoOfDays / 30.4D);
    retArr[1] = Math.round(totNoOfDays % 30);
    return retArr;
  }
  
  public static void main(String[] args) {
    DateUtility d = new DateUtility();
    System.out.println(d.reverseFormat(d.getSysDate()));
    System.out.println(d.dateDiff1("29/11/2003", "2/12/2003"));
    System.out.println(d.dateDiff1("29/10/2003", "2/11/2003"));
    d.getSysDate();
    long[] retIntArr = d.months_between("12/12/2003", "28/12/2003");
    System.out.println(String.valueOf(String.valueOf(retIntArr[0]).concat(String.valueOf("*****"))).concat(String.valueOf(retIntArr[1])));
    Date d1 = new Date(d.getDateLong("29/11/2003"));
    Date d2 = new Date(d.getDateLong("2/12/2003"));
    System.out.println(d1.compareTo(d2));
  }
}
