0

I am getting Parse exception while converting String to date. Here s my code:

String str_date = commonAttrMap.get("updateDate").toString();//2013-05-16 09:35:31.0
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
date = formatter.parse(str_date);
System.out.println(date);
productDetailForSolr.setUpdateDate(date);

Can someone let me know how to parse this.

Mayank_Thapliyal
  • 351
  • 2
  • 7
  • 16

6 Answers6

3

You won't face any ParseException for your String (2013-05-16 09:35:31.0) as it's completely valid.

date = formatter.parse("2013-05-16 09:35:31.0");
   //Assign date to the date Not format

It returns the date produced by the given String still you will get default format while you print date.To apply your format you have to use format method.

formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
System.out.println(formatter.format(date));//date after parsing

You wil only get Unparseable date: "2013-05-10 09:35:31.0" if you are trying to parse String with different date format for example yyyy/MM/dd.

akash
  • 22,664
  • 11
  • 59
  • 87
1

If that 0 at the end means milliseconds, try:

formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss.S");

For the complete reference: have a look at the SimpleDateFormat javadoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

morgano
  • 17,210
  • 10
  • 45
  • 56
1

Make sure that you are using java.util.Date instead of java.sql.Date. The following code is working in both jre6 and jre7.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParseDate {

public static void main(String[] args) {

    String str_date = "2013-05-16 09:35:31.0";
    DateFormat formatter;
    Date date;
    try {
    formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
        date = formatter.parse(str_date);
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

}
}

You can use the following format also, if you want milliseconds to be parsed,

formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss.S");

This is the output I'm getting,

Thu May 16 09:35:31 IST 2013

Rajaah
  • 51
  • 1
  • 2
  • 11
0

When reading the javadoc for SimpleDateFormat and looking at your provided timestamp I notice you are also providing milliseconds.

So you get a dateformat string as follows:

formatter = new SimpleDateFormaT("yyyy-MM-d HH:mm:ss.S");
Remcoder
  • 215
  • 2
  • 12
0

Since your date is like "2013-05-16 09:35:31.0", So you need to parse it using the pattern "yyyy-MM-d HH:mm:ss.S" (here S is Milliseconds).

this is what you have to do :

formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss.S");

See here for all Date or Time Component patterns of Date formatter in Java.

earthmover
  • 4,395
  • 10
  • 43
  • 74
0

I ran this code in Eclipse.. it is running absolutely fine!!

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class ParseDate {

    public static void main(String[] args) throws ParseException {
        String str_date = "2013-05-16 09:35:31.0";
        DateFormat formatter;
        Date date;
        formatter = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
        date = formatter.parse(str_date);
        System.out.println(date);
    }

}

The output is : Thu May 16 09:35:31 ICT 2013

I am using Java jre7

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68