Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » How to extract Hour from Date(Need to extract Hour as integer form Datetime field)
icon5.gif  How to extract Hour from Date [message #488344] Mon, 28 September 2009 10:55 Go to next message
Michele Mising name is currently offline Michele Mising nameFriend
Messages: 25
Registered: September 2009
Junior Member
Hi.

In my dataset I have a field called "Datetime".
The source date has format "yyyy-mm-dd HH:mm:ss" (2009-09-03 12:01:00).

I set this field as Datetime and when I look at the "Preview Results", my datefield has format "3 Sep 2009 13:01".

I need to create a new field which contains the hour as integer, in my example that would be "13".

Since there is not an BirtDateTime.hour(row["Date"]) function, what kind of expression can I use?

Thanks,
Michele
Re: How to extract Hour from Date [message #488414 is a reply to message #488344] Mon, 28 September 2009 15:04 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Michele,

Can you try adding a computed column and try an expression like:

importPackage( Packages.java.text );
var mydt1 = row["ORDERDATE"];
var sdf = new SimpleDateFormat("hh");
var mydtf1 = sdf.format( mydt1 );
parseInt( mydtf1 );


Jason

Michele wrote:
> Hi.
>
> In my dataset I have a field called "Datetime".
> The source date has format "yyyy-mm-dd HH:mm:ss" (2009-09-03 12:01:00).
>
> I set this field as Datetime and when I look at the "Preview Results",
> my datefield has format "3 Sep 2009 13:01".
>
> I need to create a new field which contains the hour as integer, in my
> example that would be "13".
>
> Since there is not an BirtDateTime.hour(row["Date"]) function, what kind
> of expression can I use?
>
> Thanks,
> Michele
Re: How to extract Hour from Date [message #488426 is a reply to message #488414] Mon, 28 September 2009 15:43 Go to previous messageGo to next message
Michele Mising name is currently offline Michele Mising nameFriend
Messages: 25
Registered: September 2009
Junior Member
Hi Jason.
Tried as you suggested, but it returns wrong values.

Example:
3 Sep 2009 16:01 = 4
3 Sep 2009 17:01 = 5
3 Sep 2009 18:01 = 6
3 Sep 2009 19:01 = 7
3 Sep 2009 20:01 = 0
3 Sep 2009 21:01 = 0
3 Sep 2009 22:01 = 10

Note that 4, 5, 6, 7 should be 16, 17, 18, 19 and I cannot understand why 20 and 21 equals 0.

Any idea on how to modify your script?
Or alternative solution?

Thanks.
Michele

PS: why o why there is no such a function as BirtDateTime.hour() and BirtDateTime.minute()?
Re: How to extract Hour from Date [message #488433 is a reply to message #488426] Mon, 28 September 2009 16:02 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Can you try this:

importPackage(Packages.java.util);
cl = new GregorianCalendar();
cl.setGregorianChange( row["ORDERDATE"] );
cl.get(Calendar.HOUR_OF_DAY);

Michele wrote:
> Hi Jason.
> Tried as you suggested, but it returns wrong values.
>
> Example:
> 3 Sep 2009 16:01 = 4
> 3 Sep 2009 17:01 = 5
> 3 Sep 2009 18:01 = 6
> 3 Sep 2009 19:01 = 7
> 3 Sep 2009 20:01 = 0
> 3 Sep 2009 21:01 = 0
> 3 Sep 2009 22:01 = 10
>
> Note that 4, 5, 6, 7 should be 16, 17, 18, 19 and I cannot understand
> why 20 and 21 equals 0.
>
> Any idea on how to modify your script?
> Or alternative solution?
>
> Thanks.
> Michele
>
> PS: why o why there is no such a function as BirtDateTime.hour() and
> BirtDateTime.minute()?
Re: How to extract Hour from Date [message #488435 is a reply to message #488433] Mon, 28 September 2009 16:12 Go to previous messageGo to next message
Michele Mising name is currently offline Michele Mising nameFriend
Messages: 25
Registered: September 2009
Junior Member
Hi Jason.

I tried your latest script and the result is "17" for each records!!
Confused

Michele
Re: How to extract Hour from Date [message #488447 is a reply to message #488435] Mon, 28 September 2009 17:10 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

One more time:

importPackage(Packages.java.util);
cl = new GregorianCalendar();
cl.setTime(dataSetRow["dt"]);
cl.get(Calendar.HOUR_OF_DAY);

see attached report

Jason

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.17"
id="1">
<property name="createdBy">Eclipse BIRT Designer Version
2.3.2.r232_20090202 Build &lt;2.3.2.v20090218-0730></property>
<property name="units">in</property>
<property name="iconFile">/templates/blank_report.gif</property>
<property name="bidiLayoutOrientation">ltr</property>
<data-sources>
<oda-data-source
extensionID="org.eclipse.birt.report.data.oda.jdbc" name="Data Source"
id="7">
<text-property name="displayName"></text-property>
<property
name="odaDriverClass">org.eclipse.birt.report.data.oda.sampledb.Driver </property>
<property name="odaURL">jdbc:classicmodels:sampledb</property>
<property name="odaUser">ClassicModels</property>
<property name="OdaConnProfileName"></property>
</oda-data-source>
<script-data-source name="Data Source1" id="11"/>
</data-sources>
<data-sets>
<script-data-set name="Data Set1" id="12">
<list-property name="resultSetHints">
<structure>
<property name="position">0</property>
<property name="name">dt</property>
<property name="dataType">date-time</property>
</structure>
</list-property>
<list-property name="columnHints">
<structure>
<property name="columnName">dt</property>
</structure>
</list-property>
<structure name="cachedMetaData">
<list-property name="resultSet">
<structure>
<property name="position">1</property>
<property name="name">dt</property>
<property name="dataType">date-time</property>
</structure>
</list-property>
</structure>
<property name="dataSource">Data Source1</property>
<method name="open"><![CDATA[i=0;]]></method>
<method name="fetch"><![CDATA[if( i > 1 )return false;
if( i == 0 ){
row["dt"] = new Date('January 16, 1988 2:54:16 pm');
}
if( i == 1 ){
row["dt"] = new Date('January 16, 1988 11:54:16 am');
}
i++;
return true;]]></method>
</script-data-set>
</data-sets>
<styles>
<style name="report" id="4">
<property name="fontFamily">"Verdana"</property>
<property name="fontSize">10pt</property>
</style>
<style name="crosstab" id="5">
<property name="borderBottomColor">#CCCCCC</property>
<property name="borderBottomStyle">solid</property>
<property name="borderBottomWidth">1pt</property>
<property name="borderLeftColor">#CCCCCC</property>
<property name="borderLeftStyle">solid</property>
<property name="borderLeftWidth">1pt</property>
<property name="borderRightColor">#CCCCCC</property>
<property name="borderRightStyle">solid</property>
<property name="borderRightWidth">1pt</property>
<property name="borderTopColor">#CCCCCC</property>
<property name="borderTopStyle">solid</property>
<property name="borderTopWidth">1pt</property>
</style>
<style name="crosstab-cell" id="6">
<property name="borderBottomColor">#CCCCCC</property>
<property name="borderBottomStyle">solid</property>
<property name="borderBottomWidth">1pt</property>
<property name="borderLeftColor">#CCCCCC</property>
<property name="borderLeftStyle">solid</property>
<property name="borderLeftWidth">1pt</property>
<property name="borderRightColor">#CCCCCC</property>
<property name="borderRightStyle">solid</property>
<property name="borderRightWidth">1pt</property>
<property name="borderTopColor">#CCCCCC</property>
<property name="borderTopStyle">solid</property>
<property name="borderTopWidth">1pt</property>
</style>
</styles>
<page-setup>
<simple-master-page name="Simple MasterPage" id="2">
<property name="topMargin">0.25in</property>
<property name="leftMargin">0.25in</property>
<property name="bottomMargin">0.25in</property>
<property name="rightMargin">0.25in</property>
</simple-master-page>
</page-setup>
<body>
<table id="13">
<property name="width">100%</property>
<property name="dataSet">Data Set1</property>
<list-property name="boundDataColumns">
<structure>
<property name="name">dt</property>
<property name="displayName">dt</property>
<expression
name="expression">dataSetRow["dt"]</expression>
<property name="dataType">date-time</property>
</structure>
<structure>
<property name="name">Column Binding</property>
<expression
name="expression">importPackage(Packages.java.util);
cl = new GregorianCalendar();
//cl.setGregorianChange( dataSetRow["dt"] );
cl.setTime(dataSetRow["dt"]);
cl.get(Calendar.HOUR_OF_DAY);</expression>
<property name="dataType">integer</property>
</structure>
</list-property>
<column id="22"/>
<column id="26"/>
<header>
<row id="14">
<cell id="15">
<label id="16">
<text-property name="text">dt</text-property>
</label>
</cell>
<cell id="23"/>
</row>
</header>
<detail>
<row id="17">
<cell id="18">
<data id="19">
<property name="resultSetColumn">dt</property>
</data>
</cell>
<cell id="24">
<data id="27">
<property name="resultSetColumn">Column
Binding</property>
</data>
</cell>
</row>
</detail>
<footer>
<row id="20">
<cell id="21"/>
<cell id="25"/>
</row>
</footer>
</table>
</body>
</report>

Michele wrote:
> Hi Jason.
>
> I tried your latest script and the result is "17" for each records!!
> :?
> Michele
icon10.gif  Re: How to extract Hour from Date [message #488536 is a reply to message #488447] Tue, 29 September 2009 07:53 Go to previous message
Michele Mising name is currently offline Michele Mising nameFriend
Messages: 25
Registered: September 2009
Junior Member
Hi Jason.

Your last suggestion did the trick!!

Thanks.

Michele
Previous Topic:Excel page setup
Next Topic:PDF Export page break Problem.
Goto Forum:
  


Current Time: Fri Apr 26 20:43:02 GMT 2024

Powered by FUDForum. Page generated in 0.03825 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top