java解析日期时间,以毫秒为单位进入解析错误

时间:2015-10-20 06:27:35

标签: java date parsing

我有一个字符串,其中我发现日期时间为毫秒,如下所示:

                     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 Date format。 我所做的如下:

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: Unparseable date: "20151020115247995"

3 个答案:

答案 0 :(得分:1)

根据文档字母S响应毫秒,因此您的格式应如下new SimpleDateFormat("yyyyMMddHHmmssSSS");(您的格式中有一个d

答案 1 :(得分:1)

您的格式只有一个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);
  

我将于2015年12月20日星期二12:04:08,但有趣的是我没有看到任何毫秒

默认情况下,

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

答案 2 :(得分:1)

答案是日期格式中缺少“d”,其中@MadProgrammer描述。

此外,应重新考虑生成日期的字符串表示形式。您应该使用SimpleDateFormat.format()生成日期字符串,如下面的示例代码所示:

Calendar now = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String formattedDate = formatter.format(now.getTime());
System.out.println("Formatted date: " + formattedDate);

输出将采用您请求的格式。

Formatted date: 20151020094934279