JAVA 获取当前时间的方法
public static void main(String[] args) { var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //方法一: var now = new Date(); System.out.println(sdf.format(now)); //方法二: var now2 = System.currentTimeMillis(); System.out.println(sdf.format(now)); //方法三: Calendar calendar = Calendar.getInstance(); var year = calendar.get(Calendar.YEAR); var month = calendar.get(Calendar.MONTH); var Date = calendar.get(Calendar.DATE); var hour = calendar.get(Calendar.HOUR); var minute = calendar.get(Calendar.MINUTE); var second = calendar.get(Calendar.SECOND); System.out.println(year+"-"+month+"-"+Date+" "+hour+":"+minute+":"+second); }
输出效果: