时间类和日期类——time

  1. LocalDate:表示没有时区的日期,例如2023-10-05。

    LocalDate date = LocalDate.of(2023, Month.OCTOBER, 5);
    
    

    LocalDate示例:

  2. import java.time.LocalDate;  
    import java.time.Month;  
      
    public class LocalDateExample {  
        public static void main(String[] args) {  
            // 获取当前日期  
            LocalDate today = LocalDate.now();  
            System.out.println("Today's date: " + today);  
      
            // 创建特定日期  
            LocalDate specificDate = LocalDate.of(2023, Month.OCTOBER, 5);  
            System.out.println("Specific date: " + specificDate);  
      
            // 获取年份、月份和日期  
            int year = specificDate.getYear();  
            Month month = specificDate.getMonth();  
            int day = specificDate.getDayOfMonth();  
            System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);  
        }  
    }

  3. LocalTime:表示没有时区的时间,例如14:30:45。

    LocalTime time = LocalTime.of(14, 30, 45);
  4. LocalTime示例:
  5. import java.time.LocalTime;  
      
    public class LocalTimeExample {  
        public static void main(String[] args) {  
            // 获取当前时间  
            LocalTime now = LocalTime.now();  
            System.out.println("Current time: " + now);  
      
            // 创建特定时间  
            LocalTime specificTime = LocalTime.of(14, 30, 45);  
            System.out.println("Specific time: " + specificTime);  
      
            // 获取小时和分钟  
            int hour = specificTime.getHour();  
            int minute = specificTime.getMinute();  
            System.out.println("Hour: " + hour + ", Minute: " + minute);  
        }  
    }

  6. LocalDateTime:表示没有时区的日期和时间,例如2023-10-05T14:30:45。

  7. 示例:

    import java.time.LocalDateTime;  
    import java.time.Month;  
      
    public class LocalDateTimeExample {  
        public static void main(String[] args) {  
            // 获取当前日期和时间  
            LocalDateTime now = LocalDateTime.now();  
            System.out.println("Current date and time: " + now);  
      
            // 创建特定日期和时间  
            LocalDateTime specificDateTime = LocalDateTime.of(2023, Month.OCTOBER, 5, 14, 30, 45);  
            System.out.println("Specific date and time: " + specificDateTime);  
        }  
    }

  8. 
    	LocalDateTime dateTime = LocalDateTime.of(2023, Month.OCTOBER, 5, 14, 30, 45);
  9. ZonedDateTime:表示带时区的日期和时间,例如2023-10-05T14:30:45+02:00[Europe/Berlin]。

  10. ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, Month.OCTOBER, 5, 14, 30, 45, 0, ZoneId.of("Europe/Berlin"));

    ZonedDateTime示例:

    import java.time.ZoneId;  
    import java.time.ZonedDateTime;  
    import java.time.Month;  
      
    public class ZonedDateTimeExample {  
        public static void main(String[] args) {  
            // 获取当前带时区的日期和时间  
            ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));  
            System.out.println("Current ZonedDateTime: " + now);  
      
            // 创建特定带时区的日期和时间  
            ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2023, Month.OCTOBER, 5, 14, 30, 45, 0, ZoneId.of("Europe/Berlin"));  
            System.out.println("Specific ZonedDateTime: " + specificZonedDateTime);  
        }  
    }

  11. Instant:表示一个时间戳,通常用于表示UTC时间。

    Instant instant = Instant.now();
  12. Duration:表示时间间隔,例如P2DT3H4M(2天3小时4分钟)。

    Duration duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
    //详细例子
    
    import java.time.Duration;  
      
    public class DurationExample {  
        public static void main(String[] args) {  
            // 创建Duration对象  
            Duration duration = Duration.ofHours(2).plusMinutes(30).plusSeconds(15);  
            System.out.println("Duration: " + duration);  
      
            // 获取小时、分钟和秒  
            long hours = duration.toHours();  
            long minutes = duration.toMinutesPart(); // 注意:这个方法不是标准的,应该使用duration.toMinutes() - hours * 60来计算剩余分钟数  
            long seconds = duration.getSeconds() % 60; // 获取秒数部分  
      
            // 由于toMinutesPart()不是标准方法,我们这样计算分钟数:  
            long totalMinutes = duration.toMinutes();  
            minutes = totalMinutes % 60;  
            hours = totalMinutes / 60;  
      
            System.out.println("Hours: " + hours + ", Minutes: " + minutes + ", Seconds: " + seconds);  
        }  
    }  
    // 注意:上面的DurationExample中,`toMinutesPart()`方法是不存在的,我只是为了说明如何计算剩余分钟数而提到了它。  
    // 正确的做法是使用`toMinutes()`和`toHours()`结合算术运算来计算。
  13. Period:表示日期间隔,例如P2Y3M(2年3个月)。

    Period period = Period.ofYears(2).plusMonths(3);
    
    //详细例子
    import java.time.Period;  
      
    public class PeriodExample {  
        public static void main(String[] args) {  
            // 创建Period对象  
            Period period = Period.ofYears(3).plusMonths(4);  
            System.out.println("Period: " + period);  
      
            // 获取年和月(注意:Period不包含天数部分的具体值,除非是通过两个日期计算得到的Period)  
            int years = period.getYears();  
            int months = period.getMonths();  
            System.out.println("Years: " + years + ", Months: " + months);  
      
            // 计算两个日期之间的Period  
            Period between = Period.between(LocalDate.of(2020, Month.JANUARY, 1), LocalDate.of(2023, Month.MAY, 15));  
            System.out.println("Period between two dates: " + between);  
        }  
    }
  14. Clock:提供访问当前瞬间、当前时区和当前时间的手段。

    Clock clock = Clock.systemUTC();
  15. TemporalAdjuster:用于调整日期和时间对象,例如将日期调整到下一个工作日。

    LocalDate nextWorkday = LocalDate.now().with(TemporalAdjusters.nextWorkingDay());
  16. DateTimeFormatter:用于格式化和解析日期和时间对象。

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 
    String formattedDate = dateTime.format(formatter); 
    LocalDateTime parsedDateTime = LocalDateTime.parse("2023-10-05 14:30:45", formatter);

 

