Search code examples
javastringdatetimecallable-statement

Java String to Date


I have a Stored Procedure that requires the following input parameters -

PROCEDURE `Report_Publishing`(p_StartDate datetime, p_EndDate dateTime, p_Action varchar(15))


From the Frontend I am getting the start and end dates in following format -
start as a String "2017-03-15" and end as a String "2017-04-17",

Following is the piece of code, to convert the start and end dates into the Stored Procedure's required format -

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.text.SimpleDateFormat; 

public List<PublishReportDto> getExport(String start, String end, String action, Connection conn, String sql) {
        List<PublishReportDto> publishList = null;

        try {
            CallableStatement cs = conn.prepareCall(sql);

            start += " 00:00:00";
            end += " 00:00:00";
            Date startDate = null;
            Date endDate = null;
            try {
                startDate = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(start);
                endDate = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            cs.setDate(1,(java.sql.Date) startDate);
            cs.setDate(2,(java.sql.Date) endDate);
            cs.setString(3, action);
            cs.registerOutParameter(1, java.sql.Types.DATE);
            cs.registerOutParameter(2, java.sql.Types.DATE);
            cs.registerOutParameter(3, java.sql.Types.VARCHAR);
            ResultSet rs = cs.executeQuery();

I am getting the following Exception -

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date


Modifications -
Replaced -
1. import java.sql.Date with import java.util.Date;
2.

cs.setDate(1,startDate);
cs.setDate(2,endDate);

by

cs.setDate(1,(java.sql.Date) startDate);
cs.setDate(2,(java.sql.Date) endDate);

Now I'm getting -

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

Can someone please help me out?


Solution

  • you can convert from java.util.Date to java.sql.Date by using the constructor of the java.sql.Date:

    Date(long date) //constructor of the java.sql.Date

    Constructs a Date object using the given milliseconds time value.

    to get the start date:

    Date startDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(start).getTime());
    

    to get the end date:

    Date endDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(end).getTime());
    

    note - in the future If you are going to be working with date-only values you should use the LocalDate class rather than java.util.Date.