admin管理员组

文章数量:1026989

datetime parse java

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

parse(CharSequence text)

LocalDateTime类的parse()方法用于从作为参数传递的字符串(例如“ 2018-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("2018-12-30T19:34:50.63");

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : 2018-12-30T19:34:50.630

parse(CharSequence text, DateTimeFormatter formatter)

LocalDateTime类的parse()方法用于从使用特定格式器传递为参数的字符串(例如“ 2018-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("2018-12-30T19:34:50.63",

formatter);

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : 2018-12-30T19:34:50.630

datetime parse java

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

parse(CharSequence text)

LocalDateTime类的parse()方法用于从作为参数传递的字符串(例如“ 2018-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("2018-12-30T19:34:50.63");

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : 2018-12-30T19:34:50.630

parse(CharSequence text, DateTimeFormatter formatter)

LocalDateTime类的parse()方法用于从使用特定格式器传递为参数的字符串(例如“ 2018-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("2018-12-30T19:34:50.63",

formatter);

// print result

System.out.println("LocalDateTime : "

+ lt);

}

}

输出:

LocalDateTime : 2018-12-30T19:34:50.630

本文标签: datetime parse java