java new localdatetime

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

/**
 * Java Program to demonstrate How to use LocalDateTime class in Java 8.
 * LocalDateTime is LocalDate + LocalTime, i.e. date with time

 * @author WINDOWS 8
 */
public class Java8Demo {

    public static void main(String args[])  {

        // LocalDate is date without time in Java 8
        // LocalTime is time without date in Java 8
        // LocalDateTime is both date and time e.g. LocalDate + LocalTime
        // but without Timezone information
        
        
        // LocalDateTime.now() creates a LocalDateTieme object with current
        // date and time
        LocalDateTime rightNow = LocalDateTime.now();
        System.out.println("current datetime : " + rightNow);
        
        
        // LocalDateTime.of() method is a factory method to careate 
        // LocalDateTiem with specific date and time
        LocalDateTime aDateTime = LocalDateTime.of(2015, 
                                           Month.JULY, 29, 19, 30, 40);
        System.out.println("some datetime : " + aDateTime);
        
        // You can also create LocalDateTime object by combining LocalDate
        // and LocalTime
        LocalDate currentDate = LocalDate.now();
        LocalTime currentTime = LocalTime.now();
        LocalDateTime fromDateAndTime = LocalDateTime.of(currentDate,
                                                           currentTime);
        System.out.println("LocalDateTime created by combining LocalDate"
                + " and LocalTime" + fromDateAndTime);
        
        
        // You can also retrieve LocalDate and LocalTime from LocalDateTime
        LocalDate retrievedDate = fromDateAndTime.toLocalDate();
        LocalTime retrievedTime = fromDateAndTime.toLocalTime();
        System.out.println("retreived LocalDate : " + retrievedDate);
        System.out.println("retreived LocalTime : " + retrievedTime);
        
    }

}

Output :
current datetime : 2015-08-02T00:29:53.949
some datetime : 2015-07-29T19:30:40
LocalDateTime created by combining LocalDate 
 and LocalTime2015-08-02T00:29:53.949
retreived LocalDate : 2015-08-02
retreived LocalTime : 00:29:53.949

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source