//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.xpdl.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class DateTools
{
  //-----------------------------------------------------------------
  //-----------------------------------------------------------------
  public static String toString(java.util.Date date)
  {
    //yyyy-mm-dd
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    formatter.setTimeZone(TimeZone.getDefault());
    return formatter.format(date);
  }
  public static String toString(java.sql.Date date)
  {
    //yyyy-mm-dd
    return date.toString();
  }
  public static String toString(java.sql.Time date)
  {
    //hh:mm:ss
    return date.toString();
  }
  public static String toString(java.sql.Timestamp date)
  {
    //yyyy-mm-dd hh:mm:ss
    String dateString = date.toString();
    int posIndex = dateString.lastIndexOf(".");
    return dateString.substring(0, posIndex);
  }

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------   
  public static java.sql.Date toSqlDate(java.sql.Timestamp ts)
  {
    if(ts==null) return null;
    
    return new java.sql.Date(ts.getTime());
  }
  
  public static java.sql.Date toSqlDate(java.sql.Time ts)
  {
    if(ts==null) return null;
    
     return new java.sql.Date(ts.getTime());
  }
   
  public static java.sql.Date toSqlDate(java.sql.Date dat)
  {
    return dat;
  }
    
  public static java.sql.Date toSqlDate(java.util.Date dat)
  {
    if(dat==null) return null;
    
    return new java.sql.Date(dat.getTime());
  }
  
  public static java.sql.Date toSqlDate(String dateString)
  {
    dateString=dateString.trim();
    
    java.sql.Date resultDate = null;
    try
    {
      //try yyyy-mm-dd  --> Date    
      resultDate = java.sql.Date.valueOf(dateString);
    }
    catch(IllegalArgumentException ex)
    {
      try
      {
        //try yyyy/mm/dd  --> Date 
        int year;
        int month;
        int day;
        int firstDash;
        int secondDash;
        firstDash = dateString.indexOf('/');
        secondDash = dateString.indexOf('/', firstDash + 1);
        if ((firstDash > 0) & (secondDash > 0) & (secondDash < dateString.length() - 1))
        {
          year = Integer.parseInt(dateString.substring(0, firstDash)) - 1900;
          month = Integer.parseInt(dateString.substring(firstDash + 1, secondDash)) - 1;
          day = Integer.parseInt(dateString.substring(secondDash + 1));
        }
        else
        {
          throw new java.lang.IllegalArgumentException();
        }
    
        resultDate= new java.sql.Date(year, month, day);
      }
      catch(IllegalArgumentException ex0)
      {
        //try yyyymmdd  --> Date 
        int year;
        int month;
        int day;
        year = Integer.parseInt(dateString.substring(0, 4)) - 1900;
        month = Integer.parseInt(dateString.substring(4, 6)) - 1;
        day = Integer.parseInt(dateString.substring(6,8));   
        resultDate= new java.sql.Date(year, month, day);
      }

    }

    return resultDate;
  }
//-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- 
 
  public static java.sql.Timestamp toSqlTimestamp(java.util.Date dat)
  {
    if(dat==null) return null;
    
    return new java.sql.Timestamp(dat.getTime());
  }
  
  public static java.sql.Timestamp toSqlTimestamp(String dateString)
  {
    java.sql.Timestamp resultDate =
      java.sql.Timestamp.valueOf(dateString.trim());
    return resultDate;
  }

//-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- 
  
  public static java.sql.Time toSqlTime(java.sql.Timestamp ts)
  {
    return new java.sql.Time(ts.getTime());
  }
  
  public static java.sql.Time toSqlTime(String dateString)
  {
    java.sql.Time resultDate = java.sql.Time.valueOf(dateString.trim());
    return resultDate;
  }
//============================================================================
//============================================================================ 
  public static java.util.Date toJavaDate(java.sql.Date dat)
  {
    if(dat==null) return null;
    
    return new java.util.Date(dat.getTime()); 
  }
  
  public static java.util.Date toJavaDate(java.sql.Time dat)
  {
    if(dat==null) return null;
    
    return new java.util.Date(dat.getTime()); 
  }
  
  public static java.util.Date toJavaDate(java.sql.Timestamp dat)
  {
    if(dat==null) return null;
    
    return new java.util.Date(dat.getTime()); 
  }
//============================================================================
//============================================================================
	public static java.util.Date toJavaDate(String dateString)
	{
		if(dateString==null) return null;
    
		return new java.util.Date(dateString); 
	} 
}