Koolstr
Koolstr

Reputation: 502

How To properly format Date string from DateTimePicker for parsing by SimpleDateFormat?

I am using the jQuery DateTimePicker to get a user-selected date and time (with timezone) from a JSP form I created. I'm new to Javascript so I may be making a mistake in my approach or code. So if I am, please point it out so I can learn.

The date/time jQuery selector shows and works as expected, however I need to send its data to a Java servlet I created, which then creates a new Parse Object in my Parse.com database with that date data. According to Parse's docs, the Date field stored for an object is as follows:

"The Date type contains a field iso which contains a UTC timestamp stored in ISO 8601 format with millisecond precision: YYYY-MM-DDTHH:MM:SS.MMMZ."

Essentially, I need to reformat the returned date from the form to match Parse's requirements, which I will do with SimpleDateFormat in my servlet. (I've been able to do that with a Date object in Java already, so an alternative solution would be to send the Date data as a Date object straight from the jsp. If that is possible, please tell me how.)

I matched up the formatting for SimpleDateFormat with the DatePicker format and TimePicker format so that SimpleDateFormat would be able to parse and reformat the Date string without a problem into a Date object in my servlet. (Curse whoever allowed using such a variety of formats when dealing with dates and times instead of using one universal format... T_T) (Maybe this is the part I did wrong? Anyone willing to double-check?) So I changed the format in my DateTimePicker to match up with SimpleDateFormat's, while retaining user readability:

<script>
$(function() {
    $("#timezone_datetimepicker").datetimepicker({
        dateFormat: 'mm-dd-yy', 
        timeFormat: 'HH:mm Z',
        onClose: function (dateText) {
            $(this).datetimepicker.formatTime('HH:mmZ', dateText);
            }
    });
});

The function at onClose is my attempt to match the format to SimpleDateFormat's with exact precision before it is sent to the servlet. I based it on the utility function mentioned in the TimePicker's examples:

Use the utility function to format your own time. $.datepicker.formatTime(format, time, options)

$('#utility_example_2').text(
$.datepicker.formatTime('HH:mm z', { hour: 14, minute: 36, timezone: '+2000' }, {})

);

Regardless of my numerous attempts and variations, this is the part where it fails and I need help with. When I try to parse the passed over Date string with SimpleDateFormat:

SimpleDateFormat formatter1 = new SimpleDateFormat("MM-dd-yyyy HH:mm z");       
SimpleDateFormat formatter2 = new SimpleDateFormat("MM-dd-yyyy'T'HH:mmz");
try {
        //datePicked and datePickedFormatted are global Date variables
        datePicked = formatter1.parse(request.getParameter("sendTime"));
        datePickedFormatted = formatter2.parse(datePicked.toString());
    } catch (ParseException e1) {
        System.out.println("Date formatter failed to parse chosen sendTime.");
        e1.printStackTrace();
    }

I get this error in my log:

java.text.ParseException: Unparseable date: "08-22-2014 05:18 -04:00"

Why is it still being sent to the servlet formatted like that, when I try to format it to match SimpleDateFormat's? Why won't SimpleDateFormat accept it?

I even tried to parse the date string with Joda-Time instead of using SimpleDateFormat, but even that didn't work:

DateTime dt = new DateTime(request.getParameter("sendTime"));

Any help or comments would be much appreciated. Thanks.

Upvotes: 0

Views: 5351

Answers (1)

Koolstr
Koolstr

Reputation: 502

I've solved the problem, getting a head start thanks to users' comments. What I did was use SimpleDateFormat to parse the format that the date string was arriving in, into a Date object. I then constructed a DateTime object (Joda-Time) with that Date object:

datePicked = formatter1.parse(request.getParameter("sendTime"));
DateTime dt = new DateTime(datePicked);

DateTime defaults to UTC format and ISO-8601, so the .toString() method for it is accepted by Parse.com's database without a problem. This is the final code in the end which works:

    SimpleDateFormat formatter1 = new SimpleDateFormat("MM-dd-yyyy HH:mm X");       
    try {
        datePicked = formatter1.parse(request.getParameter("sendTime"));
        System.out.println("Retrieved and formatted dateTime: " + datePicked.toString());
    } catch (ParseException e1) {
        System.out.println("Date formatter failed to parse chosen sendTime.");
        e1.printStackTrace();
    }
    DateTime dt = new DateTime(datePicked);

Upvotes: 1

Related Questions