gpt4 book ai didi

java解析日期时间(以毫秒为单位)得到解析错误

转载 作者:行者123 更新时间:2023-12-01 22:03:38 26 4
gpt4 key购买 nike

我有一个字符串,在其中查找以毫秒为单位的日期时间,如下所示:

                     Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int millis = now.get(Calendar.MILLISECOND);

String monthup = String.valueOf(month);
String dayup = String.valueOf(day);
String hourup = String.valueOf(hour);
String minuteup = String.valueOf(minute);
String secondup = String.valueOf(second);
String millisup = String.valueOf(millis);

if(monthup.length()==1){monthup="0"+monthup;}
if(dayup.length()==1){dayup="0"+dayup;}
if(hourup.length()==1){hourup="0"+hourup;}
if(minuteup.length()==1){minuteup="0"+minuteup;}
if(secondup.length()==1){secondup="0"+secondup;}
if(millisup.length()==1){millisup="0"+millisup;}
if(millisup.length()==2){secondup="00"+millisup;}


String timewithmilsec = year+ monthup + dayup+ hourup+ minuteup+ secondup+ millisup;
System.out.println(timewithmilsec);

我得到一个值:20151020115216690,这显然是正确的。

我想将其解析为java日期格式。我所做的如下:

try{
DateFormat formatter = new SimpleDateFormat("yyyyMMdHHmmssaaa");
Date date = formatter.parse(timewithmilsec);
System.out.println(date);
}
catch(Exception e){
System.out.println(e);
}

我收到如下错误:

java.text.ParseException:无法解析的日期:“20151020115247995”

最佳答案

您的格式中只有一个 d,但根据 JavaDocs 将日期填充为两个字符。 ...

a   Am/pm marker    Text    PM

这不是毫秒占位符,我认为你的意思是SSS

例如...

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int millis = now.get(Calendar.MILLISECOND);

String monthup = String.valueOf(month);
String dayup = String.valueOf(day);
String hourup = String.valueOf(hour);
String minuteup = String.valueOf(minute);
String secondup = String.valueOf(second);
String millisup = String.valueOf(millis);

if (monthup.length() == 1) {
monthup = "0" + monthup;
}
if (dayup.length() == 1) {
dayup = "0" + dayup;
}
if (hourup.length() == 1) {
hourup = "0" + hourup;
}
if (minuteup.length() == 1) {
minuteup = "0" + minuteup;
}
if (secondup.length() == 1) {
secondup = "0" + secondup;
}
if (millisup.length() == 1) {
millisup = "0" + millisup;
}
if (millisup.length() == 2) {
secondup = "00" + millisup;
}

String timewithmilsec = year + monthup + dayup + hourup + minuteup + secondup + millisup;
System.out.println(timewithmilsec);

try {
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = formatter.parse(timewithmilsec);
System.out.println(date);
} catch (Exception e) {
System.out.println(e);
}

这对我来说打印

20151020173034124
Tue Oct 20 17:30:34 EST 2015

说到这里,让我向您介绍一下 String.format,它可以减少所有 intString 的转换并将代码填充到...

String timewithmilsec = String.format("%04d%02d%02d%02d%02d%02d%03d", year, month, day, hour, minute, second, millis);

I am getting Tue Oct 20 12:04:08 IST 2015 but interestingly I did not see any millisecond here

Date#toString 默认情况下不包含毫秒,您需要提供可以包含毫秒的 DateFormat

如果我将最后一个 System.out.println 替换为 System.out.println(new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS").format(date )); 它打印类似的内容

20 Oct 2015 17:37:14.856

(对于值20151020173714856)

关于java解析日期时间(以毫秒为单位)得到解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com