在Java中,处理日期和时间的类主要位于java.util包和java.time包中。不过,从Java 8开始,推荐使用java.time包下的类来处理日期和时间,因为java.util.Datejava.util.Calendar等旧类存在设计上的缺陷,并且java.time包提供了更清晰、更强大的API。

以下是java.time包中一些常用的日期和时间类的完整包名以及举例说明:

  1. LocalDate - 表示没有时区的日期。

    • 完整包名:java.time.LocalDate
    • 举例:
      LocalDate today = LocalDate.now(); // 获取当前日期
      System.out.println(today); // 输出格式如:2023-10-05
  2. LocalTime - 表示没有时区的时间。

    • 完整包名:java.time.LocalTime
    • 举例:
      LocalTime now = LocalTime.now(); // 获取当前时间
      System.out.println(now); // 输出格式如:14:30:45
  3. LocalDateTime - 表示没有时区的日期和时间。

    • 完整包名:java.time.LocalDateTime
    • 举例:
      LocalDateTime now = LocalDateTime.now(); // 获取当前的日期和时间
      System.out.println(now); // 输出格式如:2023-10-05T14:30:45
  4. ZonedDateTime - 表示带时区的日期和时间。

    • 完整包名:java.time.ZonedDateTime
    • 举例:
      ZonedDateTime zonedDateTime = ZonedDateTime.now(); // 获取当前的带时区的日期和时间
      System.out.println(zonedDateTime); // 输出格式如:2023-10-05T14:30:45+08:00[Asia/Shanghai]
  5. Duration - 表示时间间隔,通常用于表示两个时间点之间的差异。

    • 完整包名:java.time.Duration
    • LocalTime start = LocalTime.of(10, 0);
      LocalTime end = LocalTime.of(12, 30);
      Duration duration = Duration.between(start, end);
      System.out.println(duration.toHours() + "小时 " + duration.toMinutesPart() + "分钟"); // 注意:toMinutesPart()不是真实方法,应使用适当的方法来获取分钟数
      // 正确的获取分钟数的方式可能是:long minutes = duration.toMinutes() % 60;

    注意:上面的toMinutesPart()方法是不存在的,这里只是为了说明如何表示时间间隔。正确的方式是使用toMinutes()来获取总分钟数,或者分别使用toHours()toMinutes()来获取小时和分钟,并自行处理进位问题。

  6. Period - 表示日期间隔,通常用于表示两个日期之间的差异(年、月、日)。

    • 完整包名:java.time.Period
    • 举例:
      LocalDate startDate = LocalDate.of(2023, 1, 1);
      LocalDate endDate = LocalDate.of(2023, 10, 5);
      Period period = Period.between(startDate, endDate);
      System.out.println(period.getYears() + "年 " + period.getMonths() + "月 " + period.getDays() + "日");

对于旧版的java.util包中的日期和时间类,如java.util.Datejava.util.Calendar,虽然它们仍然可以使用,但通常不推荐在新的代码中使用它们,因为它们的设计不如java.time包中的类那样直观和强大。如果你正在维护旧代码或需要与旧API兼容,那么你可能仍然需要了解和使用这些类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值