相关疑难解决方法(0)

Java 8可选时间部分无法正常工作

我正在尝试为当前实现此选项的可选时间部分创建日期时间格式

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.text.ParseException;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(Ideone.getDate("2017-07-01T00:00:00.0Z"));
        System.out.println(Ideone.getDate("2017-07-01T00:00:00.00Z"));
        System.out.println(Ideone.getDate("2017-07-01T00:00:00.000Z"));
    }

    public static LocalDateTime getDate(String date) {
        try {

            DateTimeFormatter formatter2 =
                DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]'Z'");
            LocalDateTime ldt = LocalDateTime.parse(date, formatter2);


            return ldt;

        } catch (DateTimeParseException ex) {
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并有输出

空值

2017-07-01T00:00

2017-07-01T00:00

现在我的问题是,为什么有1个时间分数的日期不起作用,它正在处理2个和3个分数?它应该招待1,2和3分吗?或只有3个分数?

提前致谢

java datetime-format java-8 java-time

11
推荐指数
1
解决办法
365
查看次数

标签 统计

datetime-format ×1

java ×1

java-8 ×1

java-time ×1