Javaexercise.com

Get Local DateTime With Altered Seconds Of Time In Java 8

While working with the java 8 date-time API, you may want to update the second field of the date-time object only without affecting the rest of the date-time fields such as month, hours, etc.

To alter or update the second field of the date-time object in java, you can use the withSecond() method of the LocalDateTime class of Java 8.

This method returns a copy of the date-time object after altering the second field.

For example, you have a date-time object. Let's say "2022-03-17T12:40:53.291844001" and you want to increment second to 55, then the resulted date-time object will be "2022-03-17T12:40:55.291844001".

Let's understand with the running examples.

Change Second of date-time in Java

To change or update the seconds of date-time, here, we used the withSecond() method.

We first, used the now() method to get the current date-time and then update the seconds. See the code and its output.

/* 
 *  Code example to get local date time with the specified seconds in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);

		// Get new datetime with altered seconds
		LocalDateTime newDate = date.withSecond(55);

		// Display new date
		System.out.println("New date : "+newDate);

	}
}

Output:

Date : 2022-03-17T12:40:53.291844001
New date : 2022-03-17T12:40:55.291844001
 

Now,  let's have a look at the method signature:

public LocalDateTime withSecond(int second)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of the local date-time object after altering the specified number of seconds, not null.

Parameters: It takes a single primitive int type value from 0 to 59.

Exceptions: It throws a DateTimeException if the result exceeds the supported(either MIN or MAX) date range.

Version: Since 1.8

Let's see some more examples:

It throws DateTimeException if the passing argument is not in the range. For example, if you pass second other than the range(0-59) then you will get an error.

/* 
 *  Code example to get local date time with the specified seconds in Java
 */
import java.time.LocalDateTime;
public class JExercise {
	public static void main(String[] args) {		

		// Current date and time
		LocalDateTime date = LocalDateTime.now();

		// Displaying date and time
		System.out.println("Date : "+date);

		// Get new datetime with altered seconds
		LocalDateTime newDate = date.withSecond(75); // invalid range

		// Display new date
		System.out.println("New date : "+newDate);

	}
}

Output:

Date : 2022-03-17T12:43:01.089795301
Exception in thread "main" java.time.DateTimeException: Invalid value for SecondOfMinute (valid values 0 - 59): 75

 

Single Line Solution - Final Shot

Use this code to get results in a single line of code. If you are a beginner, skip this code.

LocalDateTime.now().withSecond(10);

This code will return a local date-time object after altering the seconds of the current date-time. You just replace the value with your input and get the result instantly.