0

I am using materialized datepicker in my form and want to store that in my bean class property(java.util.date), but its showing null.

$('.datepicker').pickadate
 ({ 
  //format: 'mm/dd/yyyy',
  selectMonths: true, // Creates a dropdown to control month 
  selectYears: 15, // Creates a dropdown of 15 years to control year
 // closeOnSelect: true
  format : 'yyyy-mm-dd',
   hiddenName: true
 });
<input type="date" id="dob" class='datepicker' ng-model="test.dateOfBirth" /> <label
        for="dob">Date of Birth </label>

here goes my Rest class:

@POST
@Path("/create")
@Consumes({ MediaType.APPLICATION_JSON})
@Produces({ "application/json" })
public String create(EmployeeBean emp) {
    System.out.println("in rest method...");
    System.out.println(emp.getSignum());
    System.out.println(emp.getDateOfBirth());





    System.out.println("Returned  here");
    return "{}";
}

I am getting other values but in date its showing null.

Ashish Raj
  • 170
  • 1
  • 1
  • 10

1 Answers1

0

The problem is that when you use the Materialize framework, they create the date as a string instead of the Date object. You need to take the string and convert it to a Date object if that's how you want to store it.

To do this in Java, you can use the DateFormat object first like this:

String strDate = "06/04/1992";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(strDate);

System.out.println(date);

// Output
Thu Jun 04 00:00:00 CDT 1992

Subsequently, this will now print using the default Date format and you will need to add a try/catch for the exception ParseException.

If you need to format the date back to a string, then you can do

String newDateObjectAsString = dateFormat.format(date);
System.out.println(newDateObjectAsString);

// Output
06/04/1992

The final code to test this would look like this

String strDate = "06/04/1992";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
    date = dateFormat.parse(strDate);
} catch (ParseException e) {
    e.printStackTrace();
}

String newDateObjectAsString = dateFormat.format(date);

System.out.println(date);
System.out.println(newDateObjectAsString);

Edit (Javascript version)

Does something like this work for you?

var parts ='04/03/2014'.split('/');
//please put attention to the month (parts[0]), Javascript counts months from 0:
// January - 0, February - 1, etc
var mydate = new Date(parts[2],parts[0]-1,parts[1]); 

I found it here : https://stackoverflow.com/a/22835394/6294139

Kurt King
  • 1,952
  • 2
  • 11
  • 12
  • can't it be done at jquery or js level?? Because parameter passed in rest method is bean class reference. I am populating my bean on basis of form details and cannot change dob type to String in bean class. Parameter passed in rest method is bean class reference. – Ashish Raj Jun 02 '17 at 06:36
  • I have edited my post to include a piece of code and a link to another article I found on stackoverflow. Also, is it not possible to use my solution in the EmployeeBean getDateOfBirth() method? – Kurt King Jun 03 '17 at 03:56