简体   繁体   English

JXDatePicker:无法解决的日期

[英]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 我正在使用java swing开发一个小应用程序,我JXDatePickerJXDatePicker我需要从JXDatePicker转换日期,所以我可以将它插入mysql DB这里是代码

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 ... 让我们从dateFormat.format将返回一个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 . 不需要dateFormat Once the date is parsed you can simply use getTime() to return the long value to be used in the constructor. 解析日期后,您只需使用getTime()返回要在构造函数中使用的long值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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