时间日期Date()

java.util 包提供了 Date 类来封装当前的日期和时间

Date date = new Date();
date.toString();                    //Mon May 03 09:51:52 CDT 2020

使用 SimpleDateFormat 格式化日期

Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间为: " + ft.format(dNow));                     //当前时间为: 2020-03-12 10:16:34

测量时间

import java.util.*;
  
public class DiffDemo {
 
   public static void main(String args[]) {
      try {
         long start = System.currentTimeMillis( );
         System.out.println(new Date( ) + "\n");
         Thread.sleep(5*60*10);                    //休眠3秒
         System.out.println(new Date( ) + "\n");
         long end = System.currentTimeMillis( );
         long diff = end - start;
         System.out.println("Difference is : " + diff);
      } catch (Exception e) {
         System.out.println("Got an exception!");
      }
   }
}

运行结果

Fri Jan 08 09:48:47 CST 2020 Fri Jan 08 09:48:50 CST 2020 Difference is : 3019

猜你喜欢

转载自www.cnblogs.com/leilei-y/p/12468485.html