简体   繁体   中英

How to format a jQuery date using SimpleDateFormat

I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??

System.out.println("Activity IS :  IS  With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");

System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);

This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help

You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "

Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".

The pattern "yyyy-MM-dd " cannot be right due to two reasons:

a) It starts with a four-digit-year but your input begins with a two-digit-number.

b) It contains a trailing space.

Try to do it.

    SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date d = sdfOverTimeWithDate.parse("07/05/2015");
    } catch (ParseException e) {
        e.printStackTrace();
    }

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