首页 > 技术文章 > 时间戳转化

tobiasy 2018-01-10 14:21 原文

package com.xfm.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* 时间戳转化
*
* @author xfm
*/
public class TimeStampsTransform {
/**
* 根据时间戳创建Date对象
*
* @param timestamps
* @return
*/
public static Date getDate(Long timestamps) {
return new Date(timestamps);
}

/**
* 格式化时间戳
*
* @param timestamps
* @return
*/
public static String format(Long timestamps) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
Date d = new Date(timestamps);
return sdf.format(d);
}

/**
* 自定义格式化时间戳
*
* @param timestamps
* @param pattern
* @return
*/
public static String format(Long timestamps, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date d = new Date(timestamps);
return sdf.format(d);
}

public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println(format(date.getTime()));
String string = format(date.getTime(), "yyyy-MM-dd HH:mm:ss");
System.out.println(string);
System.out.println(getDate(date.getTime()));

}
}

推荐阅读