简体   繁体   中英

JXDatePicker:unpearsable date

I'm developing a small application with java swing and I'm stuck with the JXDatePicker I need to convert the date from the datepicker so I can insert it in mysql DB here is the code

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");

Date invoiceDate = formatDate.parse(jXDatePicker3.getDate().toString());
java.sql.Date sqlDate = new java.sql.Date(Long.parseLong(dateFormat.format(invoiceDate)));

and this is the error

java.text.ParseException: Unparseable date: "Thu Dec 19 00:00:00 GMT+01:00 2013"

Let's start with the fact that dateFormat.format will return a String ...

You could be trying something more like..

java.sql.Date sqlDate = new java.sql.Date(
    formatDate.parse(
        dateFormat.format(invoiceDate)).getTime());    

Instead, or may be even...

java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());    

There is no need for dateFormat . Once the date is parsed you can simply use getTime() to return the long value to be used in the constructor.

public class DateTest {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

        Date invoiceDate = formatDate.parse("Thu Dec 19 00:00:00 GMT+01:00 2013");
            //Once you have a date use .getTime() to get Long value.
        java.sql.Date sqlDate = new java.sql.Date(invoiceDate.getTime());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM