【问题标题】:JCalendar - setting date correctly in Java using correct formatJCalendar - 使用正确的格式在 Java 中正确设置日期
【发布时间】:2013-01-09 15:23:06
【问题描述】:

我从这里使用JCalendarhttp://www.toedter.com/en/jcalendar/ 我使用 JDateChooser 在表单中设置日期并以yyyy-MM-dd 格式导入SqLite。当我尝试使用此代码从数据库中以相同格式yyyy-MM-dd 检索日期时:

Date date = rs.getDate(10);
            fieldClose.setDate(date); //displays 01-Jan-1970 - I want (2013-01-08), one   that is similar in database. 

我查看了stackoverflow,并且存在一些类似的帖子,但未能在格式yyyy-MM-dd 和考虑jCalender 和SQLite 方面显示建议,所以我不得不发布这个。任何想法如何实现类似于数据库的日期,以插入相同的格式和相同的类型。

我刚刚试过这个:

String date =rs.getString(10); 
                ((JTextField)fieldClose.getDateEditor().getUiComponent()).setText(date);

返回:

2013-05-22 // 出现红线,表示 jDatechooser 无法识别它

【问题讨论】:

  • 有两种方法可以设置 SimpleDateFormat 或 setLocale,这两种方法都在这个 Java Swing JCalendar 中实现

标签: java sql database sqlite date


【解决方案1】:

使用 SimpleDateFormat,试试这个例子

   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   Date date=null;
   try{
        date=sdf.parse(rs.getString(10));
   } catch (ParseException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   fieldClose.setDate(date);

【讨论】:

    【解决方案2】:
    1. 您可以在 java 中使用 SimpleDateFormat
    2. 或在 Mysql SELECT 查询中使用 DATE_FORMAT('ColumnName', "Date Pattern")。

    【讨论】:

      【解决方案3】:

      您可以使用 String.format。让我帮你写代码

      Date o = jDateChooser1.getDate();   //Import java.util not java.sql
      String x = String.format("%1$ty-%1$tm-%1$td",o);   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 2013-09-18
        相关资源
        最近更新 更多