Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SimpleDataFormat java解析日期_Java_Date_Parsing_Simpledateformat - Fatal编程技术网

使用SimpleDataFormat java解析日期

使用SimpleDataFormat java解析日期,java,date,parsing,simpledateformat,Java,Date,Parsing,Simpledateformat,我想分析到日期的字符串,但获取的日期不正确。我的代码如下: SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S a"); date1 = df.parse("17-DEC-19 05.40.39.364000000 PM"); 但日期1是:2019年12月21日星期六22:47:19 我需要注明日期:*Dec 17 17:40:39 2019年第一季度SimpleDataFor

我想分析到日期的字符串,但获取的日期不正确。我的代码如下:

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S a");
date1 = df.parse("17-DEC-19 05.40.39.364000000 PM");
但日期1是:2019年12月21日星期六22:47:19


我需要注明日期:*Dec 17 17:40:39 2019年第一季度
SimpleDataFormat
的精度不超过毫秒(
.SSS

输出:

Tue Dec 17 17:40:39 GMT 2019
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args)  {
        DateTimeFormatter df = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // For case-insensitive (e.g. AM/am) parsing
                .appendPattern("dd-MMM-yy hh.mm.ss.n a")
                .toFormatter(Locale.ENGLISH);
        
        LocalDateTime ldt = LocalDateTime.parse("17-DEC-19 05.40.39.364000000 PM", df);
        System.out.println(ldt);
    }
}
2019-12-17T17:40:39.364
请注意,
java.util
date-time API及其格式化API,
SimpleDataFormat
已经过时且容易出错。建议完全停止使用,并切换到*

使用现代日期时间API:

Tue Dec 17 17:40:39 GMT 2019
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args)  {
        DateTimeFormatter df = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // For case-insensitive (e.g. AM/am) parsing
                .appendPattern("dd-MMM-yy hh.mm.ss.n a")
                .toFormatter(Locale.ENGLISH);
        
        LocalDateTime ldt = LocalDateTime.parse("17-DEC-19 05.40.39.364000000 PM", df);
        System.out.println(ldt);
    }
}
2019-12-17T17:40:39.364
输出:

Tue Dec 17 17:40:39 GMT 2019
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args)  {
        DateTimeFormatter df = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // For case-insensitive (e.g. AM/am) parsing
                .appendPattern("dd-MMM-yy hh.mm.ss.n a")
                .toFormatter(Locale.ENGLISH);
        
        LocalDateTime ldt = LocalDateTime.parse("17-DEC-19 05.40.39.364000000 PM", df);
        System.out.println(ldt);
    }
}
2019-12-17T17:40:39.364
了解有关现代日期时间API的更多信息


*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。

这里有一个溢出,即您添加的毫秒被解释为364000秒或4天5小时6分40秒。这些将添加到解析的日期中。尝试添加
df.setLenient(false)
,您会得到一个错误。我建议您不要使用
SimpleDataFormat
Date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。而是使用
LocalDateTime
DateTimeFormatter
,两者都来自。