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.
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