Close

Java Date Time - OffsetDateTime.now() 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 now()

Returns the current date-time from the system clock in the default time-zone.


public static OffsetDateTime now(ZoneId zone)

Returns the current date-time from the system clock and in provided ZoneId.


public static OffsetDateTime now(Clock clock)

Returns the current date-time from provided clock.



Examples


package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;

public class NowExample {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.now();
System.out.println(d);
}
}

Output

2017-05-01T16:16:35.690-05:00




package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneId;

public class NowExample2 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.now(ZoneId.of("+1"));
System.out.println(d);

OffsetDateTime d2 = OffsetDateTime.now(ZoneId.of("+013020"));
System.out.println(d2);

OffsetDateTime d3 = OffsetDateTime.now(ZoneId.of("UTC+01:00"));
System.out.println(d3);

OffsetDateTime d4 = OffsetDateTime.now(ZoneId.of("GMT+01:00"));
System.out.println(d4);
}
}

Output

2017-05-01T22:16:37.683+01:00
2017-05-01T22:46:57.683+01:30:20
2017-05-01T22:16:37.683+01:00
2017-05-01T22:16:37.683+01:00




package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneId;

public class NowExample3 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.now(
ZoneId.of("America/Argentina/Buenos_Aires"));
System.out.println(d);
}
}

Output

2017-05-01T18:16:39.870-03:00




package com.logicbig.example.offsetdatetime;

import java.time.Clock;
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class NowExample4 {

public static void main (String... args) {
OffsetDateTime d = OffsetDateTime.now(Clock.tickMinutes(
ZoneId.systemDefault()));
System.out.println(d);

OffsetDateTime d2 = OffsetDateTime.now(Clock.tickMinutes(
ZoneId.of("Indian/Maldives")));
System.out.println(d2);
}
}

Output

2017-05-01T16:16-05:00
2017-05-02T02:16+05:00




See Also