-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: dates
PostPosted: Thu May 17, 2007 5:49 pm 
Newbie

Joined: Thu May 17, 2007 5:40 pm
Posts: 1
Location: Bruges
hi,

I'm kind of new to hibernate and I ran into a problem,

I'm trying to generate statistics of a platform written in java and runs on an oracle DB

what I'm trying to achieve is to get an overview from how many people have logged on to the system. Now...

When I read the dates from the DB I get the full date AND the time, now since I'm only interested in the date part, because I want to group on date, I would like to cut of the time part,

Is there any way to do that?

thx
Renaat


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 18, 2007 2:09 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

I haven't experience in oracle db . But i know there are two dates type "DATE" and "TIMESTAMP" . default format for "DATE" is "DD-MON-YY".so it is not a problem.


If you want to get date part of the datevalue you can use usertype
like this.
package lk.wts.test;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class DateUserType implements UserType {
private static final int[] TYPES = {Types.DATE};
SimpleDateFormat shortDate=new SimpleDateFormat("dd/MMM/yyyy");
public int[] sqlTypes() {
return TYPES;
}

public Class returnedClass() {
return Date.class;
}

public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 instanceof Date) {
Date date1 = (Date) arg0;
if (arg1 instanceof Date) {
Date date2 = (Date) arg1;
if(shortDate.format(date1).equalsIgnoreCase(shortDate.format(date2))){
return true;
}
}
}

return false;
}

public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}

public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)
throws HibernateException, SQLException {
java.sql.Date date = (java.sql.Date)Hibernate.DATE.nullSafeGet(rs, names[0]);

try {
return shortDate.parse(shortDate.format(date));
} catch (ParseException e) {
throw new HibernateException(e.getMessage());
}
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
java.util.Date dateTime = (value==null) ? new java.util.Date() : (java.util.Date)value;

Hibernate.DATE.nullSafeSet(st, dateTime, index);
}

public Object deepCopy(Object value) throws HibernateException {
if (value==null) return null;
return new java.util.Date(((java.util.Date)value).getTime());

}

public boolean isMutable() {
return false;
}

public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) deepCopy(value);
}

public Object assemble(Serializable value, Object arg1)
throws HibernateException {
return deepCopy(value);

}

public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return deepCopy(arg0);
}

}


<property name="logindate" type="lk.wts.test.DateUserType">
<column name="LoginTime" sql-type="date" not-null="true"/>
</property>


First check your datatype in oracle DB then try this user type

Amila


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.