java获取当前时间的方法有哪些?很多人都不太了解,今天小编为了让大家更加了解java获取当前时间的方法,所以给大家总结了以下内容,一起往下看吧。

1、通过Util包中的Date获取

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

(视频教程推荐:java视频教程)

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+"秒");

以上就是java获取当前时间的方法有哪些的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注亿速云行业资讯频道哦!