📜  Java的.time.OffsetDateTime类在Java中

📅  最后修改于: 2022-05-13 01:55:43.818000             🧑  作者: Mango

Java的.time.OffsetDateTime类在Java中

Java是最流行的编程语言和广泛使用的编程语言。 Java用于各种应用程序,如移动应用程序、桌面应用程序、Web 应用程序。在此Java Java.time.OffsetDate 中,导入了 Time 类,它是具有偏移量的日期时间的不可变表示。此类存储所有日期和时间字段,精度为纳秒,以及与 UTC 的偏移量。例如,值“22nd February 2021 at 01:55.19.123456789 +02:00”可以存储在 OffsetDateTime 中。

Methods

Description

adjustInto(Temporal temporal)This method adjusts the specified temporal object to have the same offset, date and time as this object.
atZoneSameInstant(ZoneId zone)This method combines this date-time with a time-zone to create a ZonedDateTime ensuring that the result has the same instant.
atZoneSimilarLocal(ZoneId zone)This method combines this date-time with a time-zone to create a ZonedDateTime trying to keep the same local date and time.
 compareTo(OffsetDateTime other)This method compares this date-time to another date-time.
equals(Object obj)It is used to checks if this date-time is equal to another date-time.
getMonth()It is a method that is used to display the current month.
now()It is a method used to obtain the current date and time from the system clock.
getYear()It is a method that is used to display the current Year.
getDayOfWeek()It is a method that is used to display the current day of the week. 
getHour()It is a method that is used to display a specific hour.
getMinute()It is a method that is used to display a specific minute.
getNano()It is a method that displays a specific nanosecond.
getSecond()It is a method that displays a specific second.
 equals(Object obj)This method checks if this date-time is equal to another date-time.
format(DateTimeFormatter formatter)This method formats this date-time using the specified formatter.
from(TemporalAccessor temporal)This method obtains an instance of OffsetDateTime from a temporal object.
get(TemporalField field)This method gets the value of the specified field from this date-time as an int.
 hashCode()A hash code for this date-time.
isAfter(OffsetDateTime other)This method checks if the instant of this date-time is after that of the specified date-time.
isBefore(OffsetDateTime other)This method checks if the instant of this date-time is before that of the specified date-time.
 isEqual(OffsetDateTime other)This method checks if the instant of this date-time is equal to that of the specified date-time.
 isSupported(TemporalField field)This method checks if the specified field is supported.
isSupported(TemporalUnit unit)This method checks if the specified unit is supported.
 withMinute(int minute)This method returns a copy of this OffsetDateTime with the minute-of-hour altered.
 withYear(int year)This method returns a copy of this OffsetDateTime with the year altered.
minusDays(long days)This method returns a copy of this OffsetDateTime with the specified number of days subtracted.
minusHours(long hours)This method returns a copy of this OffsetDateTime with the specified number of hours subtracted.
minusMinutes(long minutes)This method returns a copy of this OffsetDateTime with the specified number of minutes subtracted.
minusMonths(long months)This method returns a copy of this OffsetDateTime with the specified number of months subtracted.
 minusNanos(long nanos)This method returns a copy of this OffsetDateTime with the specified number of nanoseconds subtracted.
minusSeconds(long seconds)This method returns a copy of this OffsetDateTime with the specified number of seconds subtracted.
minusWeeks(long weeks)This method returns a copy of this OffsetDateTime with the specified number of weeks subtracted.
 minusYears(long years)This method returns a copy of this OffsetDateTime with the specified number of years subtracted.
 range(TemporalField field)This method gets the range of valid values for the specified field.
timeLineOrder()This method gets a comparator that compares two OffsetDateTime instances based solely on the instant.

下面是问题陈述的实现:年/月/日格式

Java
// import java.time.OffsetDateTime class
import java.time.OffsetDateTime;
  
public class gfg1 {
  
    public static void main(String[] args)
    {
        // now() is a method used to obtain current
        // date and time from the system clock.
        OffsetDateTime offsetDateTime
            = OffsetDateTime.now();
        // Display the offsetDateTime which will
        // display all current date and time.
        System.out.println(offsetDateTime);
        // Display Year, Month and Day using methods
        System.out.println(
            "Year  : " + offsetDateTime.getYear()
            + "| Month : " + offsetDateTime.getMonth()
            + " |Day : " + offsetDateTime.getDayOfWeek());
    }
}


Java
// import java.time.OffsetDateTime class
import java.time.OffsetDateTime;
  
public class gfg1 {
  
    public static void main(String[] args)
    {
  
        // now() is a method used to obtain current
        // date and time from the system clock.
        OffsetDateTime offsetDateTime
            = OffsetDateTime.now();
        // Display the offsetDateTime which will
        // display all current date and time.
        System.out.println(offsetDateTime);
        // Display the Hour,Minute, Second and
        // Nanosecond using getHour(), getMinute(),
        // getSecond() and getNano()
        System.out.println(
            "Hour  : " + offsetDateTime.getHour()
            + " Minute : " + offsetDateTime.getMinute()
            + " Second : " + offsetDateTime.getSecond()
            + " NanoSecond : " + offsetDateTime.getNano());
    }
}


输出

2021-02-22T17:53:28.809568Z
Year  : 2021| Month : FEBRUARY |Day : MONDAY

下面是问题陈述的实现: Hour/Minute/Second/NanoSecond 格式

Java

// import java.time.OffsetDateTime class
import java.time.OffsetDateTime;
  
public class gfg1 {
  
    public static void main(String[] args)
    {
  
        // now() is a method used to obtain current
        // date and time from the system clock.
        OffsetDateTime offsetDateTime
            = OffsetDateTime.now();
        // Display the offsetDateTime which will
        // display all current date and time.
        System.out.println(offsetDateTime);
        // Display the Hour,Minute, Second and
        // Nanosecond using getHour(), getMinute(),
        // getSecond() and getNano()
        System.out.println(
            "Hour  : " + offsetDateTime.getHour()
            + " Minute : " + offsetDateTime.getMinute()
            + " Second : " + offsetDateTime.getSecond()
            + " NanoSecond : " + offsetDateTime.getNano());
    }
}
输出
2021-02-22T17:53:28.315232Z
Hour  : 17 Minute : 53 Second : 28 NanoSecond : 315232000

参考:

https://docs.oracle.com/javase/8/docs/api/的Java/time/OffsetDateTime.html