Convert String to Time Java.sql.time error [duplicate] - java

This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 5 years ago.
I want to convert a string to time using the parse method which is going to be inserted to database later. But I get: Incompatible Types: Java.util.date cannot be converted to Java.sql.Date. Any Solution?
String s = time.getText();
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date d = sdf.parse(s);

SimpleDateFormat parse() method returns java.util.Date whereas you have imported java.sql.Date, so change the imported class to java.util.Date

the errors occurs when java.sql.Date imported which is derived from java.util.Date, and DateFormat.parse return a java.util.Date. so you can't assign java.util.Date to derived type java.sql.Date.
import java.util.Date;
//not
import java.sql.Date;
OR assign it to a full-name type of java.util.Date when you both using java.sql.Date and java.util.Date:
java.util.Date d = sdf.parse(s);
OR if you need convert java.util.Date to a java.sql.Date you can do as follows:
java.sql.Date d = new java.sql.Date(sdf.parse(s).getTime());
OR if you need convert java.util.Date to a java.sql.Time you can do as follows:
java.sql.Time d = new java.sql.Time(sdf.parse(s).getTime());

Related

how can i convert int to date in java? [duplicate]

This question already has answers here:
Good way to convert integer YYYYMMDD into java.util.Date with local time zone
(4 answers)
How can I convert an Integer (e.g 19000101 ) to java.util.Date?
(5 answers)
Type mismatch: cannot convert from java.util.Date to java.sql.Date
(3 answers)
Closed 2 years ago.
I want to convert an int value to a date.
For example, 20200605 is 2020-06-05
This is my code, Why doesn't it work?
public static void main(String[] args) {
int value = 19000101;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(Integer.toString(value));
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = newFormat.format(date);
}
I am getting an error on this line.
Date date = originalFormat.parse(Integer.toString(value));
The error message is
Type mismatch: cannot convert from java.util.Date to java.sql.Date
I don't know what it means.
The error you get:
Type mismatch: cannot convert from java.util.Date to java.sql.Date
tells that you have imported a wrong Date object. Please check at the top of the class. You will need to replace import java.sql.Date; with import java.util.Date;
I don't know much about the Date and SimpleDateFormat classes in java, but this should be fairly easy to do anyway without it.
String date_str = ""+value;
String formattedDate = date_str.substring(0,4)+"-"+date_str.substring(4,6)+"-"+date_str.substring(6,8);

Convert java.util.Date to java.sql.Date with 'yyyy-mm-dd hh mm ss' [duplicate]

This question already has answers here:
java sql date time
(2 answers)
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 7 years ago.
I need to pass json string to one of the rest web service. There is a property called 'amortizationdate' which will have format 'yyyy-mm-dd hh mm ss'.
for e.g. {amortizationdate:2015-07-31 00:00:00}
we are having AmortizationVO.java with a property amortizationdate.
for e.g
private java.sql.Date amortizationDate.
public void setAmortizationDate(java.sql.Date date){
}
We need to set the date by calling setAmortizationDate(..date) method and using Jackson to convert AmortizationVO.java to Json.
but in JSON I m getting {amortizationdate:2015-07-31}. But expected result should be with timestamp.(amortizationdate:2015-07-31 00:00:00)
note: I don't want to use util date in my Value Object.
Pls help.
What I've tried:
java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());
ExcelEntityAddressVO entityAddressVO = new ExcelEntityAddressVO();
entityAddressVO.setAmortizationDate(new java.sql.Date(sq.getTime()));
This is my JSON:
{
"amortizationdate" : "2015-07-31",
}
You don't want java.sql.Date - it doesn't keep time, use java.sql.Timestamp
You can try this:
import java.sql.Timestamp;
public class Test {
public static void main(String[] args) {
java.util.Date utilDate = new java.util.Date();
System.out.println("utilDate:" + utilDate);
Timestamp ts = new Timestamp(utilDate.getTime());
System.out.println(ts);
}
}

java.util.Date cannot be cast to java.sql.Date [duplicate]

This question already has answers here:
java.util.Date vs java.sql.Date
(7 answers)
Closed 5 years ago.
I am trying to insert Date into Database but I am getting error as java.util.Date cannot be cast to java.sql.Date.
Please help.
String next_dt = req.getParameter("NextDate");
DateFormat dtFmt = null;
dtFmt = new SimpleDateFormat("yyyy-MM-dd");
dtToday = (Date) dtFmt.parse(next_dt);
You have imported java.sql.Data. But dtFmt.parse(next_dt); returns an object of type java.util.Date so you have to change
import java.sql.Date;
to
import java.util.Date;
DateFormat.parse() returns a java.util.Date, and you're trying to illegally cast it to a java.sql.Date.
Assuming you continue to import java.sql.Date, you can successfully assign the variable like so:
dtToday = new Date(dtFmt.parse(next_dt).getTime());
You should use java.sql.Timestamp or java.sql.Date instead of java.util.Date
Problem with java.sql.Date is that it will not store time. So using Timestamp is the approach i always take. As it is the child class of java.util.date it is compatible with both date and timestamp columns in DB.
Add following lines - as it needs to be a sql Date and not util date
java.sql.Date sqlDate = new java.sql.Date(dtToday.getTime());
//now insert this sqlDate
public static java.sql.Date convertFromJAVADateToSQLDate(
java.util.Date javaDate) {
java.sql.Date sqlDate = null;
if (javaDate != null) {
sqlDate = new Date(javaDate.getTime());
}
return sqlDate;
}
As the name of the class is same use should give fully qualified name(FQN) of both classes you also can use format method to convert date to proper SQL format date.
public static String toMysqlDateStr(Date date) {
String dateForMySql = "";
if (date == null) {
dateForMySql = null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateForMySql = sdf.format(date);
}
return dateForMySql;
}

error casting Java.Util.Date into Java.Sql.Date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert java.util.date to java.sql.date?
I found error on my function, it shows error result after initializing the newInstance() method from DatatypeFactory df , I'm getting another error:
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
I just change the package name from
java.util.Date into java.SQL.Date
then casting:
Date dateStarting = (Date) jDateChooserStart.getDate();
Date dateEnding = (Date) jDateChooserEnd.getDate();
How to resolve this issue?
(post before: Convert jcalendar Date into XMLGregorianCalendar Getting Null Value)
It's not possible to cast from java.util.Date to java.sql.Date. You need to convert from one type to the other instead:
java.util.Date utilStartDate = jDateChooserStart.getDate();
java.sql.Date sqlStartDate = new java.sql.Date(utilStartDate.getTime());
From the class cast exception you can see that these are 2 distinct types and can't be cast the from one to the other. To convert from java.util.Date to java.sql.Date, you can use:
java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());

SimpleDateFormat gives java.lang.classcastexception: java.util.date

String str = "13/06/2011";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = (Date)formatter.parse(str);
I guess that your Date class is actually a java.sql.Date.
What does your import statement say? Are you importing some other class (for example java.sql.Date) by accident? What does the compiler say when you remove the class cast (which should not be there)?
DateFormat.parse() returns an instance of java.util.Date and not java.sql.Date.
In order to convert from java.util.Date to java.sql.Date, I do the following:
java.util.Date fromDate = df.parse(fromdate1);
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime());

Categories