Mục lục
- 1 Khởi tạo một Instant
- 2 Các method trong instant class
- 2.1 now()
- 2.2 now(Clock clock)
- 2.3 with(TemporalAdjuster adjuster)
- 2.4 with(TemporalField field, long newValue)
- 2.5 until(Temporal endExclusive, TemporalUnit unit)
- 2.6 ofEpochSecond(long epochSecond)
- 2.7 ofEpochSecond(long epochSecond, long nanoAdjustment)
- 2.8 plus(long amountToAdd, TemporalUnit unit)
- 2.9 plus(TemporalAmountT amountToAdd)
- 2.10 minus(long amountToSubtract, TemporalUnit unit)
- 2.11 minus(TemporalAmount amountTosubtract)
- 2.12 truncatedTo(TemporalUnit unit)
- 2.13 parse(CharSequence text)
- 2.14 ofEpochMilli(long epochMilli)
- 2.15 plusSeconds(long secondsToAdd)
- 2.16 minusSeconds(long secondsToSubtract)
- 2.17 toString()
- 2.18 rang(TemporalField field)
- 2.19 plusMillis(long millis)
- 2.20 minusMillis(long millisToSubtract)
- 2.21 plusNanos(long nanoToAdd)
- 2.22 minusNanos(long nanosToSubtract)
- 2.23 get(TemporalField field)
- 2.24 from(TemporalAccessor temporal)
- 2.25 isBefore(Instant otherInstant)
- 2.26 isAfter(Instant otherInstant)
- 2.27 equals(Instant other)
- 2.28 compareTo(Instant other)
- 2.29 toEpochMilli()
- 2.30 getNano()
- 2.31 getEpochSecond()
Instant class thuộc java.time package đại diện cho một thời điểm cụ thể trên dòng thời gian. Giá trị của Instant được đếm từ thời điểm mốc 1970/01/01 (1970–01-01T00:00:00Z) hay còn gọi là EPOCH.
Instant thường được dùng để ghi vết lại một sự kiện trong ứng dụng.
Khởi tạo một Instant
Để khởi tạo một Instant instance đại diện cho thời điện hiện tại chúng ta gọi Instant.now().
Instant now = Instant.now();
Các method trong instant class
now()
now() method trả về Instant tại thời điểm hiện tại từ UTC clock.
// Syntax
public static Instant now()
// Example
// create an Instant object
Instant lt = Instant.now();
// print result
System.out.println("Instant : " + lt); // Instant : 2019-12-31T12:14:48.109Z
now(Clock clock)
Trả về Instant ở thời điểm hiện tại của đồng hồ được chỉ định trong tham số truyền vào.
// Syntax
public static Instant now(Clock clock)
//Example
// create a clock
Clock cl = Clock.systemDefaultZone();
// create an Instant object using now(Clock)
Instant lt = Instant.now(cl);
// print result
System.out.println("Instant : " + lt); // Instant : 2019-12-31T12:31:30.281Z
with(TemporalAdjuster adjuster)
Trả về một bản sao của một Instant với giá trị adjuster mới.
// Syntax
default Temporal with(TemporalAdjuster adjuster)
// Example
Instant local = Instant.parse("2019-12-31T10:33:50.63Z");
System.out.println("Instant before" + " adjustment: " + local); // Instant before adjustment: 2019-12-31T10:33:50.630Z
Instant updatedlocal = local.with(Instant.EPOCH);
System.out.println("Instant after" + " adjustment: " + updatedlocal); // Instant after adjustment: 1970-01-01T00:00:00Z
with(TemporalField field, long newValue)
Method này trả về một bản sao của Instant với giá trị của từng field được cập nhập với newValue. Chúng ta có thể thay đôi gía trị của ngày, tháng năm, giờ, phút, giây etc của Instant ban đầu.
// Syntax
public Instant with(TemporalField field, long newValue)
// Example
public static void main(String[] args) {
Instant instant = Instant.parse("2019-12-31T10:15:30.00Z");
System.out.println("Origin: " + instant); // Origin: 2019-12-31T10:15:30Z
System.out.println(instant.with(ChronoField.NANO_OF_SECOND, 20)); // 2019-12-31T10:15:30.000000020Z
until(Temporal endExclusive, TemporalUnit unit)
until method được sử dụng để tính toán khoản thời gian giữa 2 Instant theo đơn vị unit như giây, giờ, phút etc.
// Syntax
public long until(Temporal endExclusive, TemporalUnit unit)
// Example
// create Instant objects
Instant instant1 = Instant.parse("2019-01-03T19:35:50.00Z");
Instant instant2 = Instant.parse("2020-01-04T13:18:59.00Z");
// apply until method of Instant class
long result = instant1.until(instant2, ChronoUnit.MINUTES);
// print results
System.out.println("Result in Minutes: " + result); // 526663
ofEpochSecond(long epochSecond)
Khởi tạo một Instant từ số giây epochSecond được truyền vào làm tham số. Lấy mốc thời gian là 1970-01-01T00:00:00Z.
// Syntax
public static Instant ofEpochSecond(long epochSecond)
// Example
// 70 second = 1p10s
Instant instant = Instant.ofEpochSecond(70);
System.out.println("Intant: " + instant); // Intant: 1970-01-01T00:01:10Z
ofEpochSecond(long epochSecond, long nanoAdjustment)
Khởi tạo một Instant từ số giây epochSecond và nanoAdjustment mili giây được truyền vào làm tham số. Lấy mốc thời gian là 1970-01-01T00:00:00Z.
// Syntax
public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment)
// Example
Instant instant = Instant.ofEpochSecond(70, 99999999);
System.out.println("Intant: " + instant); // Intant: 1970-01-01T00:01:10.099999999Z
plus(long amountToAdd, TemporalUnit unit)
Trả về một bản sao chép của Instant với thời gian được cộng thêm một khoảng amountToAdd (đơn vị unit: second, days, etc).
// Syntax
public Instant plus(long amountToAdd, TemporalUnit unit)
//Example
Instant instant = Instant.parse("2019-12-30T19:34:50.63Z");
Instant value = instant.plus(30, ChronoUnit.DAYS);
System.out.println("Instant after adding 30 DAYS: " + value); // Instant after adding 30 DAYS: 2020-01-29T19:34:50.630Z
plus(TemporalAmountT amountToAdd)
Trả về một bản sao chép của Instant với thời gian được cộng thêm một khoảng date-time amountToAdd. amountToAdd thông thường là Period hoặc Duration hoặc bất cứ class nào implement TemporalAmount interface.
// syntax
public Instant plus(TemporalAmount amountToAdd)
// Example
// create a Instant object
Instant inst = Instant.parse("2020-12-30T19:34:50.63Z");
// add 20 Days to Instant
Instant value = inst.plus(Period.ofDays(20));
// print result
System.out.println("Instant after adding Days: " + value); // Instant after adding Days: 2021-01-19T19:34:50.630Z
minus(long amountToSubtract, TemporalUnit unit)
Trả về một bản sao chép của Instant với thời gian được trừ đi một khoảng amountToAdd (đơn vị unit: second, days, etc).
// Syntax
public Instant minus(long amountToSubtract, TemporalUnit unit)
// Example
Instant instant = Instant.now();
Instant value = instant.minus(20, ChronoUnit.DAYS);
System.out.println("Instant after subtracting DAYS: " + value); // Instant after subtracting DAYS: 2019-12-12T08:48:55.135Z
minus(TemporalAmount amountTosubtract)
Trả về một bản sao chép của Instant với thời gian được trừ đi một khoảng date-time amountToAdd. amountToAdd thông thường là Period hoặc Duration hoặc bất cứ class nào implement TemporalAmount interface.
// Syntax
public Instant minus(TemporalAmount amountTosubtract)
// Example
Instant instant = Instant.now();
Instant value = instant.minus(Period.ofDays(20));
System.out.println("Instant after subtracting DAYS: " + value); // Instant after subtracting DAYS: 2019-12-12T08:51:59.030Z
truncatedTo(TemporalUnit unit)
Lượt bỏ các phần giá trị đến đơn vị unit, Ví dụ nếu Instant của bạn đang là 2020-01-01T09:07:02.819Z, truncatedTo ChronoUnit.HOURS thì kết quả là 2020-01-01T09:00:00Z
//Syntax public Instant truncatedTo(TemporalUnit unit) // Example System.out.println(Instant.now().truncatedTo(ChronoUnit.HOURS)); // 2020-01-01T09:00:00Z
parse(CharSequence text)
Trả về một Instant từ chuỗi text truyền vào.
// Syntax
public static Instant parse(final CharSequence text)
// Example
Instant instant = Instant.parse("2019-12-12T08:51:59.030Z");
System.out.println("Instant: " + instant); // 2019-12-12T08:51:59.030Z
ofEpochMilli(long epochMilli)
ofEpochMili() trả về một instance Instant có gía trị bằng với số milisecond được truyền vào.
// Syntax
public static Instant ofEpochMilli(long epochMilli)
// Example
long milliseconds = 999999999;
Instant instant = Instant.ofEpochMilli(milliseconds);
System.out.println("Instant: " + instant); // Instant: 1970-01-12T13:46:39.999Z
plusSeconds(long secondsToAdd)
plusSeconds() trả về một instance Instant với giá trị được cộng thêm số giây secondToAdd được truyền vào.
// Syntax
public Instant plusSeconds(long secondsToAdd)
// Example
// addition of 84000 seconds to this instant
Instant instant = Instant.now().plusSeconds(84000);
System.out.println("Returned Instant: " + instant); // Returned Instant: 2020-01-04T14:47:02.883Z
minusSeconds(long secondsToSubtract)
minusSeconds() trả về một instance Instant với giá trị bị trừ đi số giây secondToSubtract được truyền vào.
// Syntax
public Instant minusSeconds(long secondsToSubtract)
// Example
Instant instant = Instant.now().minusSeconds(84000);
System.out.println("Returned Instant: " + instant); // Returned Instant: 2020-01-03T05:18:07.952Z
toString()
Trả về chuỗi đại diện cho một Instant định dạng ISO-8601.
// Syntax public String toString() // Example Instant instant = Instant.now(); System.out.println(instant.toString()); // 2020-01-03T15:30:53.562Z
rang(TemporalField field)
Trả về ValueRang chứa giá trị lớn nhất và nhỏ nhất của field truyền vào. Method này hữu ích cho chúng ta biết được chính xác khoảng giới hạn nằm ở đâu để tránh truyền dữ liệu không đúng.
// Syntax
public ValueRange range(TemporalField field)
// Example
// get range of MILLI_OF_SECOND field
ValueRange range = Instant.now().range(ChronoField.MILLI_OF_SECOND);
// print range of MILLI_OF_SECOND
System.out.println("Range of MILLI_OF_SECOND: " + range);
plusMillis(long millis)
Trả về một bản sao chép của Instant với thời gian được cộng thêm một khoảng milli giây từ tham số đầu vào.
// Syntax
public Instant plusMillis(long millisToAdd)
// Example
Instant instant = Instant.parse("2020-01-03T16:24:02.299Z");
System.out.println(instant.plusMillis(8800000)); //2020-01-03T18:50:42.299Z
minusMillis(long millisToSubtract)
Trả về một bản sao chép của Instant với thời gian bị trừ đi một khoảng milli giây từ tham số đầu vào.
// Syntax
public Instant minusMillis(long millisToSubtract)
// Example
Instant instant = Instant.parse("2020-01-03T16:24:02.299Z");
System.out.println(instant.minusMillis(8800000)); //2020-01-03T13:57:22.299Z
plusNanos(long nanoToAdd)
Trả về một bản sao chép của Instant với thời gian được cộng thêm một khoảng nano giây từ tham số đầu vào.
// Syntax
public Instant plusNanos(long nanosToAdd)
// Example
Instant instant = Instant.parse("2020-01-03T16:24:02.299Z");
System.out.println(instant.plusNanos(8800000)); //2020-01-03T16:24:02.307800Z
minusNanos(long nanosToSubtract)
Trả về một bản sao chép của Instant với thời gian bị trừ đi một khoảng nano giây từ tham số đầu vào.
Syntax
public Instant minusNanos(long nanosToSubtract)
// Example
Instant instant = Instant.parse("2020-01-03T16:24:02.299Z");
System.out.println(instant.minusNanos(8800000)); //2020-01-03T16:24:02.290200Z
get(TemporalField field)
Trả về giá trị tại field được chỉ định trong tham số truyền vào.
// Syntax
public int get(TemporalField field)
// Example
Instant instant = Instant.parse("2020-01-03T16:24:02.299Z");
System.out.println(instant.get(ChronoField.MILLI_OF_SECOND)); // 299
from(TemporalAccessor temporal)
Khởi tạo một Instant từ temporal.
// Syntax
public static Instant from(TemporalAccessor temporal)
// Example
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("ZonedDateTime: " + zonedDateTime); // ZonedDateTime: 2020-01-04T00:00:51.261+07:00[Asia/Ho_Chi_Minh]
Instant result = Instant.from(zonedDateTime);
System.out.println("Instant: " + result); // Instant: 2020-01-03T17:00:51.261Z
isBefore(Instant otherInstant)
isBefore() kiểm tra khoảng thời gian của Instant hiện tại có nằm trước khoảng thời gian của Instant otherInstant từ tham số đầu vào.
// Syntax public boolean isBefore(Instant otherInstant) //Example Instant instant1 = Instant.now(); Instant instant2 = Instant.now().plusMillis(1000); Instant instant3 = Instant.now().minusMillis(1000); System.out.println(instant1.isBefore(instant2)); // true System.out.println(instant1.isBefore(instant3)); // false System.out.println(instant1.isBefore(instant1)); // false
isAfter(Instant otherInstant)
isBefore() kiểm tra khoảng thời gian của Instant hiện tại có nằm sau khoảng thời gian của Instant otherInstant từ tham số đầu vào.
// Syntax public boolean isAfter(Instant otherInstant) //Example Instant instant1 = Instant.now(); Instant instant2 = Instant.now().plusMillis(1000); Instant instant3 = Instant.now().minusMillis(1000); System.out.println(instant1.isAfter(instant2)); // false System.out.println(instant1.isAfter(instant3)); // true System.out.println(instant1.isAfter(instant1)); // false
equals(Instant other)
Trả về true nếu Instant hiện tại bằng với Instant other từ tham số truyền vào.
// Syntax
public boolean equals(Object otherInstant)
// Example
Instant instant1 = Instant.parse("2020-01-04T05:10:57.392Z");
Instant instant2 = Instant.parse("2020-01-04T05:10:50.392Z");
Instant instant3 = Instant.parse("2020-01-04T05:10:57.392Z");
System.out.println(instant1.equals(instant2)); // false
System.out.println(instant1.equals(instant3)); // true
compareTo(Instant other)
So sánh 2 Instant với nhau. Trả về giá trị âm nếu Instant hiện tại bé hơn Instant other, 0 nếu bằng nhau và dương nếu lớn hơn
// Syntax
public int compareTo(Instant otherInstant)
// Example
Instant instant1 = Instant.parse("2020-01-04T05:10:57.392Z");
Instant instant2 = Instant.parse("2020-01-04T05:10:50.392Z");
Instant instant3 = Instant.parse("2020-01-04T05:10:57.392Z");
System.out.println(instant1.compareTo(instant2)); // 1
System.out.println(instant1.compareTo(instant3)); // 0
System.out.println(instant2.compareTo(instant3)); // -1
toEpochMilli()
Convert Instant sang milli giây.
//Syntax public long toEpochMilli() // Example Instant instant = Instant.now(); System.out.println(instant.toEpochMilli()); // 1578110000124
getNano()
Trả về phần giá trị nano của Instant
// Syntax
public int getNano()
//Example
// create a Instant object
Instant instant = Instant.parse("2018-12-30T19:34:50.63Z");
int value = instant.getNano();
System.out.println("nanoseconds value: "+ value); // nanoseconds value: 630000000
getEpochSecond()
Tính tổng số giây từ Instant mặc định “1970-01-01T00:00:00Z” đến Instant hiện tại.
// Syntax
public long getEpochSecond()
// Example
Instant instant = Instant.parse("2020-01-04T05:10:57.392Z");
System.out.println(instant.getEpochSecond()); // 1578114657