开发中,经常需要把UNIX时间戳通过日期格式显示出来,如下可以输出日期格式
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;}}
如果觉得《Java如何把UNIX时间戳转换成日期格式-日期格式转化时间戳戳-当前时间戳》对你有帮助,请点赞、收藏,并留下你的观点哦!