How to change the String Value to Date Value without Hardcoded String value

If we have a Method
updateStatusBySales1(bean, bean.dispatcheddate);

In this method we have dispatcheddate. this dispatcheddate is assigned here as String. While inserting into table it will through error. Because in the database table the dispatched date is assigned as date format.

Want to change the dispatched date value as date.

How to change the String value into date without hardcoded the value...
While Surfing the net i found that all the examples given is hard coded value. 

For example String str = "07/02/2010" 

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "07/02/2010";

 try {

  Date date = formatter.parse(dateInString);
  System.out.println(date);
  System.out.println(formatter.format(date));

 } catch (ParseException e) {
  e.printStackTrace();
 }
 --------------------------------
Here i will show you Records fetching from excel sheet
---------------------------------  
POJO Class --Infobean--
 
@Getter @Setter private String dispatcheddate;
@Getter @Setter private String deliverydate; 
--------------------------------------------------- 
BulkFile.java
------------------
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class BulkFile {
    
     public void UploadProcess1(String string) throws IOException, ClassNotFoundException, ParseException {
        UserInfoBean bean = new UserInfoBean();
        short column;
        int sheetno = 1;
        String Row = "";

        bean.workbook = new XSSFWorkbook(new FileInputStream(new File(string)));
        bean.Sheet = bean.workbook.getSheetAt(sheetno - 1);

        for (int k = 0; k < bean.Sheet.getPhysicalNumberOfRows() - 1; k++) {
            bean.row = bean.Sheet.getRow(k + 1);
            column = bean.row.getLastCellNum();
            for (int m = 0; m < column; m++) {

                bean.RowObj = bean.row.getCell((short) (m));
                if (bean.RowObj == null) {
                    Row = "";
                } else {
                    Row = bean.RowObj.toString();
                }
                if (m == 0) {
                    if (!Row.trim().equals("")) {

                        bean.printorder = Integer.parseInt(Row.trim().replace(".0", ""));
                    }
                } else if (m == 1) {//empid
                    if (!Row.trim().equals("")) {
                        bean.userid = Row.trim().replace(".0", "");
                    }
                } else if (m == 2) {//empid
                    if (!Row.trim().equals("")) {
                        bean.districcode = Row.trim().replace(".0", "");
                        System.out.println(bean.districcode);
                    }
                } else if (m == 8) {//deliverydate
                    if (!Row.trim().equals("")) {
                        bean.deliverydate = Row.trim();
                        String spltdate[] = bean.deliverydate.split("\\/");
                        System.out.println("spltdate.length is"+spltdate.length);
                        bean.dispatcheddate = spltdate[2] + "-" + spltdate[1] + "-" + spltdate[0];
                        bean.deliverydate= bean.dispatcheddate;
                    } else {
                        bean.dispatcheddate = "";
                    }
 
System.out.println("Process completed successfully"+ bean.BillingNumber);
System.out.println("Process completed successfully"+ (bean.Sheet.getPhysicalNumberOfRows() - 1));
 
 new Query().updateStatus(bean, bean.dispatcheddate);

        }

        System.out.println("Process completed successfully");
    }
    
} 
 In this bean.dispatcheddate we have string value that is fetched from the Excel sheet.
It calls the Query.java class there we will write the connections and update query.
 
Query.java
----------
public void updateStatusBySales1(UserInfoBean bean, String from, String distype, String dismethod, String trackno, String othersSm, String billingnumber, String billingdetail) throws ClassNotFoundException, ParseException{
        try {
            System.out.println("The from Value is:"+bean.deliverydate); 
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            date = df.parse(bean.deliverydate);
            String dispatcheddate = df.format(date);

            System.out.println(dispatcheddate);

  Here only we change the string value to Date.

 
 
 
            con = getconnection();
            pst = con.prepareStatement("update status  set dispatcheddate='" + dispatcheddate + "' where printorder=" + bean.printorder + "");
            int i = pst.executeUpdate();
        } 
           catch (SQLException e) {
            e.printStackTrace(); 
  
 
 

Comments

Popular posts from this blog

How do i Increase the capacity of the Eclipse Output console?

An internal error occurred during: "Building workspace". Java heap space

How to convert the Math Number to Words in Java?