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

来源:互联网 发布:vscode 滑动卡顿 编辑:程序博客网 时间:2024/05/18 00:35

1.通过Util包中的Date获取

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

2.通过Util包的Calendar 获取

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

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

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