📜  Java中的Java .time.OffsetTime 类

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

Java中的Java .time.OffsetTime 类

Java OffsetTime 类是一个不可变的日期时间对象,它表示时间,通常被视为时-分-秒偏移量。 OffsetTime 类表示在 ISO-8601 日历系统中与 UTC/Greenwich 有偏移量的时间,例如 18:30:45+08:00,通常被视为时-分-秒-偏移量。这个类是不可变的和线程安全的,存储所有时间字段,精度为纳秒,以及区域偏移。

语法:类声明

public final class OffsetTime extends Object   
implements Temporal, TemporalAdjuster, Comparable, Serializable  

现在让我们来谈谈收集此类方法的知识的主要怪癖,如下表所示:

Method

Action Performed 

format(DateTimeFormatter formatter)Formats this time using the specified formatter.
of(LocalTime time, ZoneOffset offset)Obtains an instance of OffsetTime from a local time and an offset.
range(TemporalField field)Gets the range of valid values for the specified field.
toLocalTime()Gets the LocalTime part of this date-time.
adjustInto(Temporal temporal)Adjust the specified temporal object to have the same offset and time as this object.
atDate(LocalDate date)Combine this time with a date to create an OffsetDateTime.
compareTo(OffsetTime other)Compare this OffsetTime to another time.
equals(Object obj)Check if this time is equal to another time.
format(DateTimeFormatter formatter)Formats this time using the specified formatter.
from(TemporalAccessor temporal)Obtains an instance of OffsetTime from a temporal object.
get(TemporalField field)Gets the value of the specified field from this time as an int.
getHour()Gets the hour-of-day field.
getLong(TemporalField field)Gets the value of the specified field from this time as a long.
getMinute()Gets the minute-of-hour field.
getNano()Gets the nano-of-second field.
getOffset()Gets the zone offset, such as ‘+01:00’.
getSecond()Gets the second-in-minute field.
hashCode()A hash code for this time.
isAfter(OffsetTime other)Check if the instant of this OffsetTime is after that of the specified time applying both times to a common date.
isBefore(OffsetTime other)Check if the instant of this OffsetTime is before that of the specified time, applying both times to a common date.
isEqual(OffsetTime other)Check if the instant of this OffsetTime is equal to that of the specified time, applying both times to a common date.
isSupported(TemporalField field)Check if the specified field is supported.
isSupported(TemporalUnit unit)Check if the specified unit is supported.
minus(long amountToSubtract, TemporalUnit unit)Return a copy of this time with the specified amount subtracted.
minus(TemporalAmount amountToSubtract)Return a copy of this time with the specified amount subtracted.
minusHours(long hours)Return a copy of this OffsetTime with the specified number of hours subtracted.
minusMinutes(long minutes)Return a copy of this OffsetTime with the specified number of minutes subtracted.
minusNanos(long nanos)Return a copy of this OffsetTime with the specified number of nanoseconds subtracted.
minusSeconds(long seconds)Return a copy of this OffsetTime with the specified number of seconds subtracted.
now()Obtains the current time from the system clock in the default time-zone.
now(Clock clock)Obtains the current time from the specified clock.
now(ZoneId zone)Obtains the current time from the system clock in the specified time-zone.
of(int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)Obtains an instance of OffsetTime from an hour, minute, second and nanosecond.
of(LocalTime time, ZoneOffset offset)Obtains an instance of OffsetTime from a local time and an offset.
ofInstant(Instant instant, ZoneId zone)Obtains an instance of OffsetTime from an Instant and zone ID.
parse(CharSequence text)Obtains an instance of OffsetTime from a text string such as 10:15:30+01:00.
parse(CharSequence text, DateTimeFormatter formatter)Obtains an instance of OffsetTime from a text string using a specific formatter.
plus(long amountToAdd, TemporalUnit unit)Return a copy of this time with the specified amount added.
plus(TemporalAmount amountToAdd)Return a copy of this time with the specified amount added.
plusHours(long hours)Return a copy of this OffsetTime with the specified number of hours added.
plusMinutes(long minutes)Return a copy of this OffsetTime with the specified number of minutes added.
plusNanos(long nanos)Return a copy of this OffsetTime with the specified number of nanoseconds added.
plusSeconds(long seconds)Return a copy of this OffsetTime with the specified number of seconds added.
query(TemporalQuery query)Queries this time using the specified query.
range(TemporalField field)Gets the range of valid values for the specified field.
toLocalTime()Gets the LocalTime part of this date-time.
toString()Outputs this time as a string, such as 10:15:30+01:00.
truncatedTo(TemporalUnit unit)Return a copy of this OffsetTime with the time truncated.
until(Temporal endExclusive, TemporalUnit unit)Calculates the amount of time until another time in terms of the specified unit.
with(TemporalAdjuster adjuster)Return an adjusted copy of this time.
with(TemporalField field, long newValue)Return a copy of this time with the specified field set to a new value.
withHour(int hour)Return a copy of this OffsetTime with the hour-of-day altered.
withMinute(int minute)Return a copy of this OffsetTime with the minute-of-hour altered.
withNano(int nanoOfSecond)Return a copy of this OffsetTime with the nano-of-second altered.
withOffsetSameInstant(ZoneOffset offset)Return a copy of this OffsetTime with the specified offset ensuring that the result is at the same instant on an implied day.
withOffsetSameLocal(ZoneOffset offset)Return a copy of this OffsetTime with the specified offset ensuring that the result is the same local time.
withSecond(int second)Return a copy of this OffsetTime with the second-minute altered.

