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入门
以上就是java中获取当前时间的三种方法是什么的详细内容,更多请关注大盘站其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。