100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > datetime parse java_Java LocalDateTime parse()用法及代码示例

datetime parse java_Java LocalDateTime parse()用法及代码示例

时间:2021-02-15 16:01:56

相关推荐

datetime parse java_Java LocalDateTime parse()用法及代码示例

在LocalDateTime类中,根据传递给它的参数,有两种类型的parse()方法。

parse(CharSequence text)

LocalDateTime类的parse()方法用于从作为参数传递的字符串(例如“ -10-23T17:19:33”)中获取LocalDateTime的实例。该字符串必须具有有效的日期时间,并使用DateTimeFormatter.ISO_LOCAL_DATE_TIME进行解析。

用法:

public static LocalDateTime parse(CharSequence text)

参数:此方法仅接受一个参数文本,这是要在LocalDateTime中解析的文本。它不能为空。

返回值:此方法返回LocalDateTime,它是解析的本地日期时间。

异常:如果无法解析文本,则此方法将引发DateTimeParseException。

以下示例程序旨在说明parse()方法:

示例1:

// Java program to demonstrate

// LocalDateTime.parse() method

import java.time.*;

public class GFG {

public static void main(String[] args)

{

// create an LocalDateTime object

LocalDateTime lt

= LocalDateTime

.parse("-12-30T19:34:50.63");

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : -12-30T19:34:50.630

parse(CharSequence text, DateTimeFormatter formatter)

LocalDateTime类的parse()方法用于从使用特定格式器传递为参数的字符串(例如“ -10-23T17:19:33”)中获取LocalDateTime实例。日期时间使用特定格式器进行解析。

用法:

public static LocalDateTime parse(CharSequence text,

DateTimeFormatter formatter)

参数:此方法接受两个参数text(即要解析的文本)和formatter(要使用的格式化程序)。

返回值:此方法返回LocalDateTime,它是解析的本地日期时间。

异常:如果无法解析文本,则此方法将引发DateTimeParseException。

以下示例程序旨在说明parse()方法:

示例1:

// Java program to demonstrate

// LocalDateTime.parse() method

import java.time.*;

import java.time.format.*;

public class GFG {

public static void main(String[] args)

{

// create a formater

DateTimeFormatter formatter

= DateTimeFormatter.ISO_LOCAL_DATE_TIME;

// create an LocalDateTime object and

LocalDateTime lt

= LocalDateTime

.parse("-12-30T19:34:50.63",

formatter);

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : -12-30T19:34:50.630

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。