Date and Time

formatting date using new date time api

Table of Contents

Program

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

public class FormatDateTimeApi 
  {

   public static void main(String args[]) 
   {
      FormatDateTimeApi java8tester = new FormatDateTimeApi();
      java8tester.testLocalDateTime();
   }
	
   public void testLocalDateTime()   
   {
     
      LocalDateTime currentTime = LocalDateTime.now();
      System.out.println("Current DateTime: " + currentTime);
		
      LocalDate date1 = currentTime.toLocalDate();
      System.out.println("date1: " + date1);
		
      Month month = currentTime.getMonth();
      int day = currentTime.getDayOfMonth();
      int seconds = currentTime.getSecond();
		
      System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);
		
      LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
      System.out.println("date2: " + date2);
      
      LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
      System.out.println("date3: " + date3);
		
    
      LocalTime date4 = LocalTime.of(22, 15);
      System.out.println("date4: " + date4);
		
      
      LocalTime date5 = LocalTime.parse("20:15:30");
      System.out.println("date5: " + date5);
   }
}

Output

Current DateTime: 2021-05-17T17:45:57.928069900
date1: 2021-05-17
Month: MAYday: 17seconds: 57
date2: 2012-05-10T17:45:57.928069900
date3: 2014-12-12
date4: 22:15
date5: 20:15:30

Description

public static LocalDateTime now()

Obtains the current date-time from the system clock in the default time-zone.
This will query the system clock in the default time-zone to obtain the current date-time.

Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.

Returns:

the current date-time using the system clock and default time-zone, not null

public LocalDate toLocalDate()

Gets the LocalDate part of this date-time.
This returns a LocalDate with the same year, month and day as this date-time.

Specified by:

toLocalDate in interface ChronoLocalDateTime<LocalDate>

Returns:

the date part of this date-time, not null

public Month getMonth()

Gets the month-of-year field using the Month enum.
This method returns the enum Month for the month. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int value.

Returns:

the month-of-year, not null

public int getDayOfMonth()

Gets the day-of-month field.
This method returns the primitive int value for the day-of-month.

Returns:

the day-of-month, from 1 to 31

public int getSecond()

Gets the second-of-minute field.

Returns:

the second-of-minute, from 0 to 59

public LocalDateTime withDayOfMonth(int dayOfMonth)

Returns a copy of this LocalDateTime with the day-of-month altered.
If the resulting date-time is invalid, an exception is thrown. The time does not affect the calculation and will be the same in the result.

This instance is immutable and unaffected by this method call.

Parameters:

dayOfMonth – the day-of-month to set in the result, from 1 to 28-31

Returns:

a LocalDateTime based on this date-time with the requested day, not null

public LocalDateTime withYear(int year)

Returns a copy of this LocalDateTime with the year altered.
The time does not affect the calculation and will be the same in the result. If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.

This instance is immutable and unaffected by this method call.

Parameters:

year – the year to set in the result, from MIN_YEAR to MAX_YEAR

Returns:

a LocalDateTime based on this date-time with the requested year, not null

public static LocalDate of(int year,
Month month,
int dayOfMonth)

Obtains an instance of LocalDate from a year, month and day.
This returns a LocalDate with the specified year, month and day-of-month. The day must be valid for the year and month, otherwise, an exception will be thrown.

Parameters:

year – the year to represent, from MIN_YEAR to MAX_YEAR
month – the month-of-year to represent, not null
dayOfMonth – the day-of-month to represent, from 1 to 31

Returns:

the local date, not null

public static LocalTime of(int hour,
int minute)

Obtains an instance of LocalTime from an hour and a minute.
This returns a LocalTime with the specified hour and minute. The second and nanosecond fields will be set to zero.

Parameters:

hour – the hour-of-day to represent, from 0 to 23
minute – the minute-of-hour to represent, from 0 to 59

Returns:

the local time, not null

public static LocalTime parse(CharSequence text)

Obtains an instance of LocalTime from a text string such as 10:15.
The string must represent a valid time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME.

Parameters:

text – the text to parse such as “10:15:30”, not null

Returns:

the parsed local time, not null