Java Sql Date cast to DateWritable - java

commonEDWUDFManager.ROUND(expression.toString(), format)
.getTime()
is giving the output 1901-01-01 in java.util.Date format
Date sqlDate = new java.sql.Date(commonEDWUDFManager.ROUND(expression.toString(), format.getTime());
System.out.println(sqlDate);
output 1901-01-01
DateWritable dateWritable = new DateWritable(sqlDate);
System.out.println(dateWritable);
output 1901-01-02
When I cast java.sql.Date to DateWritable it is changing the Date.
I want to know why?

Related

Insert Date in string format into a database having field type of Date

I am having a date in the following format as string.
String from_date = "2016-09-09";
I need to insert into into a database having date type field using PreparedStatement. I have done it in the following way.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date fd = formatter.parse(from_date);
select1.setDate(1, fd);
But it is showing the error as
The method setDate(int.java.sql.Date) in the type PreparedStatement is not applicable to the arguments(int,java.util.Date)
You need to use a java.sql.Date when inserting in database. Try the following :
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date fd = formatter.parse(from_date);
java.sql.Date sqlDate = new java.sql.Date(fd.getTime());
select1.setDate(1, sqlDate);
Answered here: Type mismatch: cannot convert from java.util.Date to java.sql.Date
You are trying to use a method that accepts java.sql.Date instead of java.util.Date
Welcome to programming, mind the details
Using java.sql.Date
java.util.Date
Suppose you have a variable endDate of type java.util.Date, you make the conversion thus:
select1.setDate(1, new java.sql.Date(endDate.getTime());

Timestamp to String to DateTime issues

I tried to see questions about date convert issues between two database using java but didn't solve my problem.
Here is the current date to insert in my database with a DateTime format :
java.sql.Date SQLDateValue = new java.sql.Date(System.currentTimeMillis()) ;
preparedStatement.setDate(index, SQLDateValue);
And here is the Timestamp from an API named Vdoc, convert to String and i tried to convert it to java.sql.Date (DateTime) :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateValue = (java.util.Date) this.getWorkflowInstance().getValue(this.ListeChamps[i][2]);
String StringDateValue = DateValue.toString();
java.sql.Date SQLDateValue = new java.sql.Date (sdf.parse(StringDateValue).getTime());
preparedStatement.setDate(index, SQLDateValue);
The second line return a field value containing a String but i need to use toString().
The following error message is :
Failed to convert the date and / or time from a string.
Both of my date parameters are java.sql.date, i don't understand.
If you have an idea of ​​what happens with this, it would be nice to help me.
Ezerah
Sorry for my bad english
Just construct the java.sql.Date from java.util.Date.
Call java.util.Date::getTime to extract the count of milliseconds from epoch. Pass that count to constructor of java.sql.Date.
In your case below should work.
java.util.Date DateValue = (java.util.Date) this.getWorkflowInstance().getValue(this.ListeChamps[i][2]);
java.sql.Date SQLDateValue = new java.sql.Date (DataValue.getTime());
preparedStatement.setDate(index, SQLDateValue);
try this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateValue = **sdf.parse(**this.getWorkflowInstance().getValue(this.ListeChamps[i][2])**)**;
String StringDateValue = DateValue.toString();
java.sql.Date SQLDateValue = new java.sql.Date (sdf.parse(StringDateValue).getTime());
preparedStatement.setDate(index, SQLDateValue);
You can't cast String to Date, you should parse it

getting value from jDateChooser and saving to MS sql DB

I have two jDateChooser on my dialog , I want to save to MS-SQL DB having issue with that data types. Any idea how to fix this issue ! I can only do this when i convert data type to nvarchar in DB and convert the value to string which returns from jDateChooser.
// I can save in this way but I it doesn't use jDateChooser;
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
// I cant save the date with jDateChooser
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate());
// Only Way I found
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sd = dateFormat.format(jDateChooser3.getDate());
obj.setStartDate(sd);
//
Judging from the code you posted it looks like jDateChooser3.getDate() returns a java.util.Date instance while the java.sql.Date(millis) constructor expects the date/time as a long milliseconds value.
Use this code and it will work:
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate().getTime());
Since it comes from a date chooser component, invalid input most likely results in null returned date, so you might want to also check on that:
java.util.Date d = jDateChooser3.getDate();
if (d == null) {
System.out.println("No date specified!");
} else {
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// Do something with sqldate
}
Right click on jDateChooser. Go to the Properties and dateFormatString. Set this format: dd-MM-yyyy.
Date keyword = jDateChooserattendance.getDate();
java.sql.Date sqldate = new java.sql.Date(keyword.getTime());

Convert String to date and compare

One of the fields which I extract from a database is a date in string format and I need to convert it into a date type to compare with another date.
How do I do this please? Everything I have tried so far gives me an error of java.util.Date cannot be cast to java.sql.Date and this is attempting the following example;
simpledateformat-gives-java-lang-classcastexception-java-util-date
An example of the date extracted in string format is "2012-10-15 09:00:29.157". I think the format to declare is yyyy-MM-dd HH:mm:ss.SSS but not sure.
Thanks
You can create a java.util.Date from java.sql.Date as follows:
java.util.Date date = new java.util.Date(resultSet.getDate("Column").getTime());
Alternatively you can convert the java.sql.Date to either java.util.Calendar or Joda library's DateTime and perform comparisons.
If you are looking at parsing date, then java.text.SimpleDateFormat is your friend.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.util.Date date = format.parse("2012-10-15 09:00:29.157");
It simply means you are assigning java.util.Date in java.sql.Date
From your example you have to do following :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.util.Date date = (java.util.Date)formatter.parse(""2012-10-15 09:00:29.157"");
Convert to Date as Vikdor showed. then get the long timestamp = date.getTimestamp();
Now this long value is millisecons since 1970 utc. just compare by this value.

date type in java to fill a MySQL database

i have a field in my database (MySQL) with date type (with format yyyy-mm-dd) .I dont what data type to use to assing the date type of MySql to Java . Also i have to convert a string to this type .
Use a java.sql.Date to get the value of a date column:
java.sql.Date date = resultSet.getDate("theDateColumn");
To convert a string to a java.sql.Date, use a DateFormat to parse the string into a java.util.Date, and then transform the java.util.Date into a java.sql.Date:
String s = "01/28/2013";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date d = df.parse(s);
java.sql.Date date = new java.sql.Date(d.getTime());

Categories

Resources