相关疑难解决方法(0)

解析LocalDateTime时无法从TemporalAccessor获取LocalDateTime(Java 8)

我只是想在Java 8中将日期字符串转换为DateTime对象.运行以下行:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '20140218' could not be parsed: 
Unable to obtain LocalDateTime from TemporalAccessor: 
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Run Code Online (Sandbox Code Playgroud)

语法与此处的建议相同,但我遇到了一个例外.我在用JDK-8u25.

java datetime datetime-format java-8

122
推荐指数
8
解决办法
10万
查看次数

将Java字符串转换为时间,而不是日期

我想将变量字符串转换为Time类型变量,而不是使用Java的Date.字符串看起来像这样17:40

我尝试使用下面的代码,但这个实例是一个日期类型变量而不是时间

String fajr_prayertime  =       prayerTimes.get(0);
DateFormat formatter = new SimpleDateFormat("HH:mm");
fajr_begins = (Date)formatter.parse(fajr_prayertime);
System.out.println(" fajr time " + fajr_begins);
Run Code Online (Sandbox Code Playgroud)

但Netbean抱怨我应该插入一个例外,如下所示;

DateFormat formatter = new SimpleDateFormat("HH:mm");
try {
fajr_begins = (Date)formatter.parse(fajr_prayertime);
} catch (ParseException ex) {
Logger.getLogger(JavaFXApplication4.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" fajr time " + fajr_begins);
Run Code Online (Sandbox Code Playgroud)

知道如何从上面的字符串中获取时间.

java string time date

21
推荐指数
5
解决办法
10万
查看次数

标签 统计

java ×2

date ×1

datetime ×1

datetime-format ×1

java-8 ×1

string ×1

time ×1