• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The  above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE. */
20 
21 package org.ksoap2.kobjects.isodate;
22 
23 import java.util.*;
24 
25 public class IsoDate {
26 
27     public static final int DATE = 1;
28     public static final int TIME = 2;
29     public static final int DATE_TIME = 3;
30 
dd(StringBuffer buf, int i)31     static void dd(StringBuffer buf, int i) {
32         buf.append((char) (((int) '0') + i / 10));
33         buf.append((char) (((int) '0') + i % 10));
34     }
35 
dateToString(Date date, int type)36     public static String dateToString(Date date, int type) {
37 
38         Calendar c = Calendar.getInstance();
39         c.setTimeZone(TimeZone.getTimeZone("GMT"));
40         c.setTime(date);
41 
42         StringBuffer buf = new StringBuffer();
43 
44         if ((type & DATE) != 0) {
45             int year = c.get(Calendar.YEAR);
46             dd(buf, year / 100);
47             dd(buf, year % 100);
48             buf.append('-');
49             dd(
50                     buf,
51                     c.get(Calendar.MONTH) - Calendar.JANUARY + 1);
52             buf.append('-');
53             dd(buf, c.get(Calendar.DAY_OF_MONTH));
54 
55             if (type == DATE_TIME)
56                 buf.append("T");
57         }
58 
59         if ((type & TIME) != 0) {
60             dd(buf, c.get(Calendar.HOUR_OF_DAY));
61             buf.append(':');
62             dd(buf, c.get(Calendar.MINUTE));
63             buf.append(':');
64             dd(buf, c.get(Calendar.SECOND));
65             buf.append('.');
66             int ms = c.get(Calendar.MILLISECOND);
67             buf.append((char) (((int) '0') + (ms / 100)));
68             dd(buf, ms % 100);
69             buf.append('Z');
70         }
71 
72         return buf.toString();
73     }
74 
stringToDate(String text, int type)75     public static Date stringToDate(String text, int type) {
76 
77         Calendar c = Calendar.getInstance();
78 
79         if ((type & DATE) != 0) {
80             c.set(
81                     Calendar.YEAR,
82                     Integer.parseInt(text.substring(0, 4)));
83             c.set(
84                     Calendar.MONTH,
85                     Integer.parseInt(text.substring(5, 7))
86                             - 1
87                             + Calendar.JANUARY);
88             c.set(
89                     Calendar.DAY_OF_MONTH,
90                     Integer.parseInt(text.substring(8, 10)));
91 
92             if (type != DATE_TIME || text.length() < 11) {
93                 c.set(Calendar.HOUR_OF_DAY, 0);
94                 c.set(Calendar.MINUTE, 0);
95                 c.set(Calendar.SECOND, 0);
96                 c.set(Calendar.MILLISECOND, 0);
97                 return c.getTime();
98             }
99             text = text.substring(11);
100         }
101         else
102             c.setTime(new Date(0));
103 
104         c.set(
105                 Calendar.HOUR_OF_DAY,
106                 Integer.parseInt(text.substring(0, 2)));
107         // -11
108         c.set(
109                 Calendar.MINUTE,
110                 Integer.parseInt(text.substring(3, 5)));
111         c.set(
112                 Calendar.SECOND,
113                 Integer.parseInt(text.substring(6, 8)));
114 
115         int pos = 8;
116         if (pos < text.length() && text.charAt(pos) == '.') {
117             int ms = 0;
118             int f = 100;
119             while (true) {
120                 char d = text.charAt(++pos);
121                 if (d < '0' || d > '9')
122                     break;
123                 ms += (d - '0') * f;
124                 f /= 10;
125             }
126             c.set(Calendar.MILLISECOND, ms);
127         }
128         else
129             c.set(Calendar.MILLISECOND, 0);
130 
131         if (pos < text.length()) {
132 
133             if (text.charAt(pos) == '+'
134                     || text.charAt(pos) == '-')
135                 c.setTimeZone(
136                         TimeZone.getTimeZone(
137                                 "GMT" + text.substring(pos)));
138 
139             else if (text.charAt(pos) == 'Z')
140                 c.setTimeZone(TimeZone.getTimeZone("GMT"));
141             else
142                 throw new RuntimeException("illegal time format!");
143         }
144 
145         return c.getTime();
146     }
147 }
148