我在工作中写好的一个日期转换工具类:
public class DateUtil {
private static Logger logger = Logger.getLogger(DateUtil.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
/**
* 从日期的 8 位字符串表示转换为 Unix 时间戳
* @param dateStr 日期的8位字符串表示,例如 20150630
* @return
*/
public static Long tranDateStr2Unix(String dateStr){
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
logger.error("输入的日期格式错误");
e.printStackTrace();
}
return date.getTime()/1000;
}
public static String tranUnix2dateStr(Long unixTimestamp){
Date date = new Date(unixTimestamp*1000);
return sdf.format(date);
}
}
参考资料:
Unix时间戳(Unix timestamp)转换工具
http://tool.chinaz.com/Tools/unixtime.aspx
Java将Unix时间戳转换成指定格式日期 - God’s blog - 博客频道 - youkuaiyun.com
http://blog.youkuaiyun.com/a600423444/article/details/6365539