Close

Java Date Time - OffsetDateTime.of() Examples

Java Date Time Java Java API 


Class:

java.time.OffsetDateTime

java.lang.Objectjava.lang.Objectjava.time.OffsetDateTimejava.time.OffsetDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public static OffsetDateTime of(LocalDate date,

LocalTime time,

ZoneOffset offset)

Returns an instance of OffsetDateTime from a date, time and offset.


public static OffsetDateTime of(LocalDateTime dateTime,

ZoneOffset offset)

Returns an instance of OffsetDateTime from a date-time and offset.


public static OffsetDateTime of(int year,

int month,

int dayOfMonth,

int hour,

int minute,

int second,

int nanoOfSecond,

ZoneOffset offset)

Returns a new OffsetDateTime instance from the specified year, month, dayOfMonth, hour, minute, second, nanoOfSection and ZoneOffset.

In all methods, if provided values are not valid DateTimeException is thrown.



Examples


package com.logicbig.example.offsetdatetime;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OfExample {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(LocalDate.of(2015, 10, 18),
LocalTime.of(11, 20, 30, 1000),
ZoneOffset.ofHours(-5));
System.out.println(d);

OffsetDateTime d2 = OffsetDateTime.of(LocalDate.now(),
LocalTime.now(),
ZoneOffset.UTC);
System.out.println(d2);
}
}

Output

2015-10-18T11:20:30.000001-05:00
2017-05-01T16:16:28.863Z




package com.logicbig.example.offsetdatetime;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OfExample2 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(LocalDateTime.of(2015, 10, 18,
11, 20, 30, 1000),
ZoneOffset.of("+11"));
System.out.println(d);
}
}

Output

2015-10-18T11:20:30.000001+11:00




package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OfExample3 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.of(2015, 11, 15, 18, 20, 30, 100,
ZoneOffset.ofHours(-10));
System.out.println(d);
}
}

Output

2015-11-15T18:20:30.000000100-10:00




See Also