Santhucool
Santhucool

Reputation: 1716

java parse date time with milliseconds into date getting parse error

I have a string in which I am finding the datetime with milliseconds as follows:

                     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);

I am getting a value: 20151020115216690 which is obviousely correct.

I want to parse it to java Date format. What I did is as follows:

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

I am getting an error as follows:

java.text.ParseException: Unparseable date: "20151020115247995"

Upvotes: 0

Views: 606

Answers (3)

Hasan
Hasan

Reputation: 21

The answer is the missing "d" in the date format, where @MadProgrammer depicts.

In addition, the generation of the string representation of the date should be reconsidered. You should use SimpleDateFormat.format() to generate date string as in the sample code below:

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

And the output will be in a format that you requested.

Formatted date: 20151020094934279

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347314

You have only one d in your format, but are padding the day to two characters, also, according to the JavaDocs...

a   Am/pm marker    Text    PM

which isn't a millisecond place holder, I think you mean SSS

For example...

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);
}

Which for me prints

20151020173034124
Tue Oct 20 17:30:34 EST 2015

And while I'm at it, let me introduce you to String.format, which can reduce all you int to String conversion and padding code down to...

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 won't include the milliseconds by default, you will need to supply a DateFormat which can.

If I replace the last System.out.println with System.out.println(new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS").format(date)); it prints something like

20 Oct 2015 17:37:14.856

(for the value 20151020173714856)

Upvotes: 1

wawek
wawek

Reputation: 1597

According to documentation letter S responds to milliseconds so your format should look like this new SimpleDateFormat("yyyyMMddHHmmssSSS"); (you have one d in your format).

Upvotes: 1

Related Questions