0
import java.text.SimpleDateFormat;
import java.util.Date;


public class StringtoDate {
    public static void main (String args[]){
        String inputDate = "04.11.2013 11:14:45 GMT 00:00";
        Date dat = convertStringToDate(inputDate);
    }
    public static Date convertStringToDate(String inDate){
    SimpleDateFormat sdf = new SimpleDateFormat("mm.dd.yyyy hh24:mm:ss");
        Date date = new Date();
         try {
             date = sdf.parse(inDate);

         } catch (Exception e) {
          e.printStackTrace();
        }
         return date;
    }
}

my input String is of type String ("04.11.2013 11:14:45 GMT 00:00"),
I m trying to convert to a type Date and which should be of foramt ("mm.dd.yyyy hh24:mi:ss") , and output of the DAte should look like ("3/7/2013 12:33:15.000000000 PM").
but i m getting exception ,

    java.text.ParseException: Unparseable date: "04.11.2013 11:14:45 GMT 00:00"
at StringtoDate.convertStringToDate(StringtoDate.java:14)
at StringtoDate.main(StringtoDate.java:8)

please help me to get the correct results.

Edited by ~s.o.s~: convert to discussion thread

6
Contributors
11
Replies
47
Views
3 Years
Discussion Span
Last Post by pooran.c
Featured Replies
  • The main issue is actually with your format string; for month, you should use capitalized letter M. Similarly, for 24-hour time going from 00 to 23, you need to use capital H. To get the time zone in the format you want, use the capital letter Z twice. "MM.dd.yyyy HH:mm:ss … Read More

  • 1
    IIM 163   3 Years Ago

    take a look at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and use proper date format. And for changing date format into another format SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSSSSSSSS a"); // 3/7/2013 12:33:15.000000000 PM System.out.println(sdf1.format(date)); Read More

  • You seeem to be confused about the Date class. A Date object represents an instant in time, and has methods to access it. It does NOT have a format. When you convert a Date to or from a String that's when a format is applied. Read More

2

The main issue is actually with your format string; for month, you should use capitalized letter M. Similarly, for 24-hour time going from 00 to 23, you need to use capital H. To get the time zone in the format you want, use the capital letter Z twice.

"MM.dd.yyyy HH:mm:ss ZZ"
0

But

sdf1.format(date)

would return Stringbuffer or string .
i need the format in DATE or timestamp

1

You seeem to be confused about the Date class.
A Date object represents an instant in time, and has methods to access it. It does NOT have a format. When you convert a Date to or from a String that's when a format is applied.

0

The main objective here is to pass my Date to Storedprocedure , where the stored procedure is mapped to Timestamp variable.

0

yeah i correct it, not the Date format . want it in Date object .

0

You have a String in a known format and you want to convert that to a Date object - yes?
If so your first code was the right approach. Did you fix the format specification as noted by Schol-R-LEA

0

if your main objective is to pass it to a stored procedure, pass it as is (meaning, as a string) and write the stored procedure in such a way that it does the conversion.
Much more efficient, as it eliminates the implicit conversion of a Java Date to a database date, and pushes the conversion to the database server, which typically is much better at such tasks (faster...) than the application server.

0

OK, thanks guys, tried using java.sql.timestamp , but still was not able to truncate the millisec from the timestamp object .
now changing the stored procedure .
if more doubts i ll start a new discussion about the stored procudure.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.