用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 日期时间处理类

2013-01-11 作者: 海大软件1102班举报

[java]代码库

import java.util.Calendar;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 * Utility to date time
 * @version 0.1
 * @author Super Zhao
 */
public class DateTimeUtil
{

    /**
     * 由java.util.Date到java.sql.Date的类型转换
     * @param date
     * @return Date
     */
    public static Date getSqlDate (java.util.Date date)
    {
        return new Date (date.getTime() );
    }

    public static Date nowDate()
    {
        Calendar calendar = Calendar.getInstance();
        return getSqlDate (calendar.getTime() );
    }

    /**
     * 获得某一日期的后一天
     * @param date
     * @return Date
     */
    public static Date getNextDate (Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime (date);
        int day = calendar.get (Calendar.DATE);
        calendar.set (Calendar.DATE, day + 1);
        return getSqlDate (calendar.getTime() );
    }

    /**
     * 获得某一日期的前一天
     * @param date
     * @return Date
     */
    public static Date getPreviousDate (Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime (date);
        int day = calendar.get (Calendar.DATE);
        calendar.set (Calendar.DATE, day - 1);
        return getSqlDate (calendar.getTime() );
    }

    /**
     * 获得某年某月第一天的日期
     * @param year
     * @param month
     * @return Date
     */
    public static Date getFirstDayOfMonth (int year, int month)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set (Calendar.YEAR, year);
        calendar.set (Calendar.MONTH, month - 1);
        calendar.set (Calendar.DATE, 1);
        return getSqlDate (calendar.getTime() );
    }

    /**
     * 获得某年某月最后一天的日期
     * @param year
     * @param month
     * @return Date
     */
    public static Date getLastDayOfMonth (int year, int month)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set (Calendar.YEAR, year);
        calendar.set (Calendar.MONTH, month);
        calendar.set (Calendar.DATE, 1);
        return getPreviousDate (getSqlDate (calendar.getTime() ) );
    }

    /**
     * 由年月日构建java.sql.Date类型
     * @param year
     * @param month
     * @param date
     * @return Date
     */
    public static Date buildDate (int year, int month, int date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set (year, month - 1, date);
        return getSqlDate (calendar.getTime() );
    }

    /**
     * 取得某月的天数
     * @param year
     * @param month
     * @return int
     */
    public static int getDayCountOfMonth (int year, int month)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set (Calendar.YEAR, year);
        calendar.set (Calendar.MONTH, month);
        calendar.set (Calendar.DATE, 0);
        return calendar.get (Calendar.DATE);
    }

    /**
     * 获得某年某季度的最后一天的日期
     * @param year
     * @param quarter
     * @return Date
     */
    public static Date getLastDayOfQuarter (int year, int quarter)
    {
        int month = 0;
        if (quarter > 4)
        {
            return null;
        }
        else
        {
            month = quarter * 3;
        }
        return getLastDayOfMonth (year, month);

    }

    /**
     * 获得某年某季度的第一天的日期
     * @param year
     * @param quarter
     * @return Date
     */
    public static Date getFirstDayOfQuarter (int year, int quarter)
    {
        int month = 0;
        if (quarter > 4)
        {
            return null;
        }
        else
        {
            month = (quarter - 1) * 3 + 1;
        }
        return getFirstDayOfMonth (year, month);
    }

    /**
     * 获得某年的第一天的日期
     * @param year
     * @return Date
     */
    public static Date getFirstDayOfYear (int year)
    {
        return getFirstDayOfMonth (year, 1);
    }

    /**
     * 获得某年的最后一天的日期
     * @param year
     * @return Date
     */
    public static Date getLastDayOfYear (int year)
    {
        return getLastDayOfMonth (year, 12);
    }

    /**
     * String到java.sql.Date的类型转换
     * @param param
     * @return Date
     */
    public static java.sql.Date StringToDate (String param)
    {
        if (StringUtil.nullOrBlank (param) )
        {
            return null;
        }
        else
        {
            java.util.Date date = null;
            try
            {
                SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
                date = sdf.parse (param);
                return new Date (date.getTime() );
            }
            catch (ParseException ex)
            {
                //ex.printStackTrace();
                return null;
            }
        }
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...