您的位置:首页 > 编程语言 > Java开发

Java如何把UNIX时间戳转换成日期格式-日期格式转化时间戳戳-当前时间戳

2018-10-25 09:04 573 查看
版权声明:博文原创+转载 https://blog.csdn.net/tangqing24680/article/details/83374149

开发中,经常需要把UNIX时间戳通过日期格式显示出来,如下可以输出日期格式

 

[code]package com.self.date;

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

public class DateTest{
public static final String GSTIME="yyyy-MM-dd HH:mm:ss";

//UNIX时间戳转换日期:方法一
//方法一,可能会报Long未定义错误;[未知,若有知道的,请留下言]
//当然可以转换成 str=unix_time.format(new Date(Long.parseLong(nowtime+"000")));
public static String getTimestampDate(String timestamp){
String str;
SimpleDateFormat unix_time=new SimpleDateFormat(GSTIME);
str=unix_time.format(new Date(Long.valueOf(timestamp+"000")));
//此处增加"000"是因为Date类中,传入的时间单位为毫秒。
return str;
}

//时间戳转换成日期格式:方法二
public static void getUnixTransferTime(){
System.out.println("转换的日期是:");
long nowtime=1541261100;//某个时间戳;
Date date=new Date(nowtime*1000);
SimpleDateFormat format=new SimpleDateFormat(GSTIME);
String nowDateString=format.format(date);
System.out.println(nowDateString);
}

//日期格式转换为UNIX时间戳
public static String getDateTimestamp(String timestamp) throws ParseException{
String str;
SimpleDateformat date_time=new SimpleDateFormat(GSTIME);
Date date=date_time.parse(timestamp);
long ts=date.getTime();
str=String.valueOf(ts/1000);
return str;
}

//获取当前时间时间戳
public static String timecurrentTime(){
long time=System.currentTimeMillis();
String str=String.valueOf(time/1000);
return str;
}

}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: