System.currentTimeMillis() 获取当前系统时间

本文介绍Java中如何使用SimpleDateFormat类进行日期时间的格式化,并演示了如何获取当前时间并进行毫秒级的时间差计算。通过具体代码示例展示了不同格式的日期时间输出,以及如何对比两个时间点之间的差距。

时间转换


1秒=1000毫秒(ms) 

1秒=1,000,000 微秒(μs) 

1秒=1,000,000,000 纳秒(ns) 

1秒=1,000,000,000,000 皮秒(ps)1纳秒=1000 皮秒 

1纳秒 =0.001  微秒

1纳秒=0.000001 毫秒

1纳秒=0.00000 0001秒

1分钟=60秒

1小时=60分钟=3600秒


类 SimpleDateFormat

  [object Object]
      [object Object]
          [object Object]

SimpleDateFormat 可以选择任何用户定义的日期-时间格式的模式

 import java.util.Date;
import java.text.SimpleDateFormat;
class DateTime {
 private SimpleDateFormat sdf = null;
 public String getDate() {
  this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  return this.sdf.format(new Date());
 }
 public String getDateComplete() {
  this.sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 SSS 毫秒");
  return this.sdf.format(new Date());
 }
 public String getTimeStamp() {
  this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  return this.sdf.format(new Date());
 }
}
public class demo {
 public static void main(String[] args) {
  DateTime dt = new DateTime();
  System.out.println("系统日期:" + dt.getDate());
  System.out.println("中文日期:" + dt.getDateComplete());
  System.out.println("时间戳:" + dt.getTimeStamp());
 }
}

 


 获取系统时间计算比较毫秒数

 public static void main(String[] args) {
  Date date=new Date();
  System.out.println(date);
  
  
//  String dateFormet=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//  System.out.println(dateFormet);
  
  long timemillis=System.currentTimeMillis();
  System.out.println(timemillis);
  
  long time=new Date().getTime();
  System.out.println(time);
  
  String linkTime="2015-06-22 21:18:00";
//  SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//  Date formatParse=null;
//  try {
//   formatParse=dateFormat.parse(linkTime);
//  } catch (ParseException e) {
//   e.printStackTrace();
//  }
//  long formatLong=formatParse.getTime();
  
  long formatLong=0l;
  try {
   formatLong=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(linkTime).getTime();
  } catch (ParseException e) {
   e.printStackTrace();
  }
  System.out.println(formatLong);
  System.out.println(time - formatLong);
  System.out.println("分:"+(time - formatLong)/(1000*60));
  System.out.println("秒:"+(time - formatLong)/1000);
  if(time-formatLong > 1000*60){
   System.out.println("系统时间大于2分 true");
  }else{
   System.out.println("系统时间不大于2分 false");
  }
  
  
 }

 

System.currentTimeMillis()返回的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。

 

转载于:https://my.oschina.net/saulc/blog/469541

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值