/**
* @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
{
//获得SimpleDateFormat
public static SimpleDateFormat getSimpleDateFormat()
{
String pattern = "yyyy-MM-dd HH:mm:ss";
return new SimpleDateFormat(pattern);
}
//将字符串转换成date
public static Date convertDate(String time) throws Exception
{
return getSimpleDateFormat().parse(time);
}
//将long 转换成 date
public 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));
}
}
DateUtils工具类
最新推荐文章于 2021-12-02 18:55:52 发布