convert object into date - java

I get an ArrayList of Object[] from a database and I want to convert the java.sql.Date stored in an object into a java.util.Date (in order to use it in jfreechart):
my code is as follows:
fills up the Array of object with data from MySQL
ArrayList<Object[]> mydata=new ArrayList<>();
mydata=sqlGetter.getMdbObjectList(sqlString, null);
for(Object[] myobject : mydata){
if (myobject[1].getClass()==java.sql.Date.class){
java.util.Date mydate=null;
mydate = Date ( myobject[1]);
}
}
Netbeans return an error: "Java incompatible types: Object cannot be converted to Date"
While I understand the idea, I would have expected to be able to cast the object into a Date after having checked that it is indeed of the right class.
I'm starting java, so please any helps on the obvious mistake that I must be doing would be useful.

You are missing a cast. Additionally, you'd be better off using the instanceof operator:
for(Object[] myobject : mydata){
// Note that java.sql.Date extends java.util.Date
if (myobject[1] instanceof java.util.Date) {
java.util.Date mydate = (java.util.Date) myobject[1];
}
}

you just need to explicit cast the object to Date
ex: (Date) obj;

Related

Converting LocalDateTime Object to LocalDateTime

I am new to Java and I am working on Java 8.
I am trying to convert LocalDateTime Object to LocalDateTime but not able to find any way without converting it to String. Is there any direct method for converting Object to LocalDateTime when the underlying Object type is LocalDateTime?
Moreover, if there is any way to convert, can it work for underlying String type LocalDateTime Object too?
Below is my current code which is converting the Object to String before converting it to LocalDateTime as LocalDateTime.parse method needs String input.
public static LocalDateTime toDateTime(Object dateTimeObject) {
LocalDateTime dateTime = LocalDateTime.parse(dateTimeObject.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
return dateTime;
}
If the object is a LocalDateTime, you can cast the object.
public static LocalDateTime toDateTime(Object dateTimeObject) {
if (dateTimeObject instanceof LocalDateTime) {
return (LocalDateTime) dateTimeObject;
}
return null;
}
According to the Java 8 LocalDateTime API there is no method that takes an Object argument and returns a LocalDateTime. But there is one that takes a CharSequence parameter, which is why it works when you convert the object to String, and why it won't work if you just pass the Object parameter. If you don't want to have to do the call to the toString() method, perhaps the parameter of toDateTime(Object o) should be of a different type.

Spring Mapping using a custom mapper method

I have two objects with all the same members except for the date member.
In Obj1.date is a java.sql.Date, and Obj2.date is a long (epoch).
I need to write a mapper to map obj1 to obj2. This is what I tried to do:
#Named("sqlDateToEpoch")
default long sqlDateToEpoch(Date timestamp) throws ParseException {
return myUtils.sqlDateToEpoch(timestamp);
}
#Mapping(source = "date", target = "date", qualifiedByName = "sqlDateToEpoch")
Obj2 toObj2(Obj1 source);
List<Obj2> toRecordList(List<Obj1> source);
But the mapperImpl just has its own implementation for the date conversion:
if (source.getDate() != null) {
Obj2.setDate(Long.parseLong(source.getDate()));
}
I'm getting:
java.lang.NumberFormatException: For input string: "2019-04-02 00:00:00.0"
What is the right way for this kind of conversion?
I think that the reason why it doesn't work is because your source.getDate() is returning a String and not a java.sql.Date. This leads to MapStruct using the implicit String to long conversion.
In order to fix this you would either need to make sure that source.getDate() returns java.sql.Date or add a method that would get a String and return Long.

getting classcast exception while converting date from string type to date data type

I am getting the date (SettlementDate) in form of string in a method parameter as shown in the below method
public List<abc> eee(Long Id,
String sfsdf, boolean dfds,
String SettlementDate,
) {
now I am applying criteria as shown below but in pojo to which I am applying criteria IN THAT POJO THAT FIELD (SettlementDate) is of Date type as shown below
class ddd
{
private Date settlementDate;
}
below is the criteria implemetation
Criteria query = session.createCriteria(ddd.class);
query.add(Restrictions.eq("Id", Id));
query.add(Restrictions.eq("sfsdf", sfsdf));
query.add(Restrictions.eq("settlementDate", SettlementDate );
ultimately in database i want to store the date in this format 22.07.2016 00:00:00
now please advise how how do i set this value in criteria as rite now I am getting class cast exception in the below line
query.add(Restrictions.eq("settlementDate", SettlementDate );
which clearly proves that date of string type is not converted in date format ( 22.07.2016 00:00:00)
folks , if I have to use simple date format then please advise how to use that in this situation ,Thanks
If I understand it correctly, you want convert a date string to a date. You can try this:
query.add(Restrictions.eq("settlementDate", new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").parse(SettlementDate)));
Some other remarks:
When storing a date in a db, you should use a Date or DateTime format, or maybe a Number containing the millisecs, but definately not a String
Java classes should start with a capital
Variable and parameter names should start with a lowercase letter

java.util.Date cannot be cast to Object[]

I'm using java reflection to call methods at runtime. How do I cast a date object into an object array?
ie,
Method m = ....;
Object[] result = (Object[]) m.invoke(...);
public Date getDate() {
return new Date();
}
Would give:
java.lang.ClassCastException: java.util.Date cannot be cast to [Ljava.lang.Object;
Edit: yeah, I should just add it to an Object array instead and return it.
you don't need to as invoke is a varargs method. you can do.
Object result = method.invoke (instance , new Date ());
There is no need to create an array.
If it's the result you want to change you can wrap it, but I suspect there is no real need to do this. I would see if you really need an array

Converting java.sql.Date to java.util.Date

What's the simplest way to convert a java.sql.Date object to a java.util.Date while retaining the timestamp?
I tried:
java.util.Date newDate = new Date(result.getDate("VALUEDATE").getTime());
with no luck. It's still only storing the date portion into the variable.
The class java.sql.Date is designed to carry only a date without time, so the conversion result you see is correct for this type. You need to use a java.sql.Timestamp to get a full date with time.
java.util.Date newDate = result.getTimestamp("VALUEDATE");
If you really want the runtime type to be util.Date then just do this:
java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
Brian.
Since java.sql.Date extends java.util.Date, you should be able to do
java.util.Date newDate = result.getDate("VALUEDATE");
This function will return a converted java date from SQL date object.
public static java.util.Date convertFromSQLDateToJAVADate(
java.sql.Date sqlDate) {
java.util.Date javaDate = null;
if (sqlDate != null) {
javaDate = new Date(sqlDate.getTime());
}
return javaDate;
}
From reading the source code, if a java.sql.Date does actually have time information, calling getTime() will return a value that includes the time information.
If that is not working, then the information is not in the java.sql.Date object. I expect that the JDBC drivers or the database is (in effect) zeroing the time component ... or the information wasn't there in the first place.
I think you should be using java.sql.Timestamp and the corresponding resultset methods, and the corresponding SQL type.
In the recent implementation, java.sql.Data is an subclass of java.util.Date, so no converting needed.
see here: https://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Date.html

Categories