-->
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.  [ 4 posts ] 
Author Message
 Post subject: GTM Time to local time
PostPosted: Thu Mar 02, 2006 6:49 am 
Newbie

Joined: Thu Mar 02, 2006 6:32 am
Posts: 2
I have following problem, i need convert the GMT time to local time, so I add 8 hours to GMT time, the HQL seem selected the number of records correctly so i assume the conversion is successful however the date obtain from individual object getter property does not convert successfuly. Am I doing anything wrong here?



mapping table

<property
name="reg_created"
type="date"
column="REG_CREATED + 8/24 "
length="7"
/>


HQL syntax
from ItsmServicecall Servicecall where to_number (to_char(Servicecall.regCreated , 'YYYY')) ='2006'
and to_number (to_char(Servicecall.regCreated , 'mm')) = 1

Sql generated by Hibernate 3.1
select itsmservic0_.SER_OID as SER1_90_, itsmservic0_.SER_ID as SER2_90_, itsmservic0_.REG_CREATED + 8/24 as REG3_90_, itsmservic0_.SER_DESCRIPTION as SER4_90_, itsmservic0_.SER_SRV_OID as SER5_90_, itsmservic0_.SER_ASS_WOG_OID as SER6_90_, itsmservic0_.SER_ASS_PER_TO_OID as SER7_90_, itsmservic0_.SER_CALLER_PER as SER8_90_, itsmservic0_.SER_CAT_OID as SER9_90_, itsmservic0_.SER_STA_OID as SER10_90_, itsmservic0_.SER_CLO_OID as SER11_90_, itsmservic0_.SER_IMP_OID as SER12_90_ from SERVICEDESK.ITSM_SERVICECALLS itsmservic0_ where to_number(to_char(itsmservic0_.REG_CREATED + 8/24 , 'YYYY'))='2006' and to_number(to_char(itsmservic0_.REG_CREATED + 8/24 , 'mm'))=1

expecting result


1 Sun 01-01-06
2 Sun 01-01-06
3 Sun 01-01-06
4 Sun 01-01-06
5 Tue 03-01-06
6 Thu 05-01-06
7 Mon 09-01-06
8 Fri 20-01-06
9 Mon 23-01-06
10 Thu 26-01-06

actual result (number of record is correct but the date is wrong)


1 Sat 31-12-05
2 Sat 31-12-05
3 Sat 31-12-05
4 Sat 31-12-05
5 Mon 02-01-06
6 Wed 04-01-06
7 Sun 08-01-06
8 Thu 19-01-06
9 Sun 22-01-06


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 5:38 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Changing timezones is not a job for select statements. Have a look at the javadocs for java.util.Date and java.util.Calendar. You should probably be dealing with only a single, well-known timezone at the database layer, and only worry about timezones at the UI layer, because that's a presentation thing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:46 pm 
Newbie

Joined: Thu Mar 02, 2006 6:32 am
Posts: 2
Thank, :) i got your point.

I implemented this code to solve the problem.

Code:
public void setRegCreated(Date regCreated) {      
   long localTime = regCreated.getTime(); 
   localTime += 1L*8*60*60*1000;      
   this.regCreated = new Date(localTime);;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 11:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Here's a better alternative.
Code:
  private Date m_dteInternalDate = null; // This is in milliseconds from the epoch, TZ agnositic.
   private static final TimeZone m_tzUI = TimeZone.getTimeZone("GMT+5"); // Really this should be in the UI layer, and passed into methods in this class
  private static final Calendar m_Cal = GregorianCalendar.getInstance(m_tzUI);

  /* This is the mapped getter, used only by Hibernate */
  public Date getInternalDate()
  {
    return m_dteInternalDate;
  }

  /* This is the mapped setter, used only by Hibernate */
  public void setInternalDate(Date date)
  {
    m_dteInternalDate = date;
  }

  /* This is the getter in the interface, used by the API */
  public Calendar getDate()
  {
    return cal.setTime(m_dteInternalDate).clone();
  }

  /* This is the setter in the interface, used by the API */
  public void setDate(Calendar date)
  {
    m_dteInternalDate = date.getTime().clone();
  }
Better still, you could try using a custom UserType.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.