BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java OffsetDateTime Class explained with examples

By Chaitanya Singh | Filed Under: java

OffsetDateTime represents the date-time with an offset from UTC time such as 2022-06-12T11:25:45+02:00. This class stores date-time with all date & time fields upto precision of nanoseconds, as well as offset from UTC timezone. For example, the value “12th June 2022 at 10:33.49.123456789 +01:00” can be stored in an OffsetDateTime.

Java OffsetDateTime class:

public final class OffsetDateTime extends Object
implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable

Java OffsetDateTime – Method Summary

MethodDescription
static OffsetDateTime now()Returns the current date-time from system in default timezone.
int compareTo(OffsetDateTime other)It compares the date-time with another date-time. Returns negative if this date-time less than specified date-time else returns positive.
booelan equals(Object obj)Checks if this date-time is equal to other specified date-time.
String format(DateTimeFormatter formatter)It formats the date-time using specified formatter.
int getDayOfMonth()Returns day-of-month field as an int value.
DayOfWeek getDayOfWeek()Returns day-of-week field as an enum value.
int getDayOfYear()Returns day-of-year field.
int getHour()Returns hour-of-day field. Other similar get methods are getMinute(), getSecond(), getNano() & getYear()
boolean isAfter(OffsetDateTime other)Checks if this date-time is after the specified date-time.
boolean isBefore(OffsetDateTime other)Checks if this date-time is before the specified date-time.
boolean isEqual(OffsetDateTime other)Checks if this date-time is equal to the specified date-time.
OffsetDateTime minusDays(long days)Returns a copy of this OffsetDateTime after subtracting the specified number of days from it. Similar methods are minusHours(), minusMinutes(), minusSeconds(), minusNanos(), minusWeeks(), minusMonths() & minusYears().
OffsetDateTime plusDays(long days)Returns a copy of this OffsetDateTime after adding the specified number of days to it. Similar methods are plusHours(), plusMinutes(), plusSeconds(), plusNanos(), plusWeeks(), plusMonths() & plusYears().
LocalDateTime toLocalDateTime()Gets LocalDateTime field from this date-time.
OffsetDateTime withDayOfMonth(int dayOfMonth)Returns a copy of this OffsetDateTime after altering the day-of-month field with the specified value. Similar methods are withDayOfYear(), withHour(), withMinute(), withSecond(), withNano() etc.

Java OffsetDateTime – now & get methods example

Here we are getting current date-time info from the system using now() method and then we are using get methods to obtain the value of various fields from the current OffsetDateTime.

import java.time.OffsetDateTime;
public class JavaExample {
    public static void main(String[] args) {
        OffsetDateTime offDateTime = OffsetDateTime.now();
        System.out.println("Current OffsetDateTime: "+offDateTime);
        System.out.println("getMonth(): "+offDateTime.getMonth());
        System.out.println("getDayOfWeek(): "+offDateTime.getDayOfWeek());
        System.out.println("getHour(): "+offDateTime.getHour());
        System.out.println("getMinute(): "+offDateTime.getMinute());
        System.out.println("getSecond(): "+offDateTime.getSecond());
    }
}

Output:

Current OffsetDateTime: 2022-06-12T11:36:32.571473+05:30
getMonth(): JUNE
getDayOfWeek(): SUNDAY
getHour(): 11
getMinute(): 36
getSecond(): 32

Java OffsetDateTime – isEqual(), isAfter(), isBefore() & with methods example

In this example, we are demonstrating the use of several important methods of OffsetDateTime class. The withHour() method is used to alter the hour in date-time. Methods isEqual(), isBefore() and isAfter() are used to compare two date-time.

import java.time.OffsetDateTime;
public class JavaExample {
    public static void main(String[] args) {
        OffsetDateTime offDateTime = OffsetDateTime.now();
        System.out.println("Current OffsetDateTime: "+offDateTime);

        OffsetDateTime offDateTime2 = offDateTime.withHour(9);
        System.out.println("Altered OffsetDateTime: "+offDateTime2);

        //Is current date-time equal to altered date time?
        System.out.println(offDateTime.isEqual(offDateTime2));

        //Is current date-time before altered date time?
        System.out.println(offDateTime.isBefore(offDateTime2));

        //Is current date-time after altered date time?
        System.out.println(offDateTime.isAfter(offDateTime2));
    }
}

Output:

Current OffsetDateTime: 2022-06-12T11:40:51.959723+05:30
Altered OffsetDateTime: 2022-06-12T09:40:51.959723+05:30
false
false
true

Java OffsetDateTime – plus & minus methods example

Here we will learn how to use various variants of plus and minus methods that are used to add or subtract date-time from this OffsetDateTime.

import java.time.OffsetDateTime;
public class JavaExample {
    public static void main(String[] args) {
        OffsetDateTime offDateTime = OffsetDateTime.now();
        System.out.println("Current OffsetDateTime: "+offDateTime);
        System.out.println("Minus 2 Hours: "+offDateTime.minusHours(2));
        System.out.println("Minus 3 Days: "+offDateTime.minusDays(3));
        System.out.println("Plus 5 minutes: "+offDateTime.plusMinutes(5));
        System.out.println("Plus 2 months: "+offDateTime.plusMonths(2));

    }
}

Output:

Current OffsetDateTime: 2022-06-12T11:44:32.129831+05:30
Minus 2 Hours: 2022-06-12T09:44:32.129831+05:30
Minus 3 Days: 2022-06-09T11:44:32.129831+05:30
Plus 5 minutes: 2022-06-12T11:49:32.129831+05:30
Plus 2 months: 2022-08-12T11:44:32.129831+05:30

Java OffsetDateTime – toLocalDate() example

Methods toLocalDate() and toLocalDateTime() are used to obtain local date and local date-time from offset time.

import java.time.OffsetDateTime;
public class JavaExample {
    public static void main(String[] args) {
        OffsetDateTime offDateTime = OffsetDateTime.now();
        System.out.println("LocalDate: "+offDateTime.toLocalDate());
        System.out.println("LocalDate: "+offDateTime.toLocalDateTime());
    }
}

Output:

LocalDate: 2022-06-12
LocalDate: 2022-06-12T13:04:13.274151
❮ Java Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap