1

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) ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Pramod Tiwari
  • 298
  • 5
  • 16

3 Answers3

1

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

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();
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for reply.but issue is when i am inserting value in jsp page 1988/01/13 at that time in java console its written [Sat Jun 11 00:00:00 JST 18] because of in model object is Date. this issue is while using postgreSQL i tried in sql server also.dont have any issue. – Pramod Tiwari Mar 02 '16 at 11:42
  • In the console it should be the same format as above. – Roman C Mar 02 '16 at 12:22
  • No.in console output is Sat Jun 11 00:00:00 JST 18 while inserting from jsp 1988/01/13. – Pramod Tiwari Mar 02 '16 at 12:25
  • The default format is determined by the struts locale, but you can use fixed format `dysplayFormat="yy/mm/dd"` with `readonly="true"`. – Roman C Mar 02 '16 at 18:14
0

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();
}
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • Thanks for reply.but issue is when i am inserting value in jsp page 1988/01/13 at that time in java console its written [Sat Jun 11 00:00:00 JST 18]
    1988/01/13 - Sat Jun 11 00:00:00 JST 18 this is a wrong output.
    – Pramod Tiwari Mar 02 '16 at 11:49
  • Is your problem in front-end? – Vinoth Krishnan Mar 02 '16 at 11:54
  • i also dont know. In jsp page textbox inserting 1988/01/13 In java getting output Sat Jun 11 00:00:00 JST 18 .my variable type is Date birthday. – Pramod Tiwari Mar 02 '16 at 12:02
  • Try adding the date format attribute `displayFormat="mm/dd/yy"` in your datepicker tag. – Vinoth Krishnan Mar 02 '16 at 12:12
  • Actually i want to show dateformat in yyyy/mm/dd in jsp page and except from user also input in this format.but as u said i tried at that time output is Sun Jan 01 00:00:00 JST 1989 while inputting 01/13/88 or 01/13/1988. – Pramod Tiwari Mar 02 '16 at 12:20
  • FYI, These are all the valid date format `dd.mm.yy mm/dd/yy DD, d MM yy d M, yy dd-mm-yy` and see the [reference](https://code.google.com/archive/p/struts2-jquery/wikis/DatePickerTag.wiki#Datepicker_with_different_formats). Is there any chance of using normal `jquery datepicker` with simple textbox? – Vinoth Krishnan Mar 02 '16 at 12:34
  • i agree with you.but issue is while parsing date from jsp to Java not written properly.because jsp contain all data in string format.at that string data not getting parse correctly in java side using struts 2. – Pramod Tiwari Mar 02 '16 at 12:40
  • How about trying the [jQuery Datepicker](https://jqueryui.com/datepicker/)? Is there any constraints like only Struts datepicker? – Vinoth Krishnan Mar 02 '16 at 12:47
  • What about the second option you've tried? `private String birthday` – Vinoth Krishnan Mar 02 '16 at 12:54
  • Actuallly sj:datepicker internally using jQuery Datepicker.and i wrapped all control with sj.so currently not able to change. – Pramod Tiwari Mar 02 '16 at 12:58
  • by using second option its work properly .but issue is i need to modifed in insert and update query. i need to insert to_date(birthday,'yyyy/mm/dd').but i dont want to update in mapper file.because its issue for maintain.bcoz using mybatis generator. – Pramod Tiwari Mar 02 '16 at 13:00