1024programmer Java LocalTimewithSecond() method in Java, example

LocalTimewithSecond() method in Java, example

LocalTime withSecond() method in Java, examples

Original text: https://www . geesforgeks . org/local time-with second-method-in-Java-with-examples/

The withSecond() method of a LocalTime class is used to obtain a copy of this LocalTime with the seconds changed to as argument The seconds passed to this method. The remaining value of this local time will remain unchanged. This instance is immutable and is not affected by calls to this method.

Syntax:

public LocalTime withSecond(int second)

Parameters:This method accepts A single parameter seconds, representing the minutes and seconds set in the result, from 0 to 59.

Return Value: This method returns a Local Time Instance based on which the seconds are requested.

Exception:If the second value is invalid, this method throws an exceptionDateTimeException

The following program illustrates withSecond() method:

Program 1:

// Java program to demonstrate
// LocalTime.withSecond() method
import java.time.*;
public class GFG {
 public static void main(String[] args)
 {
 // create a LocalTime object
  LocalTime time
 = LocalTime.parse("19:34:50.63 ");
 // print time
 System .out.println("Old LocalTime: "
                                                                              span> time);
 // Get a new LocalDateTime with seconds 4
 LocalTime newtime = time.withSecond(4);
 // print result
 System.out.println("New LocalDateTime: "
                                                                    ">+ newtime);
 }
}

Output:

Old LocalTime: 19:34:50.630
New LocalDateTime: 19:34: 04.630

Procedure 2:

// Java program to demonstrate
// LocalTime.withSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
 {
 // create a LocalTime object
LocalTime time
   = LocalTime.parse("01:21:30.13");
 // print time
 System .out.println("Old LocalTime: "
                                                    /span>);
 // Get a new LocalDateTime with seconds 23
 LocalTime newtime = time.withSecond(23);
 // print result
 System.out.println("New LocalDateTime: "
                                                          n">newtime);
 }
}

Output:

Old LocalTime: 01:21:30.130
New LocalDateTime: 01:21: 23.130

References: https://docs . Oracle . com/javase/10/docs/API/Java/time /localtime . html # with second(int)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/localtimewithsecond-method-in-java-example/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索