java打印时间的方法有哪些

16次阅读
没有评论

共计 645 个字符,预计需要花费 2 分钟才能阅读完成。

有多种方法可以在 Java 中打印时间,以下是一些常用的方法:

  1. 使用 java.util.Date 类:
Date date = new Date();
System.out.println(date);
  1. 使用 java.time 包中的 LocalDateTime 类:
LocalDateTime currentTime = LocalDateTime.now();
System.out.println(currentTime);
  1. 使用 java.text.SimpleDateFormat 类:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
System.out.println(currentTime);
  1. 使用 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);

这些方法可以根据需要打印出不同格式的时间信息。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-04-22发表,共计645字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)