KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > impl > CF_hhhmmss


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model.impl;
14
15 import java.text.DecimalFormat JavaDoc;
16
17 import com.tonbeller.jpivot.olap.model.Cell;
18 import com.tonbeller.jpivot.olap.model.CellFormatter;
19
20 /**
21   * this is an example for the cell formatting exit
22   * returns hhh:mm:ss for the value beeing number of seconds
23   * hhh = hours
24   * mm = minutes
25   * ss = seconds
26  */

27 public class CF_hhhmmss implements CellFormatter {
28
29   static DecimalFormat JavaDoc f00 = new DecimalFormat JavaDoc("00");
30   
31   public String JavaDoc formatCell(Cell cell) {
32     long longVal = objToLong(cell.getValue());
33
34     long hours = longVal / 3600;
35     long secs = longVal % 3600;
36     long mins = secs / 60;
37     secs = secs % 60;
38     String JavaDoc str = Long.toString(hours) + ":" + f00.format(mins) + ":" + f00.format(secs);
39     return str;
40   }
41
42   public static long objToLong(Object JavaDoc obj) {
43     long longVal;
44     if (obj instanceof Double JavaDoc)
45       longVal = ((Double JavaDoc) obj).longValue();
46     else if (obj instanceof Long JavaDoc)
47       longVal = ((Long JavaDoc) obj).longValue();
48     else if (obj instanceof Integer JavaDoc)
49       longVal = ((Integer JavaDoc) obj).longValue();
50     else if (obj instanceof String JavaDoc)
51       longVal = Long.parseLong((String JavaDoc) obj);
52     else
53       longVal = 0;
54     return longVal;
55   }
56 } // CellFormat
57
Popular Tags