1. Obtain
Date date = new Date(); SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss"); System.out.println(dateFormat.format(date));
through Date in the Util package (video tutorial recommendation: java video tutorial)
2 , Get the time through the Calendar of the Util package
Calendar calendar= Calendar.getInstance(); SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss"); System.out.println(dateFormat.format(calendar.getTime()));
3. Get the time through the Calendar of the Util package, and get the year, month, day, hour, minute and second respectively
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+"秒");
Recommended related tutorials: Getting started with java
The above is the detailed content of What are the three ways to get the current time in java. For more information, please follow other related articles on the PHP Chinese website!