sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

java 获取当前时间的三种方法
https://blog.csdn.net/c851204293/article/details/91801973

1.通过Util包中的Date获取

  1. Date date = new Date();
  2. SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
  3. System.out.println(dateFormat.format(date));


2.通过Util包的Calendar 获取

  1. Calendar calendar= Calendar.getInstance();
  2. SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
  3. System.out.println(dateFormat.format(calendar.getTime()));


3.通过Util包的Calendar 获取时间,分别获取年月日时分秒

  1. Calendar cal=Calendar.getInstance();      
  2. int y=cal.get(Calendar.YEAR);      
  3. int m=cal.get(Calendar.MONTH);      
  4. int d=cal.get(Calendar.DATE);      
  5. int h=cal.get(Calendar.HOUR_OF_DAY);      
  6. int mi=cal.get(Calendar.MINUTE);      
  7. int s=cal.get(Calendar.SECOND);      
  8. System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");

 


--------------------- 
参考:https://blog.csdn.net/qq_34682683/article/details/78716547 

posted on 2022-03-20 19:55  sunny123456  阅读(12402)  评论(0编辑  收藏  举报