package com.stylefeng.guns.core.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 时间转换工具
* @Author: dingqi
* @Date: 2018/7/26
*/
public class TimeUtil {
/**
* 时间戳转换显示(yyyy-MM-dd HH:mm:ss)
* @param timestamp
* @return
*/
public static String toDate(String timestamp){
StringBuffer sbf=new StringBuffer();
sbf.append(timestamp);
if(timestamp.length() == 10){
sbf.append("000");
}
long time = Long.parseLong(sbf.toString()); //int放不下的,用long
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(time);
}
/**
* 时间戳转换显示(yyyy-MM-dd HH:mm:ss)
* @param timestamp
* @return
*/
public static String toDate2(String timestamp){
StringBuffer sbf=new StringBuffer();
sbf.append(timestamp);
if(timestamp.length() == 10){
sbf.append("000");
}
long time = Long.parseLong(sbf.toString()); //int放不下的,用long
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
return sdf.format(time);
}
/**
* 时间戳转换显示(yyyy-MM-dd)
* @param timestamp
* @return
*/
public static String toDate1(String timestamp){
StringBuffer sbf=new StringBuffer();
sbf.append(timestamp);
if(timestamp.length() == 10){
sbf.append("000");
}
long time = Long.parseLong(sbf.toString()); //int放不下的,用long
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(time);
}
/**
* 时间显示转10位时间戳(yyyy-MM-dd HH:mm:ss)
* @return
*/
public static String toTimestamp10(String date) throws ParseException {
return String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime()).substring(0,10);
}
public static String toTimestamp11(String date) throws ParseException {
return String.valueOf(new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime()).substring(0,10);
}
/**
* 时间转换string(yyyy-MM-dd)
* @return
*/
public static String DatetoTime(Date date) {
try {
return new SimpleDateFormat("yyyy-MM-dd").format(date);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}