java.time.OffsetDateTime.parse(CharSequence text,DateTimeFormatter formatter)
方法使用特定格式化程序从文本字符串中获取OffsetDateTime
的实例。
声明
以下是java.time.OffsetDateTime.parse(CharSequence text,DateTimeFormatter formatter)
方法的声明。
public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter)
参数
text
- 要解析的文本,例如:"2017-02-03T10:15:30 + 01:00"
,而不是null
。formatter
- 要使用的格式化程序,而不是null
。
返回值
本地日期,不为null
。
异常
DateTimeParseException
- 如果无法解析文本。
例
以下示例显示了java.time.OffsetDateTime.parse(CharSequence text,DateTimeFormatter formatter)
方法的用法。
package com.zaixian;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class OffsetDateTimeDemo {
public static void main(String[] args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String date = "2017-12-03T10:15:30+01:00";
OffsetDateTime date1 = OffsetDateTime.parse(date, dateTimeFormatter);
System.out.println(date1);
}
}
编译并运行上面的程序,这将产生以下结果 -
2017-12-03T10:15:30+01:00