Coder
Coder

Reputation: 79

Date change issue in struts2

When i selecting the date format from date picker it showing in "dd/MM/yyyy" format. Passing the date into java class it become "MM/dd/yyyy". Due to this reason i can't pass the day above 12 ,it showing input error.

Example: 11/08/2014 in jsp the same date showing in
java class is Sat Nov 08 00:00:00 IST 2014

Upvotes: 0

Views: 1032

Answers (2)

Andrea Ligios
Andrea Ligios

Reputation: 50203

Read Date conversion in Struts2, then set a Locale to Struts2 in struts.xml in the following way:

<constant name="struts.locale" value="it_IT"  />

with your desired Locale instead of it_IT, if you are not building a multi-language (i18n) web application.

Also consider using HTML5 native <input type="date" />, with a fallback (in this case with Modernizr) to jQuery for browsers not supporting that feature:

<script>
    // If not native HTML5 support, fallback to jQuery datePicker
    $(function(){
        if (!Modernizr.inputtypes.date) {
            $('input[type=date]').datepicker({
                // Consistent format with the HTML5 picker
                dateFormat: 'yy-mm-dd'
                },
                // Localization
                $.datepicker.regional['it']
            );
        }
    });
</script>

Upvotes: 0

sravtej
sravtej

Reputation: 63

You can use DateFormat to convert the date in the desired format.

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
String currDate = dateFormat.format(cal.getTime());

Upvotes: 1

Related Questions