关于java:Java-8中处理日期时间的API和方法atZoneZoneIdofInstant类Datefrom

2次阅读

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

1.atZone() 是 LocalDateTime 类的一个办法,用于将日期工夫与指定的时区关联起来,返回一个 ZonedDateTime 对象。该办法承受一个 ZoneId 参数,用于指定时区。它将 LocalDateTime 对象与指定的时区进行关联,并返回一个在该时区下的 ZonedDateTime 对象。

以下是 atZone() 办法的示例用法:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class AtZoneExample {public static void main(String[] args) {
        // 获取以后日期和工夫
        LocalDateTime dateTime = LocalDateTime.now();

        // 指定时区为纽约
        ZoneId zoneId = ZoneId.of("America/New_York");

        // 将日期工夫与指定时区关联
        ZonedDateTime zonedDateTime = dateTime.atZone(zoneId);

        // 打印关联后的日期工夫
        System.out.println("ZonedDateTime:" + zonedDateTime);
    }

// 输入:2023-07-03T14:11:41.610-04:00[America/New_York]
}

在上述代码中,咱们首先获取以后的日期和工夫 LocalDateTime.now()。而后,应用 ZoneId.of() 办法创立一个 ZoneId 对象,指定时区为纽约(”America/New_York”)。接下来,应用 atZone() 办法将日期工夫与指定时区关联,返回一个在纽约时区下的 ZonedDateTime 对象。最初,打印关联后的日期工夫。

须要留神的是,atZone() 办法只能用于 LocalDateTime 对象,而不能用于 LocalDate 或 LocalTime 对象。如果须要将 LocalDate 或 LocalTime 与时区关联,能够先将其转换为 LocalDateTime 对象,而后再应用 atZone() 办法进行关联。


2.ZoneId.systemDefault() 是一个静态方法,用于获取零碎默认的时区。该办法会返回一个代表以后零碎默认时区的 ZoneId 对象。零碎默认时区是依据操作系统的设置来确定的,它反映了以后零碎所在的地理位置的时区设置。

以下是 ZoneId.systemDefault() 办法的示例用法:

import java.time.ZoneId;

public class SystemDefaultZoneExample {public static void main(String[] args) {
        // 获取零碎默认时区
        ZoneId defaultZone = ZoneId.systemDefault();

        // 打印零碎默认时区
        System.out.println("System Default Zone:" + defaultZone);
    }
}

在上述代码中,咱们应用 ZoneId.systemDefault() 办法获取零碎默认的时区。而后,通过打印输出,咱们能够查看零碎默认时区的标识符。

须要留神的是,零碎默认时区是基于操作系统的设置,因而在不同的操作系统上可能会有不同的默认时区。如果须要在应用程序中应用特定的时区,能够应用 ZoneId.of() 办法来指定所需的时区。例如,ZoneId.of(“Asia/Shanghai”) 示意应用亚洲 / 上海时区。


3.ZoneId.of() 是 Java 8 中 java.time 包中的一个静态方法,用于创立 ZoneId 对象,示意特定的时区。该办法承受一个字符串参数,用于指定时区的标识符。时区标识符通常采纳 ” 区域 / 城市 ” 的模式,例如:”Asia/Shanghai”、”America/New_York” 等。这些标识符遵循 IANA 时区数据库的命名规定。

以下是 ZoneId.of() 办法的示例用法:

import java.time.ZoneId;

public class ZoneIdExample {public static void main(String[] args) {
        // 创立时区对象
        ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
        ZoneId zoneId2 = ZoneId.of("America/New_York");

        // 打印时区标识符
        System.out.println("ZoneId 1:" + zoneId1);
        System.out.println("ZoneId 2:" + zoneId2);
    }
}

在上述代码中,咱们应用 ZoneId.of() 办法创立了两个不同的时区对象,别离代表 ”Asia/Shanghai” 和 ”America/New_York” 时区。而后,通过打印输出,咱们能够查看这些时区对象的标识符。

须要留神的是,ZoneId.of() 办法在创立 ZoneId 对象时会进行时区标识符的验证,如果指定的时区标识符不存在或不可辨认,将会抛出 ZoneRulesException 异样。

总结来说,ZoneId.of() 办法是 Java 8 中 java.time 包中用于创立 ZoneId 对象的一个静态方法,用于示意特定的时区。通过指定时区标识符,咱们能够创立相应的 ZoneId 对象来解决时区相干的操作。


4.Instant 类,用于示意时间轴上的一个特定时刻,Instant 提供了一些重要的性能。

以下是一个示例代码,演示了 Instant 的应用:

import java.time.Instant;

// 创立以后时刻的 Instant 对象
Instant instant1 = Instant.now();
System.out.println("以后工夫的 Instant 对象:" + instant1);

// 依据工夫戳创立 Instant 对象
Instant instant2 = Instant.ofEpochMilli(1609459200000L);
System.out.println("工夫戳对应的 Instant 对象:" + instant2);

// 获取工夫戳
long timestamp = instant1.toEpochMilli();
System.out.println("以后工夫的工夫戳:" + timestamp);

// 比拟两个 Instant 对象
boolean isBefore = instant1.isBefore(instant2);
boolean isAfter = instant1.isAfter(instant2);
System.out.println("是否在 instant2 之前:" + isBefore);
System.out.println("是否在 instant2 之后:" + isAfter);

// 进行工夫运算
Instant instant3 = instant1.plusSeconds(60);
System.out.println("加 60 秒后的 Instant 对象:" + instant3);

// 转换为 ZonedDateTime 对象
ZonedDateTime zonedDateTime = instant1.atZone(ZoneId.systemDefault());
System.out.println("关联到零碎默认时区的 ZonedDateTime 对象:" + zonedDateTime);

// 格式化输入
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedInstant = formatter.format(instant1);
System.out.println("格式化后的 Instant:" + formattedInstant);

这是 Instant 类的一些重要性能和用法。它是 Java 8 日期工夫 API 中的外围类之一,提供了解决时间轴上的时刻的能力。


5.Date.from() 办法是将 Instant 对象转换为 Date 对象的静态方法。

以下是一个示例代码:

import java.time.Instant;
import java.util.Date;

// 获取以后工夫的 Instant 对象
Instant instant = Instant.now();

// 将 Instant 转换为 Date
Date date = Date.from(instant);

// 能够对 date 进行进一步操作或者输入
System.out.println(date);

请留神,Date 对象和 Instant 对象都示意一个特定的时刻,但它们有不同的 API 和性能。java.util.Date 是可变的,而 Instant 是不可变的。在我的项目中,倡议优先应用 java.time 包中的日期工夫类型,如 Instant、LocalDateTime 等。

正文完
 0