Java中的 ChronoLocalDateTime toString() 方法及示例
ChronoLocalDateTime类的toString()方法用于将此 ChronoLocalDateTime 转换为字符串。
句法:
String toString()
参数:此方法不接受任何参数。
返回值:此方法返回一个字符串,它是此 ChronoLocalDateTime 的字符串表示形式
下面的程序说明了 toString() 方法:
方案一:
// Java program to demonstrate
// ChronoLocalDateTime.toString() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime
.parse("2019-12-31T19:15:30");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// print result
System.out.println("String: "
+ time.toString());
}
}
输出:
ChronoLocalDateTime: 2019-12-31T19:15:30
String: 2019-12-31T19:15:30
方案二:
// Java program to demonstrate
// ChronoLocalDateTime.toString() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime.parse(
"2018-10-25T23:12:31.123");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// print result
System.out.println("String: "
+ time.toString());
}
}
输出:
ChronoLocalDateTime: 2018-10-25T23:12:31.123
String: 2018-10-25T23:12:31.123
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoLocalDateTime.html#toString–