KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > handlers > NetDateTime


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.handlers;
22
23 import java.util.*;
24
25 import com.db4o.*;
26
27 /**
28  * @exclude
29  */

30 // TODO: Between .NET and Java there seems to be a difference of two days between era offsets?!?
31
public class NetDateTime extends NetSimpleTypeHandler{
32     private final static String JavaDoc ZEROES="0000";
33     
34     private final static String JavaDoc[] MONTHS= {
35         "Jan",
36         "Feb",
37         "Mar",
38         "Apr",
39         "May",
40         "Jun",
41         "Jul",
42         "Aug",
43         "Sep",
44         "Oct",
45         "Nov",
46         "Dec"
47     };
48     
49     // ms between 01.01.0001,00:00:00.000 and 01.01.1970,00:00:00.000
50
//private static final long ERA_DIFFERENCE_IN_MS = 62135604000000L; // Carl's diff
51
private static final long ERA_DIFFERENCE_IN_MS = 62135596800000L; // .net diff
52
//private static final long ERA_DIFFERENCE_IN_MS = 62135769600000L; // java diff
53

54     // ratio from .net ticks (100ns) to java ms
55
private static final long TICKS_TO_MS_RATIO = 10000;
56
57     public NetDateTime(YapStream stream) {
58         super(stream, 25, 8);
59     }
60     
61     public String JavaDoc toString(byte[] bytes) {
62         long ticks = 0;
63         for (int i = 0; i < 8; i++) {
64             ticks = (ticks << 8) + (bytes[i] & 255);
65         }
66         long ms = ticks / TICKS_TO_MS_RATIO - ERA_DIFFERENCE_IN_MS;
67         Date date=new Date(ms);
68         Calendar cal=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
69         cal.setTime(date);
70         StringBuffer JavaDoc result=new StringBuffer JavaDoc()
71             .append(prependZeroes(cal.get(Calendar.YEAR),4))
72             .append('-')
73             .append(MONTHS[cal.get(Calendar.MONTH)])
74             .append('-')
75             .append(prependZeroes(cal.get(Calendar.DAY_OF_MONTH),2))
76             .append(", ")
77             .append(prependZeroes(cal.get(Calendar.HOUR_OF_DAY),2))
78             .append(':')
79             .append(prependZeroes(cal.get(Calendar.MINUTE),2))
80             .append(':')
81             .append(prependZeroes(cal.get(Calendar.SECOND),2))
82             .append('.')
83             .append(prependZeroes(cal.get(Calendar.MILLISECOND),3))
84             .append(" UTC");
85         return result.toString();
86     }
87     
88     private String JavaDoc prependZeroes(int val,int size) {
89         String JavaDoc str=String.valueOf(val);
90         int missing=size-str.length();
91         if(missing>0) {
92             str=ZEROES.substring(0,missing)+str;
93         }
94         return str;
95     }
96 }
97
Popular Tags