Pramod Tiwari
Pramod Tiwari

Reputation: 297

Date object issue Mybatis using postgreSQL

Actually i am not clear this issue is Java ,JSP or Mybatis .

Currently facing issue is as follows :

Front End
Java JSP Struts 2 Spring

Back End
PostgreSQL 9.3.XX

Issue
In PostgreSQL

birthday date;

In jsp Page code written

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

In Java File using mybatis generator its generated following code

private Date birthday;

In Mybatis File

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

When inserting value from jsp page [1988/01/13] then in java console getting

[Sat Jun 11 00:00:00 JST 18] value. This is wrong.

If I change private Date birthday to private String birthday then console don't have any issue but inserting value in database generating error.

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

I've tried different ways, but still not found an answer.

How can I get in YYYY/MM/DD Format in Date object (not in String) ?

Upvotes: 1

Views: 2434

Answers (3)

Andrea Ligios
Andrea Ligios

Reputation: 50281

Struts2 Date conversion

For dates, Struts2 uses the SHORT format for the Locale associated with the current request.

This means that if you are using Indian Locale, which format is dd/MM/yy, hence you can safely enter 13/01/1988 in the JSP and get it succesfully converted into a java.util.Date object in the action.

If you instead are using an American Locale, which format is MM/dd/yy, you need to insert 01/13/1988 or it won't work.


ISO8601 and RFC3339

To handle this kind of problems, many years ago the International Standard Organization created the ISO 8601 standard:

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 Date format is yyyy-MM-dd, the one you're using.

A specific profile of ISO 8601 has been chosen and adopted in the HTML5 ecosystem (Date picker, etc), and is described in RFC 3339.
It slighty differs in the full representation, but the Date-only format is identical (yyyy-MM-dd).

Struts2-jQuery-plugin's <sj:datepicker> tag Date format should already be defaulted to yy-MM-dd, IIRC.


Getting things done

The automatic conversion of dates entered in this format has been recently introduced and is available in Struts 2.5 (beta). It should also be released in the next release (2.3.25+).

Otherwise, you need to create a converter like the following:

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];
         }
    }
}

Did you know... ?

Browsers (may) have native datepickers, that you could use with a graceful degradation toward javascript datepickers, as described in this answer.

Upvotes: 1

Vinoth Krishnan
Vinoth Krishnan

Reputation: 2949

Try converting the UI String format to DB known(yyyy-MM-dd) format and insert into the database.

Create some common utility convert to String format.

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

Upvotes: 0

Roman C
Roman C

Reputation: 1

You should parse the date from the string property before you store it to the database because it's mapped to the Date object.

You can parse it by using different locales used on the client side. The current locale is available in the action context.

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

Upvotes: 0

Related Questions