java时间处理工具类总结

package org.util;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/

public class DateFormat
{
    public static final Date getDate(String dateString, String pattern)
    {
        SimpleDateFormat format=new SimpleDateFormat(pattern);
        Date date = null;
        try
        {
            date = format.parse(dateString);
        }
        catch (Exception e)
        {
        }
        return date;
    }
    public static final String getDate(Date date, String pattern)
    {
        SimpleDateFormat df = null;
        String returnValue =
"";
        if (date != null)
        {
            df = new SimpleDateFormat(pattern);
            try
            {
                returnValue = df.format(date);
            }
            catch(Exception e)
            {}
        }
        return (returnValue);
    }
    
/**
     * 返回小时
     * @param date Date
     * @return int
     */

    public static int getHour(Date date)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return c.get(Calendar.HOUR_OF_DAY);
        }
        else
        {
            return 0;
        }
    }
    
/**
     * 返回分钟
     * @param date Date
     * @return int
     */

    public static int getMinute(Date date)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return c.get(Calendar.MINUTE);
        }
        else
        {
            return 0;
        }
    }
    
/**
     * 返回秒钟
     * @param date Date
     * @return int
     */

    public static int getSecond(Date date)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return c.get(Calendar.SECOND);
        }
        else
        {
            return 0;
        }
    }
    
/**
     * 返回毫秒
     * @param date Date
     * @return long
     */

    public static long getMillis(Date date)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return c.getTimeInMillis();
        }
        else
        {
            return 0;
        }
    }
    
/**
     * 日期相减
     * @param date Date
     * @param day int
     * @return Date
     */

    public static Date diffDate(Date date, int day)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
            return c.getTime();
        }
        else
        {
            return new Date();
        }
    }
    
/**
     * 日期相加
     * @param date Date
     * @param day int
     * @return Date
     */

    public static Date addDate(Date date, int day)
    {
        if (date != null)
        {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
            return c.getTime();
        }
        else
        {
            return new Date();
        }
    }
    
/**
     *
     * @param date Date
     * @param field int 1年 2月 3周 5天
     * @param amount int
     * @return Date
     */

    public static Date addDate(Date date, int field,int amount)
    {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(field, amount);
        return gc.getTime();
    }
    public static String getYearWeek(Date date, int diff)
    {
        if (date != null)
        {
            Date d = diffDate(date, diff);
            return getDate(date,
"yyyy") + getWeekOfYearByString(d);
        }
        else
        {
            return null;
        }
    }
    public static int getWeekOfYear(Date date)
    {
        if (date != null)
        {
            Calendar c = new GregorianCalendar();
            c.setFirstDayOfWeek(Calendar.MONDAY);
            c.setMinimalDaysInFirstWeek(7);
            c.setTime(date);
            return c.get(Calendar.WEEK_OF_YEAR);
        }
        else
        {
            return 0;
        }
    }
    public static String getWeekOfYearByString(Date date)
    {
        if (date != null)
        {
            Calendar c = new GregorianCalendar();
            c.setFirstDayOfWeek(Calendar.MONDAY);
            c.setMinimalDaysInFirstWeek(7);
            c.setTime(date);
            int week = c.get(Calendar.WEEK_OF_YEAR);
            if (week > 9) {
                return week +
"";
            } else {
                return
"0" + week;
            }
        }
        else
        {
            return null;
        }
    }
private int getWeek(Date date)
    {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }
    private int getDayOfMonth(Date date)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);

    }
}
lunzi   2008-01-11 23:38:29 评论:0   阅读:927   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog