java.time.LocalDateTime.atOffset() Method Example



Description

The java.time.LocalDateTime.atOffset(ZoneOffset offset) method combines this date with an offset to create an OffsetDateTime.

Declaration

Following is the declaration for java.time.LocalDateTime.atOffset(ZoneOffset offset) method.

public OffsetDateTime atOffset(ZoneOffset offset)

Parameters

offset − the offset to combine with, not null.

Return Value

the offset date-time formed from this date and the specified time, not null.

Example

The following example shows the usage of java.time.LocalDateTime.atOffset(ZoneOffset offset) method.

package com.tutorialspoint;

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

public class LocalDateTimeDemo {
   public static void main(String[] args) {
 LocalDateTime date = LocalDateTime.parse("2017-02-03T12:30:30");
      System.out.println(date);  
      OffsetDateTime date1 = date.atOffset(ZoneOffset.ofHours(2));
      System.out.println(date1);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-02-03T12:30:30
2017-02-03T12:30:30+02:00
Advertisements