0

I have a joda DateTime value stored in mysql as String.

But when I try to get this string from mysql table, its format changes and when I try to convert this string into DateTime, it throws:-

IllegalArgumentException: Invalid format: "2012-10-04 00:00:00.0" is malformed at "  00:00:00.0"

In the Mysql table I can see the values as"2012-10-04 00:00:00", but when I get this time from mysql it is printing as "2012-10-04 00:00:00.0".

Please help me.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
vikas27
  • 563
  • 5
  • 14
  • 36

5 Answers5

2
   `String dob="your date String";
    //2013-jan-12 == 2013-01-12
    String dobis=null;
    final DateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
    final Calendar c = Calendar.getInstance();
    try {
        if(dob!=null && !dob.isEmpty() && dob != "")
        {
        c.setTime(df.parse(dob));
        int month=c.get(Calendar.MONTH);
        month=month+1;
        dobis=c.get(Calendar.YEAR)+"-"+month+"-"+c.get(Calendar.DAY_OF_MONTH);
        }

    } `
Suresh U
  • 463
  • 3
  • 14
2

Try this, Just pass a string to this method and get the value of date

public String DateToDateFormat(String dateandTime) {
    final Calendar calendar = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
    try {
        calendar.setTime(df.parse(dateandTime));
    } catch (java.text.ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    int yearofsendingmsg = calendar.get(Calendar.YEAR);
    // int monthofsendingmsg = calendar.get(Calendar.MONTH) + 1;
    int dayofsendingmsg = calendar.get(Calendar.DAY_OF_MONTH);
    java.util.Date d = new java.util.Date(calendar.getTimeInMillis());
    String monthofsendingmsg = new SimpleDateFormat("MMM").format(d);
    String date = dayofsendingmsg + " " + monthofsendingmsg + " "
            + yearofsendingmsg;
    return date;
}
Kartheek Sarabu
  • 3,886
  • 8
  • 33
  • 66
0

Try this

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
Date date = df.parse("2012-10-04 00:00:00.0");
usman allam
  • 274
  • 1
  • 5
  • He is not hard coding the input. It is fetched from a database which is not is the expected format and hence getting that exception. Your code will fail for the input mentioned in the question. – Ankur Shanbhag Aug 09 '13 at 10:35
0

The right format is:

 "yyyy-MM-dd hh:mm:ss.S"
Azad
  • 5,047
  • 20
  • 38
0
String newF="yyyy-MM-dd hh:mm:ss";
String oldF="yyyy-MM-dd hh:mm:ss.S";
SimpleDateFormat sdf1=new SimpleDateFormat(oldF);
SimpleDateFormat sdf2=new SimpleDateFormat(newF);
String txt1=ClassWhichContainsTheVariableYouGotTheResultInTo.txtVariable.getText();

String date1=sdf2.format(sdf1.parse(txt1));
java.util.Date javaDate1=(Date)sdf2.parse(date1); //this converts it as java.util.Date
java.sql.Date sqlDate1=new java.sql.Date(theDate1.getTime()); //this converts it to java.sql.Date (if needed)

This way you can parse the String variable to a java.Date of your choice. The ClassWhichContainsTheVariableYouGotTheResultInTo means the class name of (for eg. Main.java) in which you are returning the sql value in, and the txtVariable.getText() is a reference to a variable such as textfield or label, or whatever you returned the value into. After the conversion is done, you can then just use either javaDate1 or sqlDate1, depending on which date type you want.

ghoulfolk
  • 326
  • 7
  • 17