现在让我们实现上表中列出的一些上述方法,以深入了解这些方法在内部是如何工作的。

示例 1: now() 方法

Java
// Java Program to Implement OffsetTime class
// via now() method
 
// Importing required classes
import java.time.OffsetTime;
import java.time.temporal.ChronoField;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of class in main() method
        OffsetTime offset = OffsetTime.now();
 
        int h = offset.get(ChronoField.HOUR_OF_DAY);
        System.out.println(h);
 
        int m = offset.get(ChronoField.MINUTE_OF_DAY);
        System.out.println(m);
 
        int s = offset.get(ChronoField.SECOND_OF_DAY);
        System.out.println(s);
    }
}


Java
// Java Program to Implement OffsetTime Class
// via getHour() method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an instance of class and
        // operation now() method over it to
        // get the current time from clock
        OffsetTime offset = OffsetTime.now();
 
        // Getting the hour of day field
        int h = offset.getHour();
 
        // Print and display the hours
        System.out.println(h + " hours");
    }
}


Java
// Java Program to Implement OffsetTime Class
// via getMinute() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an instance of the class
        OffsetTime offset = OffsetTime.now();
 
        // Getting the minutes of the day field
        int m = offset.getMinute();
 
        // Print and display the minutes
        System.out.println(m + " minutes");
    }
}


Java
// Java Program to Implement OffsetTime Class
// via getSecond() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an instance of the class
        OffsetTime offset = OffsetTime.now();
 
        // Getting the second from the day field
        int s = offset.getSecond();
 
        // Print and display the seconds
        System.out.println(s + " seconds");
    }
}


Java
// Java Program to Implement OffsetTime Class
// via getNano() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // reating an instance of the class
        // in the main() method
        OffsetTime offset = OffsetTime.now();
 
        // Getting the nano-of-second field
        int n = offset.getNano();
 
        // Print and displa the nanoseconds
        System.out.println(n + " nanoseconds");
    }
}


Java
// Java Program to Implement OffsetTime Class
// via of() Method
 
// Importing desired classes from java.time package
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Obtaining an instance of OffsetTime
        // from a local time and an offset.
        OffsetTime time = OffsetTime.of(LocalTime.now(),
                                        ZoneOffset.UTC);
 
        // Print and display the time
        System.out.println(time);
    }
}


Java
// Java Program to Implement OffsetTime Class
// via toLocalTime() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the current time from the system clock
        // in the default time zone
        OffsetTime time = OffsetTime.now();
 
        // Getting the LocalTime part of this date-time
        System.out.println(time.toLocalTime());
    }
}


输出
4
253
15211

示例 2: getHour() 方法

Java

// Java Program to Implement OffsetTime Class
// via getHour() method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an instance of class and
        // operation now() method over it to
        // get the current time from clock
        OffsetTime offset = OffsetTime.now();
 
        // Getting the hour of day field
        int h = offset.getHour();
 
        // Print and display the hours
        System.out.println(h + " hours");
    }
}
输出
4 hours

示例 3: getMinute() 方法

Java

// Java Program to Implement OffsetTime Class
// via getMinute() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an instance of the class
        OffsetTime offset = OffsetTime.now();
 
        // Getting the minutes of the day field
        int m = offset.getMinute();
 
        // Print and display the minutes
        System.out.println(m + " minutes");
    }
}
输出
12 minutes

示例 4: getSecond() 方法

Java

// Java Program to Implement OffsetTime Class
// via getSecond() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an instance of the class
        OffsetTime offset = OffsetTime.now();
 
        // Getting the second from the day field
        int s = offset.getSecond();
 
        // Print and display the seconds
        System.out.println(s + " seconds");
    }
}
输出
40 seconds

示例 5: getNano() 方法 

Java

// Java Program to Implement OffsetTime Class
// via getNano() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // reating an instance of the class
        // in the main() method
        OffsetTime offset = OffsetTime.now();
 
        // Getting the nano-of-second field
        int n = offset.getNano();
 
        // Print and displa the nanoseconds
        System.out.println(n + " nanoseconds");
    }
}
输出
464297000 nanoseconds

示例 6: of() 方法 

Java

// Java Program to Implement OffsetTime Class
// via of() Method
 
// Importing desired classes from java.time package
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Obtaining an instance of OffsetTime
        // from a local time and an offset.
        OffsetTime time = OffsetTime.of(LocalTime.now(),
                                        ZoneOffset.UTC);
 
        // Print and display the time
        System.out.println(time);
    }
}
输出
04:12:05.574520Z

示例 7: toLocalTime() 方法

Java

// Java Program to Implement OffsetTime Class
// via toLocalTime() Method
 
// Importing the class from java.time package
import java.time.OffsetTime;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the current time from the system clock
        // in the default time zone
        OffsetTime time = OffsetTime.now();
 
        // Getting the LocalTime part of this date-time
        System.out.println(time.toLocalTime());
    }
}
输出
04:11:47.834567