简体   繁体   中英

Moment.js Date parse exception in java

I am using this format to getting a date from a user on a form

moment().format('MMMM Do YYYY, h:mm:ss a');

getting this

// May 30th 2018, 11:38:58 am

but when try to format this date in java like below getting a parse exception

SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
            Date date= ft.parse(jsonObject.get("date").getAsString());

can anyone tell me whats wrong , any working example

Just replace th , it is not mandatory

SimpleDateFormat ft = new SimpleDateFormat("MMM dd yyyy, hh:mm:ss a", Locale.US);

        String dateString = "May 30th 2018, 11:38:58 am";
        Date date= ft.parse(dateString
                .replaceAll("(?<=\\d)(st|nd|rd|th)", "")
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM