在Java的日常开发中,会随时遇到需要对时间处理的情况,有些时候是需要将时间Date转为时间戳,或是需要将时间戳TimeStamp转换为时间,于是在此篇文章中都有详细的写法与转换方式。
废话不多说,直接上代码:
/**
* 时间转化工具
* @author suxuelian
*/
public class DateConvertUtil {
/**
* 根据时间转换为时间戳
* @param date
* @param timestampType 转换类型 0毫秒 1秒
* @return
*/
public long getTimeStamp(Date date,int timestampType)
{
long times = date.getTime();
if (timestampType == 1)
{
times = times/1000L;
}
return times;
}
/**
* 时间戳转时间
* @param timestamp
* @param timestampType 时间戳格式 0毫秒 1秒
* @return
*/
public Date getDateTime(long timestamp,int timestampType)
{
if (timestampType == 1)
{
//如果时间戳格式是秒,需要江时间戳变为毫秒
timestamp = timestamp * 1000L;
}
Date dateTime = new Date(timestamp);
return dateTime;
}
/**
* 格式化传入的时间,将时间转化为指定格式字符串
* @param date
* @param format 时间格式,如:yyyy-MM-dd HH:mm:ss SSS 或 yyyy年MM月dd日 HH:mm:ss
* @return
*/
public String getDateTimeString(Date date,String format )
{
if (format == null || format.length() <=0)
{
return null;
}
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timeString = sdf.format(date);
return timeString;
}
/**
* 格式化传入的时间戳,将时间戳转化为指定格式字符串
* @param timestamp
* @param format 时间格式,如:yyyy-MM-dd HH:mm:ss SSS 或 yyyy年MM月dd日 HH:mm:ss *
* @param timestampType 时间戳格式 0毫秒 1秒
* @return
*/
public String getTimeStampString(long timestamp,String format ,int timestampType)
{
if (format == null || format.length() <=0)
{
return null;
}
if (timestampType == 1)
{
//如果时间戳格式是秒,需要江时间戳变为毫秒
timestamp = timestamp * 1000L;
}
Date dateTime = new Date(timestamp);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timeString = sdf.format(dateTime);
return timeString;
}
}
唯美诗句:梨花初雨纳微凉,拂袖青颜是檀郎。