Demo01_LocalDateTimeTest.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package J20250808.localDateTime;
  2. import java.time.LocalDateTime;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title Demo01_LocalDateTime
  7. * @description
  8. * @create 2025/8/8
  9. */
  10. public class Demo01_LocalDateTimeTest {
  11. public static void main(String[] args) {
  12. //获取当前系统的时间
  13. LocalDateTime now = LocalDateTime.now();
  14. System.out.println(now);
  15. //使用指定年月日时分秒初始化一个LocalDateTime对象
  16. LocalDateTime of = LocalDateTime.of(2000, 2, 22, 22, 22, 22);
  17. System.out.println(of);
  18. //获取年 int getYear()
  19. System.out.println("今年是:"+now.getYear());
  20. //获取月份1-12 int getMonthValue()
  21. System.out.println("这个月是:"+now.getMonthValue());
  22. //获取月份中的第几天 1~31 int getDayOfMonth()
  23. System.out.printf("今天是本月中的第%d天\n",now.getDayOfMonth());
  24. //获取一年中的第几天 1~366 int getDayOfYear()
  25. System.out.printf("今天是今年中的第%d天\n",now.getDayOfYear());
  26. //获取星期 DayOfWeek getDayOfWeek()
  27. System.out.println("今天是"+now.getDayOfWeek());
  28. //获取小时 getHour()
  29. System.out.println("小时:"+now.getHour());
  30. //获取分钟 getMinute()
  31. System.out.println("分钟:"+now.getMinute());
  32. //获取秒 getSecond()
  33. System.out.println("秒:"+now.getSecond());
  34. }
  35. }