0

I'm using datepair.js which can be found here http://jonthornton.github.io/Datepair.js/

I'm building a leave request calendar where the end user can select a particular date in one input field and a time in a separate input field. I'm looking for the best way to parse these two fields into a single date/time field for the db.

I have the following JSON

[startTime:12:30am, startDate:11/12/2015, endDate:11/12/2015]

and the following SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");

I know I can set the date by doing something like

new Date(sdf.parse(jsonObj.startDate))

But what is the best way to combine those dates?

Code Junkie
  • 7,602
  • 26
  • 79
  • 141
  • You can also take a look at [this](http://stackoverflow.com/questions/12838603/concat-two-strings-then-convert-to-date-in-java). – Sotirios Delimanolis Nov 09 '15 at 17:13
  • [This](http://stackoverflow.com/questions/30657440/combining-two-date-instance-to-create-date-time-using-joda) as well. – Sotirios Delimanolis Nov 09 '15 at 17:13
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 28 '18 at 22:14

1 Answers1

3

Ok, this is a bit of a kludge, but have you considered retrieving startDate and startTime as strings, concatenating them with a space (or something) in the middle, and then using a single SimpleDateFormat to parse the combined string? Something like this:

String datetime = jsonObj.startDate + " " + jsonObj.startTime;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mma");  //changed the format to your example input
Date d = sdf.parse(datetime);
dcsohl
  • 7,186
  • 1
  • 26
  • 44