Java utility methods-- to process date.

package JavaUtils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;


/**
 * This class contains utility methods to deal with dates.
 * 
 */
public class DateTimeUtils
{
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String BACKLASH_MM_DD_YYYY = "MM/dd/yyyy";
    public static final String DASH_MMDDYYYY = "yyyy-MM-dd";
    public static final String TIME_STAMP = "yyyy-MM-dd'T'hh:mm:ss:SSSS";


    /**
     * Returns current date and time depending on format string passed in
     */
    public static String formatedCurrentDateTime(String formatterString)
    {
        SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
        Date a_oCurrentTime = new Date();
        return a_oFormatter.format(a_oCurrentTime);
    }


    /**
     * Returns date and time depending on format string and date passed in
     */
    public static String formatedDateTime(String formatterString, Date date)
    {
        String sformat = "";
        if (date != null)
        {
            SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
            sformat = a_oFormatter.format(date);
        }
        return sformat;
    }


    /**
     * Returns a Date object from a simple date format + the date
     */
    public static Date getDateFromDateFormat(String inFormatter, String date)
    {
        Date oDate = null;
        if (date != null)
        {
            SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
            try
            {
                oDate = a_oFormatter.parse(date);
            }
            catch (ParseException ex)
            {
              //  UtilsTrace.catching(DateTimeUtils.class, "getDateFromDateFormat", ex);
            }
        }
        return oDate;
    }


    /**
     * Converts one date format to another
     */
    public static String convertDateTime(String inFormatter, String date, String outFormatter)
    {
        String newFormatedDate = "";
        if (date != null)
        {
            SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
            Date oDate = null;
            try
            {
                oDate = a_oFormatter.parse(date);
                newFormatedDate = DateTimeUtils.formatedDateTime(outFormatter, oDate);
            }
            catch (ParseException ex)
            {
                //UtilsTrace.catching(DateTimeUtils.class, "convertDateTime", ex);
            }
        }
        return newFormatedDate;
    }


    /**
     * Returns the current EST time, used in histroy page.
     */
    public static Date getCurrentEstTime()
    {
        TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
        GregorianCalendar estDate = new GregorianCalendar (est);
       
        return estDate.getTime();
    }


    /**
     * Returns a string formatted
     */
    public static String formatEstDate(Date date)
    {
        TimeZone est = TimeZone.getTimeZone ( "America/New_York" );


        DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
        dateFormatter.setTimeZone(est);
        return dateFormatter.format(date);
    }


    /**
     * Returns current EST Timestamp as string in format yyyy-MM-dd'T'hh:mm:ss:SSSS
     */
    public static String getCurrentEstTimeStamp()
    {
        TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
        SimpleDateFormat dateFormatter = new SimpleDateFormat(TIME_STAMP, Locale.US);
        dateFormatter.setTimeZone(est);
        return dateFormatter.format(getCurrentEstTime());
    }
    
    public static void main(String[] args){
    // this will get China Standard time, seems that will base the server time zone.
        System.out.println(getCurrentEstTime()); //Tue Nov 19 16:52:17 CST 2013
        System.out.println(getCurrentEstTimeStamp());//2013-11-19T03:52:17:0264
        System.out.println(formatEstDate(getCurrentEstTime()));//Tuesday, November 19, 2013 3:52:17 AM EST
        // format a string date from one pattern to another.       
        System.out.println(convertDateTime(YYYYMMDD,"20131119",BACKLASH_MM_DD_YYYY));
        
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值