java 获取当前时间的三种方法


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. get Time()));


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 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM