Java 8 – LocalDateTime with method details and examples

In this article, we will discuss about newly introduced LocalDateTime in Java 1.8 version for dealing with date and time in program with ease and convenience.

Prior to introducing LocalDate, LocalTime and LocalDateTime in Java 1.8 version, we have to deal with java.util.Date, java.util.Calendar, java.sql.Timestamp, java.sql.Time, java.util.TimeZone for date/time handling in Java which isn’t easy & straight-forward and there are few issues as mentioned below,

  • Thread-safety :- Existing Date/Time classes and its methods aren’t thread-safe and hence it’s not convenient to handle in concurrent/parallel environment
  • Not so-easy API design :- Existing Date/Time classes’ methods aren’t convenient to use in day-to-day programmer’s coding or development
  • Time-zone settings :- Developers or Programmer’s life becomes difficult while dealing with time zone settings in programs

Let’s move forward and discuss about java.time.LocalDateTime introduced in Java 1.8 version

1. LocalDateTime :

  • There are 3 ways to get/form a LocalDateTime,
    1. First is to get current system date/time using static factory method now()
    2. Second is to form a LocalDateTime using static factory method of() passing year, month, day, hour, minute, second and nano information
    3. Third and final is to parse date/time in String-form to LocalDateTime using static factory method parse()
  • The fully qualified package/class name of LocalDateTime is java.time.LocalDateTime i.e.; LocalDateTime is present under java.time package
  • Class declaration for LocalDateTime is as follows,
package java.time;

public final class LocalDateTime
        implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
}

2. LocalDateTime methods or APIs :

  • Important LocalDateTime method details,
    • now() – get current date-time from the system clock in the default time-zone
    • of() – get an instance of LocalDateTime from a year, month, day, hour, minute, second and nanoseconds passed
    • parse() – get an instance of LocalDateTime from a text string in yyyy-MM-ddTHH:mm:ss.nnn or yyyy-MM-ddTHH:mm:ss or yyyy-MM-ddTHH:mm formats (there are 9 variants)
    • getYear() – get the Year field from LocalDateTime 
    • getMonthValue() – get the month-of-year field from 1 to 12 from LocalDateTime 
    • getMonth() – get the month-of-year field using the Month enum from LocalDateTime 
    • getDayOfMonth() – get the day-of-month field from LocalDateTime 
    • getDayOfWeek() – get the day-of-week field, which is an enum DayOfWeek from LocalDateTime 
    • getDayOfYear() – get the day-of-year field from LocalDateTime
    • getHour() – get the hour-of-day field from LocalDateTime
    • getMinute() – get the minute-of-hour field from LocalDateTime
    • getSecond() – get the  second-of-minute field from LocalDateTime
    • getNano() – get the nano-of-second field from LocalDateTime
    • plusDays() – Returns a copy of this LocalDateTime with the specified number of days added
    • plusWeeks() – Returns a copy of this LocalDateTime with the specified number of weeks added
    • plusMonths() – Returns a copy of this LocalDateTime with the specified number of months added
    • plusYears() – Returns a copy of this LocalDateTime with the specified number of years added
    • plusNanos() – Returns a copy of this LocalDateTime with the specified number of nanoseconds added
    • plusSeconds() – Returns a copy of this LocalDateTime with the specified number of seconds added
    • plusMinutes() – Returns a copy of this LocalDateTime with the specified number of minutes added
    • plusHours() – Returns a copy of this LocalDateTime with the specified number of hours added
    • minusDays() – Returns a copy of this LocalDateTime with the specified number of days subtracted
    • minusWeeks() – Returns a copy of this LocalDateTime with the specified number of weeks subtracted
    • minusMonths() – Returns a copy of this LocalDateTime with the specified number of months subtracted
    • minusYears() – Returns a copy of this LocalDateTime with the specified number of years subtracted
    • minusNanos() – Returns a copy of this LocalDateTime with the specified number of nanoseconds subtracted
    • minusSeconds() – Returns a copy of this LocalDateTime with the specified number of seconds subtracted
    • minusMinutes() – Returns a copy of this LocalDateTime with the specified number of minutes subtracted
    • minusHours() – Returns a copy of this LocalDateTime with the specified number of hours subtracted
    • format() – Formats invoking date-time using the specified date formatter
    • withDayOfMonth() – Returns a copy of this LocalDateTime with the day-of-month altered
    • withMonth() – Returns a copy of this LocalDateTime with the month-of-year altered
    • withYear() – Returns a copy of this LocalDateTime with the year altered
    • withHour() – Returns a copy of this LocalDateTime with the hour-of-day altered
    • withMinute() – Returns a copy of this LocalDateTime with the minute-of-hour altered
    • withSecond() – Returns a copy of this LocalDateTime with the second-of-minute altered
    • withNano() – Returns a copy of this LocalDateTime with the nano-of-second altered

3. LocalDateTime examples :

  1. LocalDateTime.now() – get current date/time from system clock
  2. LocalDateTime.of() – form LocalDateTime passing Year, Month, Day, Hour, Minute, Second and Nano fields
  3. LocalDateTime.parse() – parse the date/time in String-form to LocalDateTime
  4. Adding day, week, month and year to LocalDateTime using plusDays(), plusWeeks(), plusMonths() and plusYears() methods respectively
  5. Adding nano, second, minute and hour to LocalDateTime using plusNanos(), plusSeconds(), plusMinutes() and plusHours() methods respectively
  6. Subtracting day, week, month and year from LocalDateTime using minusDays(), minusWeeks(), minusMonths() and minusYears() methods respectively
  7. Subtracting nano, second, minute and hour from LocalDateTime using minusNanos(), minusSeconds(), minusMinutes() and minusHours() methods respectively
  8. Formatting LocalDateTime in different formats using DateTimeFormatter class
  9. Altering day, month, year, nano, second, minute and hour fields with LocalDateTime using withDayOfMonth(), withMonth(), withYear(), withNano(), withSecond(), withMinute() and withHour() methods respectively

3.1 LocalDateTime.now() method – get Current System Date/time

  • LocalDateTime.now() method helps to get current system date/time which will be in the yyyy-MM-ddTHH:mm:ss.nnn format
  • We can get year, month, day, hour, minute, second and nano-second field/part from LocalDateTime using different methods mentioned above and then we can form our own format as required like “dd.MM.yyyy HH:mm:ss.nnn

LocalDateTimeExampleUsingNowMethod.java

package in.bench.resources.localdatetime.sorting;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeExampleUsingNowMethod {

	public static void main(String[] args) {

		// get current system date along with time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Today's local date/time is = " + localDateTime);


		// 1. Date part
		System.out.println("\n1. Date part from LocalDateTime : \n");


		// 1.1 get YEAR part from current system date
		int year = localDateTime.getYear();
		System.out.println("Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = localDateTime.getMonthValue();
		System.out.println("Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = localDateTime.getMonth();
		System.out.println("Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = localDateTime.getDayOfMonth();
		System.out.println("Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
		System.out.println("Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = localDateTime.getDayOfYear();
		System.out.println("Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n2. Time part from LocalDateTime : \n");


		// 2.1 get HOUR value from current system time
		int hours = localDateTime.getHour();
		System.out.println("Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = localDateTime.getMinute();
		System.out.println("Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = localDateTime.getSecond();
		System.out.println("Seconds is : " + seconds);


		// 2.4 get NANO SECOND value from current system time
		int nano = localDateTime.getNano();
		System.out.println("Nano Seconds is : " + nano);
	}
}

Output:

Today's local date/time is = 2022-06-23T19:32:48.179955500

1. Date part from LocalDateTime : 

Year is : 2022
Month is : 6
Month in Words is : JUNE
Day is : 23
Day of Week is : THURSDAY
Day of Year is : 174

2. Time part from LocalDateTime : 

Hour is : 19
Minutes is : 32
Seconds is : 48
Nano Seconds is : 179955500

3.2 LocalDateTime.of() method – form Date/time

  • Passing year, month, day, hour, minute, second and nano-second field/part to LocalDateTime.of() method returns LocalDateTime which will be in the yyyy-MM-ddTHH:mm:ss.nnn format
  • We can extract different fields like year, month, day, hour, minute, second and nano-second from LocalDateTime and then form our own format as required like “dd/MM/yyyy HH:mm:ss.nnn

LocalDateTimeExampleUsingOfMethod.java

package in.bench.resources.localdatetime.sorting;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeExampleUsingOfMethod {

	public static void main(String[] args) {

		// for specific date along with time
		LocalDateTime dateTime = LocalDateTime.of(1950, Month.JANUARY, 26, 11, 45, 37, 987000000);
		System.out.println("Date/time is = " + dateTime);


		// 1. Date part
		System.out.println("\n1. Date part from LocalDateTime :- ");


		// 1.1 get YEAR part from current system date
		int year = dateTime.getYear();
		System.out.println("\n1.1 Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = dateTime.getMonthValue();
		System.out.println("\n1.2 Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = dateTime.getMonth();
		System.out.println("\n1.3 Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = dateTime.getDayOfMonth();
		System.out.println("\n1.4 Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
		System.out.println("\n1.5 Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = dateTime.getDayOfYear();
		System.out.println("\n1.6 Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n\n2. Time part from LocalDateTime :- ");


		// 2.1 get HOUR value from current system time
		int hours = dateTime.getHour();
		System.out.println("\n2.1 Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = dateTime.getMinute();
		System.out.println("\n2.2 Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = dateTime.getSecond();
		System.out.println("\n2.3 Seconds is : " + seconds);


		// 2.4 get NANO value from current system time
		int nano = dateTime.getNano();
		System.out.println("\n2.4 Nano is : " + nano);
	}
}

Output:

Date/time is = 1950-01-26T11:45:37.987

1. Date part from LocalDateTime :- 

1.1 Year is : 1950

1.2 Month is : 1

1.3 Month in Words is : JANUARY

1.4 Day is : 26

1.5 Day of Week is : THURSDAY

1.6 Day of Year is : 26


2. Time part from LocalDateTime :- 

2.1 Hour is : 11

2.2 Minutes is : 45

2.3 Seconds is : 37

2.4 Nano is : 987000000

3.3 LocalDateTime.parse() method – get Date/time in String-form

  • Sometimes, we need to parse date/time passed in String-form to LocalDateTime, for that we can use LocalDateTime.parse() method which will return LocalDateTime in yyyy-MM-ddTHH:mm:ss.nnn format
  • While parsing Date/Time, value in String-form should be in either of the below formats only, otherwise java.time.format.DateTimeParseException will be thrown
    • yyyy-MM-ddTHH:mm:ss.nnn
    • yyyy-MM-ddTHH:mm:ss
    • yyyy-MM-ddTHH:mm

LocalDateTimeExampleUsingParseMethod.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class LocalDateTimeExampleUsingParseMethod {

	public static void main(String[] args) {

		// 1. LocalDateTime with all fields y, M, d, H, m, s, n
		String dateTimeInStr1 = "1950-01-26T11:17:39.987654321";


		// 1.1 convert/parse to dateInString to LocalDateTime
		LocalDateTime dateTime1 = LocalDateTime.parse(dateTimeInStr1);
		System.out.println("1. Parsed date time is - " + dateTime1);



		// 2. LocalDateTime with fields y, M, d, H, m, s
		String dateTimeInStr2 = "1999-07-23T07:43:27";


		// 2.1 convert/parse to dateInString to LocalDateTime
		LocalDateTime dateTime2 = LocalDateTime.parse(dateTimeInStr2);
		System.out.println("2. Parsed date time is - " + dateTime2);



		// 3. LocalDateTime with fields y, M, d, H, m
		String dateTimeInStr3 = "2022-06-23T05:30";


		// 3.1 convert/parse to dateInString to LocalDateTime
		LocalDateTime dateTime3 = LocalDateTime.parse(dateTimeInStr3);
		System.out.println("3. Parsed date time is - " + dateTime3);
	}
}

Output:

1. Parsed date time is - 1950-01-26T11:17:39.987654321
2. Parsed date time is - 1999-07-23T07:43:27
3. Parsed date time is - 2022-06-23T05:30

3.4 Adding Day/Week/Month/Year to LocalDateTime :

  1. Add 5 Days to current system LocalDateTime using plusDays() method
  2. Add 2 Weeks to current system LocalDateTime using plusWeeks() method
  3. Add 3 Months to current system LocalDateTime using plusMonths() method
  4. Add 1 Year to current system LocalDateTime using plusYears() method

AddDatePartWithLocalDateTime.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class AddDatePartWithLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current System Date/time is = " + localDateTime);


		// 1.1 add 5 days with current system date/time
		LocalDateTime add_5_Days = localDateTime.plusDays(5);
		System.out.println("\n1.1 After adding 5 Days to Current System Date/time is = " 
				+ add_5_Days);


		// 1.2 add 2 weeks to current system date/time
		LocalDateTime add_2_Weeks = localDateTime.plusWeeks(2);
		System.out.println("\n1.2 After adding 2 Weeks to Current System Date/time is = " 
				+ add_2_Weeks);


		// 1.3 add 3 months to current system date/time
		LocalDateTime add_3_Months = localDateTime.plusMonths(3);
		System.out.println("\n1.3 After adding 3 Months to Current System Date/time is = " 
				+ add_3_Months);


		// 1.4 add 1 year to current system date/time
		LocalDateTime add_1_Year = localDateTime.plusYears(1);
		System.out.println("\n1.4 After adding 1 Year to Current System Date/time is = " 
				+ add_1_Year);
	}
}

Output:

1. Current System Date/time is = 2022-06-23T19:34:46.705338300

1.1 After adding 5 Days to Current System Date/time is = 2022-06-28T19:34:46.705338300

1.2 After adding 2 Weeks to Current System Date/time is = 2022-07-07T19:34:46.705338300

1.3 After adding 3 Months to Current System Date/time is = 2022-09-23T19:34:46.705338300

1.4 After adding 1 Year to Current System Date/time is = 2023-06-23T19:34:46.705338300

3.5 Adding Nano/Second/Minute/Hour to LocalDateTime :

  1. Add 125 Nanos to current system LocalDateTime using plusNanos() method
  2. Add 37 Seconds to current system LocalDateTime using plusSeconds() method
  3. Add 19 Minutes to current system LocalDateTime using plusMinutes() method
  4. Add 5 Hours to current system LocalDateTime using plusHours() method

AddTimePartWithLocalDateTime.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class AddTimePartWithLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current System Date/time is - " + localDateTime);


		// 1.1 add 125 NanoSeconds to current system Date/time
		LocalDateTime add_125_Nanos = localDateTime.plusNanos(125);
		System.out.println("\n1.1 After adding 125 Nano Seconds to Current System Date/time is - " 
				+ add_125_Nanos);


		// 1.2 add 37 Seconds to current system Date/time
		LocalDateTime add_37_Seconds = localDateTime.plusSeconds(37);
		System.out.println("\n1.2 After adding 37 Seconds to Current System Date/time is - " 
				+ add_37_Seconds);


		// 1.3 add 19 Minutes to current system Date/time
		LocalDateTime add_19_Minutes = localDateTime.plusMinutes(19);
		System.out.println("\n1.3 After adding 19 Minutes to Current System Date/time is - " 
				+ add_19_Minutes);


		// 1.4 add 5 Hours to current system Date/time
		LocalDateTime add_5_Hours = localDateTime.plusHours(5);
		System.out.println("\n1.4 After adding 5 Hours to Current System Date/time is - " 
				+ add_5_Hours);
	}
}

Output:

1. Current System Date/time is - 2022-06-23T19:35:07.327247900

1.1 After adding 125 Nano Seconds to Current System Date/time is - 2022-06-23T19:35:07.327248025

1.2 After adding 37 Seconds to Current System Date/time is - 2022-06-23T19:35:44.327247900

1.3 After adding 19 Minutes to Current System Date/time is - 2022-06-23T19:54:07.327247900

1.4 After adding 5 Hours to Current System Date/time is - 2022-06-24T00:35:07.327247900

3.6 Subtracting Day/Week/Month/Year from LocalDateTime :

  1. Subtract 5 Days from current system LocalDateTime using minusDays() method
  2. Subtract 2 Weeks from current system LocalDateTime using minusWeeks() method
  3. Subtract 3 Months from current system LocalDateTime using minusMonths() method
  4. Subtract 1 Year from current system LocalDateTime using minusYears() method

SubtractDatePartFromLocalDateTime.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class SubtractDatePartFromLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current System Date/time is - " + localDateTime);


		// 1.1 subtract 5 days from current system Date/time
		LocalDateTime subtract_5_Days = localDateTime.minusDays(5);
		System.out.println("\n1.1 After subtracting 5 Days from Current System Date/time is - " 
				+ subtract_5_Days);


		// 1.2 subtract 2 weeks from current system Date/time
		LocalDateTime subtract_2_Weeks = localDateTime.minusWeeks(2);
		System.out.println("\n1.2 After subtracting 2 Weeks from Current System Date/time is - " 
				+ subtract_2_Weeks);


		// 1.3 subtract 3 months from current system Date/time
		LocalDateTime subtract_3_Months = localDateTime.minusMonths(3);
		System.out.println("\n1.3 After subtracting 3 Months from Current System Date/time is - " 
				+ subtract_3_Months);


		// 1.4 subtract 1 year from current system Date/time
		LocalDateTime subtract_1_Year = localDateTime.minusYears(1);
		System.out.println("\n1.4 After subtracting 1 Year from Current System Date/time is - " 
				+ subtract_1_Year);
	}
}

Output:

1. Current System Date/time is - 2022-06-23T19:36:04.834354

1.1 After subtracting 5 Days from Current System Date/time is - 2022-06-18T19:36:04.834354

1.2 After subtracting 2 Weeks from Current System Date/time is - 2022-06-09T19:36:04.834354

1.3 After subtracting 3 Months from Current System Date/time is - 2022-03-23T19:36:04.834354

1.4 After subtracting 1 Year from Current System Date/time is - 2021-06-23T19:36:04.834354

3.7 Subtracting Nano/Second/Minute/Hour from LocalDateTime :

  1. Subtract 125 Nanos from current system LocalDateTime using minusNanos() method
  2. Subtract 37 Seconds from current system LocalDateTime using minusSeconds() method
  3. Subtract 19 Minutes from current system LocalDateTime using minusMinutes() method
  4. Subtract 5 Hours from current system LocalDateTime using minusHours() method

SubtractTimePartFromLocalDateTime.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class SubtractTimePartFromLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current System Date/time is - " + localDateTime);


		// 1.1 subtract 125 NanoSeconds from current system Date/time
		LocalDateTime sub_125_Nanos = localDateTime.minusNanos(125);
		System.out.println("\n1.1 After subtracting 125 Nano Seconds from Current System Date/time is - " 
				+ sub_125_Nanos);


		// 1.2 subtract 37 Seconds from current system Date/time
		LocalDateTime sub_37_Seconds = localDateTime.minusSeconds(37);
		System.out.println("\n1.2 After subtracting 37 Seconds from Current System Date/time is - " 
				+ sub_37_Seconds);


		// 1.3 subtract 19 Minutes from current system Date/time
		LocalDateTime sub_19_Minutes = localDateTime.minusMinutes(19);
		System.out.println("\n1.3 After subtracting 19 Minutes from Current System Date/time is - " 
				+ sub_19_Minutes);


		// 1.4 subtract 5 Hours from current system Date/time
		LocalDateTime sub_5_Hours = localDateTime.minusHours(5);
		System.out.println("\n1.4 After subtracting 5 Hours from Current System Date/time is - " 
				+ sub_5_Hours);
	}
}

Output:

1. Current System Date/time is - 2022-06-23T19:36:28.729563700

1.1 After subtracting 125 Nano Seconds from Current System Date/time is - 2022-06-23T19:36:28.729563575

1.2 After subtracting 37 Seconds from Current System Date/time is - 2022-06-23T19:35:51.729563700

1.3 After subtracting 19 Minutes from Current System Date/time is - 2022-06-23T19:17:28.729563700

1.4 After subtracting 5 Hours from Current System Date/time is - 2022-06-23T14:36:28.729563700

3.8 Formatting LocalDateTime using DateTimeFormatter:

  • We can convert default ISO_LOCAL_DATE_TIME LocalDateTime format yyyy-MM-ddTHH:mm:ss.nnn to any other formats using LocalDateTime.format() method by passing DateTimeFormatter class as argument with required pattern in String-form
  • In this illustration, we are using 5 different custom formats as mentioned below,
    1. DateTimeFormatter.ofPattern(“dd.MM.yyyy“)
    2. DateTimeFormatter.ofPattern(“dd-MMM-yyyy“)
    3. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm“)
    4. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss“)
    5. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss.nnn“)

FormattingLocalDateTimeUsingFormatMethod.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class FormattingLocalDateTimeUsingFormatMethod {

	public static void main(String[] args) {

		// 1. get current system Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current Date/time in ISO_LOCAL_DATE_TIME format is = "
				+ localDateTime);


		// 1.1 format LocalDateTime to dd.MM.yyyy
		String formattedDate = localDateTime.format(DateTimeFormatter
				.ofPattern("dd.MM.yyyy"));
		System.out.println("\n1.1 Current Date/time in dd.MM.yyyy format is = "
				+ formattedDate);


		// 1.2 format LocalDateTime to dd-MMM-yyyy
		String formattedDate2 = localDateTime.format(DateTimeFormatter
				.ofPattern("dd-MMM-yyyy"));
		System.out.println("\n1.2 Current Date/time in dd-MMM-yyyy format is = " 
				+ formattedDate2);


		// 1.3 format LocalDateTime to dd-MM-yyyy HH:mm
		String formattedDate3 = localDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm"));
		System.out.println("\n1.3 Current Date/time in dd-MM-yyyy HH:mm format is = " 
				+ formattedDate3);


		// 1.4 format LocalDateTime to dd-MM-yyyy HH:mm:ss
		String formattedDate4 = localDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm:ss"));
		System.out.println("\n1.4 Current Date/time in dd-MM-yyyy HH:mm:ss format is = " 
				+ formattedDate4);


		// 1.5 format LocalDateTime to dd-MM-yyyy HH:mm:ss.nnn
		String formattedDate5 = localDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm:ss.nnn"));
		System.out.println("\n1.5 Current Date/time in dd-MM-yyyy HH:mm:ss.nnn format is = "
				+ formattedDate5);
	}
}

Output:

1. Current Date/time in ISO_LOCAL_DATE_TIME format is = 2022-06-23T19:36:59.155166500

1.1 Current Date/time in dd.MM.yyyy format is = 23.06.2022

1.2 Current Date/time in dd-MMM-yyyy format is = 23-Jun-2022

1.3 Current Date/time in dd-MM-yyyy HH:mm format is = 23-06-2022 19:36

1.4 Current Date/time in dd-MM-yyyy HH:mm:ss format is = 23-06-2022 19:36:59

1.5 Current Date/time in dd-MM-yyyy HH:mm:ss.nnn format is = 23-06-2022 19:36:59.155166500

3.9 Altering Day/Month/Year & Nano/Second/Minute/Hour fields with LocalDateTime:

  • Altering Day, Month, Year, Hour, Minute, Second and Nano-second field/part of LocalDateTime is possible with the help below methods,
    • withDayOfMonth() – This method alters day-of-month part/field of the invoking LocalDateTime
    • withMonth() – This method alters month-of-year part/field of the invoking LocalDateTime
    • withYear() – This method alters year part/field of the invoking LocalDateTime
    • withHour() – This method alters hour part/field of the invoking LocalDateTime
    • withMinute() – This method alters minute part/field of the invoking LocalDateTime
    • withSecond() – This method alters second part/field of the invoking LocalDateTime
    • withNano() -This method alters nano-second part/field of the invoking LocalDateTime

AlterLocalDateTime.java

package in.bench.resources.localdatetime.sorting;

import java.time.LocalDateTime;

public class AlterLocalDateTime {

	public static void main(String[] args) {

		// 1. get current system Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("1. Current Date/time in ISO_LOCAL_DATE_TIME format is = " 
				+ localDateTime);


		// 1.1 alter day part to current system Date/time
		LocalDateTime dateAltered = localDateTime.withDayOfMonth(27);
		System.out.println("\n1.1 Day (27) altered in current system Date/time is = " 
				+ dateAltered);


		// 1.2 alter month part to current system Date/time
		LocalDateTime monthAltered = localDateTime.withMonth(7);
		System.out.println("\n1.2 Month (7) altered in current system Date/time is = " 
				+ monthAltered);


		// 1.3 alter year part to current system Date/time
		LocalDateTime yearAltered = localDateTime.withYear(2023);
		System.out.println("\n1.3 Year (2023) altered in current system Date/time is = " 
				+ yearAltered);


		// 1.4 alter hour part to current system Date/time
		LocalDateTime hourAltered = localDateTime.withHour(5);
		System.out.println("\n1.4 Hour (5) altered in current system Date/time is = " 
				+ hourAltered);


		// 1.5 alter minute part to current system Date/time
		LocalDateTime minuteAltered = localDateTime.withMinute(19);
		System.out.println("\n1.5 Minute (19) altered in current system Date/time is = " 
				+ minuteAltered);


		// 1.6 alter second part to current system Date/time
		LocalDateTime secondAltered = localDateTime.withSecond(47);
		System.out.println("\n1.6 Second (47) altered in current system Date/time is = " 
				+ secondAltered);


		// 1.7 alter nano part to current system Date/time
		LocalDateTime nanoAltered = localDateTime.withNano(125);
		System.out.println("\n1.7 Nano Second (125) altered in current system Date/time is = " 
				+ nanoAltered);
	}
}

Output:

1. Current Date/time in ISO_LOCAL_DATE_TIME format is = 2022-06-25T16:40:47.688193800

1.1 Day (27) altered in current system Date/time is = 2022-06-27T16:40:47.688193800

1.2 Month (7) altered in current system Date/time is = 2022-07-25T16:40:47.688193800

1.3 Year (2023) altered in current system Date/time is = 2023-06-25T16:40:47.688193800

1.4 Hour (5) altered in current system Date/time is = 2022-06-25T05:40:47.688193800

1.5 Minute (19) altered in current system Date/time is = 2022-06-25T16:19:47.688193800

1.6 Second (47) altered in current system Date/time is = 2022-06-25T16:40:47.688193800

1.7 Nano Second (125) altered in current system Date/time is = 2022-06-25T16:40:47.000000125

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – ZonedDateTime with method details and examples
Java 8 - LocalTime with method details and examples