使用postgreSQL的Mybatis日期对象问题

Date object issue Mybatis using postgreSQL

其实我不清楚这个问题是Java,JSP还是Mybatis.

目前面临的问题如下:

前端
Java JSP Struts 2 Spring

后端
PostgreSQL 9.3.XX

问题
在 PostgreSQL 中

birthday date;

在jsp页面代码写成

<sj:datepicker name="birthday" id="birthday"></sj:datepicker>

在Java文件中使用mybatis生成器生成如下代码

private Date birthday;

在 Mybatis 文件中

insert into "TABLE" (birthday) values (#{birthday, jdbcType=DATE});

从 jsp 页面插入值时 [1988/01/13] 然后在 java 控制台中获取

[6 月 11 日星期六 00:00:00 JST 18] 值。这是错误的。

如果我将 private Date birthday 更改为 private String birthday 那么控制台没有任何问题,但在数据库中插入值会生成错误.

Error Code :42804 
birthday is a date trying to insert character varying.

我尝试了不同的方法,但仍然没有找到答案。

如何在日期对象(而不是字符串)中获取 YYYY/MM/DD 格式?

您应该在将日期存储到数据库之前解析字符串 属性 中的日期,因为它映射到 Date 对象。

您可以使用客户端使用的不同语言环境来解析它。当前语言环境在操作上下文中可用。

Locale locale = ActionContext.getContext().getLocale();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", locale);
try {
  birthday = sdf.parse(birthdayString);
} catch (ParseException e) {
  e.printStackTrace();
}

尝试将 UI 字符串格式转换为数据库已知 (yyyy-MM-dd) 格式并插入数据库。

创建一些转换为字符串格式的通用实用程序。

SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
Date date = null;
try {
    date = formatter.parse(dateVal);
} catch (ParseException e) {
    e.printStackTrace();
}

Struts2 日期转换

对于日期,Struts2 uses the SHORT format for the Locale associated 与当前请求

这意味着如果您使用的是 Indian Locale,其格式为 dd/MM/yy,因此您可以安全地在 JSP 中输入 13/01/1988 并将其成功转换为 java.util.Date 动作中的对象。

如果您使用的是美国语言环境,其格式为 MM/dd/yy,您需要插入 01/13/1988 否则它将不起作用。


ISO8601 和 RFC3339

为了处理这类问题,多年前国际标准组织制定了ISO 8601标准:

ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date and time-related data. It was issued by the International Organization for Standardization (ISO) and was first published in 1988. The purpose of this standard is to provide an unambiguous and well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times, particularly when data are transferred between countries with different conventions for writing numeric dates and times.

ISO 8601 日期格式是 yyyy-MM-dd,您正在使用的格式。

已在 HTML5 生态系统(日期选择器等)中选择并采用了 ISO 8601 的特定配置文件,并在 RFC 3339 中进行了描述。
它在完整表示上略有不同,但仅日期格式是相同的 (yyyy-MM-dd)。

Struts2-jQuery-插件的 <sj:datepicker> 标签日期格式应该已经默认为 yy-MM-dd, IIRC.


完成任务

自动转换以这种格式输入的日期 has been recently introduced 并且在 Struts 2.5(测试版)中可用。应该也会在下个版本发布(2.3.25+).

否则,您需要create a converter像下面这样:

public class RFC3339Converter extends StrutsTypeConverter{

    @Override
    public String convertToString(Map context, Object o) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(o);
    }

    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         try {
             return (values[0].length()==0) ? null : (Date) sdf.parse(values[0]);
         } catch (ParseException e) {
             return values[0];
         }
    }
}

你知道吗……?

浏览器(可能)具有本机日期选择器,您可以使用它优雅地降级到 javascript 日期选择器,as described in this answer