java换算10位时间戳_13位10位时间戳转换日期

工具类:时间转换

一、10位13位时间戳转String 格式(2018-10-15 16:03:27) 日期

/**

* 10位13位时间戳转String 格式(2018-10-15 16:03:27) 日期

* @param timestamp

* @param simpleDateFormatType 时间戳类型("yyyy-MM-dd HH:mm:ss")

* @return

*/

public static String numberDateFormat(String timestamp,String simpleDateFormatType){

SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatType);//要转换的时间格式

String date = null;

if (timestamp.length() == 13){

date = sdf.format(Long.parseLong(timestamp));

}else{

date = sdf.format(Long.parseLong(timestamp)*1000);

}

return date;

}

二、10位13位时间戳转Date

/**

* 10位13位时间戳转Date

* @param timestamp 参数时间戳

* @param simpleDateFormatType 时间戳类型("yyyy-MM-dd HH:mm:ss")

* @return

*/

public static Date numberDateFormatToDate(String timestamp,String simpleDateFormatType){

SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatType);//要转换的时间格式

Date date = null;

try {

if (timestamp.length() == 13){

date = sdf.parse(sdf.format(Long.parseLong(timestamp)));

}else{

date = sdf.parse(sdf.format(Long.parseLong(timestamp)*1000));

}

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}

三、 Date转10位13位时间戳

/**

* Date转10位13位时间戳

* @param date 参数date

* @param n 需要转换成几位时间戳

* @return

*/

public static String numberDateFormatToDate(Date date,int n){

String result = null;

if (n == 13){

result = String.valueOf(date.getTime());

}else {

result = String.valueOf(date.getTime()/1000);

}

return result;

}

工具类:时间转换 一、10位13位时间戳转String 格式(2018-10-15 16:03:27) 日期 /** * 10位13位时间戳转String 格式(2018-10-15 16:03:27) 日期 * @param timestamp * @param simpleDateFormatType 时间戳类型("yyyy-MM-dd HH:mm:ss") * @return */ public static String numberDateFormat(String timestamp,String simpleDateFormatType){ SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatType);//要转换的时间格式 String date = null; if (timestamp.length() == 13){ date = sdf.format(Long.parseLong(timestamp)); }else{ date = sdf.format(Long.parseLong(timestamp)*1000); } return date; } 二、10位13位时间戳转Date /** * 10位13位时间戳转Date * @param timestamp 参数时间戳 * @param simpleDateFormatType 时间戳类型("yyyy-MM-dd HH:mm:ss") * @return */ public static Date numberDateFormatToDate(String timestamp,String simpleDateFormatType){ SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatType);//要转换的时间格式 Date date = null; try { if (timestamp.length() == 13){ date = sdf.parse(sdf.format(Long.parseLong(timestamp))); }else{ date = sdf.parse(sdf.format(Long.parseLong(timestamp)*1000)); } } catch (ParseException e) { e.printStackTrace(); } return date; } 三、 Date转10位13位时间戳 /** * Date转10位13位时间戳 * @param date 参数date * @param n 需要转换成几位时间戳 * @return */ public static String numberDateFormatToDate(Date date,int n){ String result = null; if (n == 13){ result = String.valueOf(date.getTime()); }else { result = String.valueOf(date.getTime()/1000); } return result; }
经验分享 程序员 微信小程序 职场和发展