DateUtils工具类

来源:互联网 发布:淘宝返现一淘 编辑:程序博客网 时间:2024/04/27 21:53
/**   * @Title: Demo.java * @Package org.yuzheng.date * @Description: TODO * @author yuzheng.xia * @date 2014-1-8 下午10:44:40 * @version V1.0   */package org.yuzheng.utils;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.sql.Timestamp;/** * @ClassName: Demo * @Description: 对于时间日期的格式化转换 * @author yuzheng.xia * @date 2014-1-8 下午10:44:40 */public class DateUtil{//获得SimpleDateFormatpublic static SimpleDateFormat getSimpleDateFormat(){String pattern = "yyyy-MM-dd HH:mm:ss";return new SimpleDateFormat(pattern);}//将字符串转换成datepublic static Date convertDate(String time) throws Exception{return getSimpleDateFormat().parse(time);}//将long 转换成 datepublic static Date convertDate(long time) throws Exception{return getSimpleDateFormat().parse(getSimpleDateFormat().format(new Date(time)));}//将date类型的日期格式化public static String convertDateFormat(Date time) throws Exception{return getSimpleDateFormat().format(time);}//将date类型转换成Timestamp -- 接收一个Date类型的日期    public static Timestamp convertTimestamp(Date time) throws Exception    {    //传了一个String字符串日期     return convertTimestamp(getSimpleDateFormat().format(time));    }        //将String类型转换成Timestamp -- 接收一个字符串日期    public static Timestamp convertTimestamp(String time) throws Exception    {    //传了一个long类型的日期    return new Timestamp(getSimpleDateFormat().parse(time).getTime());    }        //将long类型转换成Timestamp -- 接收一个long日期    public static Timestamp convertTimestamp(long time) throws Exception    {    //传了一个Date类型的日期    return convertTimestamp(new Date(time));    }}


0 0