有多种方法可以在Java中打印时间,以下是一些常用的方法:
使用java.util.Date类:Date date = new Date();System.out.println(date);使用java.time包中的LocalDateTime类:LocalDateTime currentTime = LocalDateTime.now();System.out.println(currentTime);使用java.text.SimpleDateFormat类:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String currentTime = sdf.format(new Date());System.out.println(currentTime);使用java.time.format.DateTimeFormatter类:LocalDateTime currentTime = LocalDateTime.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String formattedTime = currentTime.format(formatter);System.out.println(formattedTime);这些方法可以根据需要打印出不同格式的时间信息。




