可以使用format()Java中LocalTime类中的方法,使用指定的格式化程序对LocalTime进行格式化。此方法需要单个参数,即要格式化的LocalTime对象,并使用指定的格式化程序返回格式化的LocalTime。
演示此过程的程序如下:
示例import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalTime lt = LocalTime.parse("14:30:47");
System.out.println("LocalTime是: " + lt);
DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
System.out.println("格式化的LocalTime为: " + dtf.format(lt));
}
}
输出LocalTime是: 14:30:47
格式化的LocalTime为: 14:30:47
现在让我们了解上面的程序。
首先显示LocalTime。然后,使用指定的格式化程序使用该format()方法对LocalTime进行格式化,并显示格式化的LocalTime。演示此代码段如下所示:LocalTime lt = LocalTime.parse("14:30:47");
System.out.println("LocalTime是: " + lt);
DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
System.out.println("格式化的LocalTime为: " + dtf.format(lt));