java获取当前时间,时间戳,时间戳和时间相互转换

获取当前时间戳

//方法 一
long time1 = System.currentTimeMillis()
//方法 二
long time2 = Calendar.getInstance().getTimeInMillis();
//方法 三
long time3 = new Date().getTime();

获取当前时间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date = df.format(new Date());
System.out.println("当前时间:"+date);

时间戳转换为时间

//date是yyyy-MM-dd HH:mm:ss格式的String类型的时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time= simpleDateFormat.parse(date);
long timeStamp= time.getTime();
System.out.println(timeStamp);

时间转换为时间戳

//s是String类型的时间戳
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeStamp= new Long(s);
Date date = new Date(timeStamp);
String time= simpleDateFormat.format(date);
System.out.println(time);

 

你可能感兴趣的:(java获取当前时间,时间戳,时间戳和时间相互转换)