Wednesday, July 3, 2024
HomeLanguagesJavaOffsetTime atDate() method in Java with examples

OffsetTime atDate() method in Java with examples

The atDate() method of OffsetTime class in Java combines this time with a date to create a OffsetDateTime.

Syntax :

public OffsetDateTime atDate(LocalDate date)

Parameter: This method accepts a single parameter date which specifies the date to combine with, not null.

Return Value: It returns the OffsetDateTime formed from this time and the specified date, not null

Below programs illustrate the atDate() method:

Program 1 :




// Java program to demonstrate the atDate() method
  
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
        // parses the current date
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);
  
        // Parses the current time
        OffsetTime time = OffsetTime.parse("11:10:10+06:03");
        OffsetDateTime datetime = time.atDate(date);
        System.out.println("Current date and time: " + datetime);
    }
}


Output:

Current date: 2018-12-31
Current date and time: 2018-12-31T11:10:10+06:03

Program 2 :




// Java program to demonstrate the atDate() method
  
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
        // parses the current date
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);
  
        // Parses the current time
        OffsetTime time = OffsetTime.parse("12:15:14+16:03");
        OffsetDateTime datetime = time.atDate(date);
        System.out.println("Current date and time: " + datetime);
    }
}


Output:

Current date: 2018-12-31
Current date and time: 2018-12-31T12:15:14+16:03

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#atDate-java.time.LocalDate-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments