/*
 * @(#)Command.java	1.1.1 2002/11/20
 *
 * Copyright 2001-2002 by BeiJing Camelot, Inc., CHINA
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Camelot, Inc.  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Camelot.
 */
package com.coolservlets.beans.method;

import java.text.*;
import java.util.*;

/**
 * Class for convert all kinds of data format
 * @author      yulixin
 * @version 	1.1.1, 2001/02/27
 * @since       JDK1.2, JSDK 2.0
 */
public class DateConvertor
{
    /**
     * chage null String to ""
     * @param String strParm:string value to be processed
     * @return String the processed value
     */
    public static String changeNull(String strParm)
    {
        String strReturn = "";

        if(strParm != null)
        {
            strReturn = strParm;
        }
        return strReturn;
    }

    /**
     * formate DB Date (YYYYMMDD -> YYYY/MM/DD)
     * @param  String strRawDate:the value to be processed
     * @return String precessed value
     */
    public static String formatDBDate(String strRawDate)
    {
        String strReturn;
        String strTemp;
        strTemp = changeNull(strRawDate);
        strTemp = strTemp.trim();
        if ( strTemp.length() < 8 )
            strReturn = "";
        else
        {
            strReturn = strRawDate.substring(0,4);
            strReturn = strReturn + "/";
            strReturn = strReturn + strRawDate.substring(4,6);
            strReturn = strReturn + "/";
            strReturn = strReturn + strRawDate.substring(6,8);
        }
        return strReturn;
    }
    /**
     * fromate DB Date (YYYYMM -> YYYY/MM)
     * @param  String strRawDate:the value to be processed
     * @return String precessed value
     */
    public static String formatDBDateEx(String strRawDate)
    {
        String strReturn;
        String strTemp;

        strTemp = changeNull( strRawDate );
        if ( strTemp.length() < 6 )
            strReturn = "";
        else
        {
            strReturn = strRawDate.substring(0,4);
            strReturn = strReturn + "/";
            strReturn = strReturn + strRawDate.substring(4,6);
        }
        return strReturn;
    }
    /**
     * formate DB Date (YYYY-MM-DD HH:MM:SS.SSSSSS -> YYYY/MM/DD HH:MM:SS)
     * @param  String strRawDate:the value to be processed
     * @return String precessed value
     */
    public static String formatDBDateTime(String strRawDate)
    {
        String strReturn;

        if (strRawDate == null) return "";

        if (strRawDate.length() < 19) return "";

        strReturn = strRawDate.substring(0,4) + "/";
        strReturn = strReturn + strRawDate.substring(5,7) + "/";
        strReturn = strReturn + strRawDate.substring(8,19);
        return strReturn;
    }

    /**
     * formate Form Date (YYYY/MM/DD -> YYYYMMDD)
     * @param  String strRawDate:the value to be processed
     * @return String precessed value
     */
    public static String formatFormDate(String strRawDate)
    {
        String strReturn;
        String strTemp;

        strTemp = changeNull( strRawDate );
        strTemp = strTemp.trim();
        if ( strTemp.length() < 10 )
            strReturn = "";
        else
        {
            strReturn = strRawDate.substring(0,4);
            strReturn = strReturn + strRawDate.substring(5,7);
            strReturn = strReturn + strRawDate.substring(8,10);
        }

        return strReturn;
    }

    /**
     * get current date (YYYY/MM/DD)
     * @return String current date value
     */
    public static String getCurrentDate()
    {
        // Format the current time.
        SimpleDateFormat formatter
                = new SimpleDateFormat ("yyyy/MM/dd");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * get current date (YY-MM-DD HH24:MI:SS )
     * @return String current date value
     */
    public static String getCurrentDateTime() {
        java.util.Date sysDate = new java.util.Date();
        int iYear = sysDate.getYear() + 1900;
        int iMonth = sysDate.getMonth() + 1;
        int iDay = sysDate.getDate();
        int iHour = sysDate.getHours();
        int iMinute = sysDate.getMinutes();
        int iSeconds = sysDate.getSeconds();
        String dateString = Integer.toString(iYear)+"-"+Integer.toString(iMonth)+"-"+Integer.toString(iDay)+" "+Integer.toString(iHour)+":"+Integer.toString(iMinute)+":"+Integer.toString(iSeconds);
        // Format the current time.
        return dateString;
    }

    /**
     * formate Form Date (YYYY/MM/DD -> YYYY-MM-DD)
     * @param  String strDate:the value to be processed
     * @return String precessed value
     */
    public static String toDB2Date(String strDate)
    {
        String strReturn;
        String strTemp;

        strTemp = changeNull( strDate );
        if ( strTemp.length() < 10 )
            strReturn = "";
        else
        {
            strReturn = strTemp.replace('/', '-');
        }

        return strReturn;
    }
    /**
     * frmate Form Date (YYYY-MM-DD -> YYYY/MM/DD)
     * @param  String strDate:the value to be processed
     * @return String precessed value
     */
    public static String toFormDate(String strDate)
    {
        String strReturn;
        String strTemp;

        strTemp = changeNull( strDate );
        if ( strTemp.length() < 10 )
            strReturn = "";
        else
        {
            strReturn = strTemp.replace('-', '/');
        }
        return strReturn;
    }
    /**
     * frmate Form Date (YYYY-M-D -> YYYY-MM-DD)
     * @param  String strDate:the value to be processed
     * @return String precessed value
     */
    public static String FormDate(String strDate)
    {
        String strReturn;
        String strTemp;

        strTemp = changeNull( strDate );
        if ( strTemp.length() < 10 )
            strReturn = "";
        else
        {
            strReturn = strTemp.replace('-', '/');
        }
        return strReturn;
    }
    /**
     * frmate Form Date (YY-MM-DD(YY-M-DD) -> YYMMDD)
     * @param  String strDate:the value to be processed
     * @return String precessed value
     */
    public static String toFormDate(String strDate,String strSpear)
    {
        String strReturn;
        String sYear  = genMethod.readValueStr(strDate,strSpear,0);
        String sMonth = genMethod.readValueStr(strDate,strSpear,1);
        sMonth = (sMonth.length() == 1) ? "0"+sMonth :sMonth;
        String sDate  = genMethod.readValueStr(strDate,strSpear,2);
        if(sDate.indexOf(" ",0) > -1){
            sDate  = genMethod.readValueStr(sDate," ",0);
            sDate = (sDate.length() == 1) ? "0"+sDate :sDate;
        }
        strReturn = sYear + sMonth + sDate;
        return strReturn;
    }
}