如何将日期格式从dd-MM-yyyy更改为yyyy-mm-dd,我得到的结果为Thu Jan 02 00:00:00 IST 2014

时间:2014-11-01 08:42:08

标签: java mysql servlets

 HttpSession session = req.getSession(false);
 if(session!=null)
 {    
     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
     SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
     DateFormat sdf2 = new SimpleDateFormat("yyyy-mm-dd");

     String studid = req.getParameter("studid");
     int studentid = Integer.parseInt(studid);
     String firstName = req.getParameter("firstname");
     String lastName = req.getParameter("lastname");
     String emailstudent = req.getParameter("emailstud");
     String qualification = req.getParameter("qualification");
     String dateintervieww = req.getParameter("dtintervieww");
     try {

         java.util.Date dtt_intervw = sdf.parse(dateintervieww);
         String dateintervw = sdf1.format(dtt_intervw);
         dtt_intervw = sdf1.parse(dateintervw);
         dateintervw  = sdf2.format(dtt_intervw);

         dtt_intervw = sdf2.parse(dateintervw);
         dateintervw  = sdf2.format(dtt_intervw);


     } catch (ParseException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
     }

     String joining_course = req.getParameter("joiningcourse");
     String ending_course = req.getParameter("endingcourse");
     String medicalinsurdate4rm = req.getParameter("mediinsurancedat4rm");
     String medicalinsurdatto = req.getParameter("mediinsurancedatto");

     String drive_location = req.getParameter("drivelocation");
     String course_offered = req.getParameter("courseoffered");

     String place_training = req.getParameter("placetraining");
     String modeselection = req.getParameter("modeselection");
     String selectprocedure = req.getParameter("selectprocedure");
     String bankacccnt_no = req.getParameter("bankaccntno");
     String Travel_exp  = req.getParameter("travelexpense");
     double trvl_exp = Double.parseDouble(Travel_exp);
     String medicalinsurnum = req.getParameter("mediinsurno");
     String Transferto = req.getParameter("transferto");

     String leaving_hostel = req.getParameter("leavinghostel");
     String placementstatus = req.getParameter("placementstatus");

     String appoint_letter = req.getParameter("appoint_letter");
     String select_month1 = req.getParameter("selectmonth1");
     String year1 = req.getParameter("year1");

     String select_month2 = req.getParameter("selectmonth2");
     String year2 = req.getParameter("year2");

     String select_month3 = req.getParameter("selectmonth3");
     String year3 = req.getParameter("year2");

     String book_return = req.getParameter("book_returns");

     LoginBean lgbean = new LoginBean();
     lgbean.setStudid(studentid);
     lgbean.setFirstname(firstName);
     lgbean.setLastname(lastName);
     lgbean.setEmail(emailstudent);
     lgbean.setQualification(qualification);
     lgbean.setDateinterview(dateintervieww);
     lgbean.setJoiningcourse(joining_course);
     lgbean.setEndingcourse(ending_course);
     lgbean.setMediinsurancedat4rm(medicalinsurdate4rm);
     lgbean.setMediinsurancedatto(medicalinsurdatto);

     lgbean.setDrivelocation(drive_location);
     lgbean.setCourseoffered(course_offered);
     lgbean.setPlacetraining(place_training);
     lgbean.setModeselection(modeselection);
     lgbean.setSelectprocedure(selectprocedure);
     lgbean.setBankaccntno(bankacccnt_no);
     lgbean.setTravelexpense(trvl_exp);
     lgbean.setMedicalinsuranceno(medicalinsurnum);
     lgbean.setTransferto(Transferto);

     lgbean.setLeavinghostel(leaving_hostel);
     lgbean.setPlacementstatus(placementstatus);
     lgbean.setAppoint_letter(appoint_letter);
     lgbean.setSelectmonth1(select_month1);
     lgbean.setYear1(year1);

     lgbean.setSelectmonth2(select_month2);
     lgbean.setYear2(year2);

     lgbean.setSelectmonth3(select_month3);
     lgbean.setYear3(year3);

     lgbean.setBook_returns(book_return);

     serviceclass sc = new serviceclass();  
     int l =  sc.submitstudentrecord(lgbean);

     if(l>0)
     {
         resp.sendRedirect("studentrecordpagee.do");
     }
     else
     {
         resp.sendRedirect("studgenrartereport.do");
     }

 }
 else
 {
     resp.sendRedirect("logOut.do");
 }  

1 个答案:

答案 0 :(得分:2)

试试这个

try {
        SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
        String date = "01-11-2014";
        Date d = inputFormat.parse(date);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        System.out.println(formatter.format(d));
    } catch (ParseException e) {
        e.printStackTrace();
    }