1 /******************************************************************
2 JADE - Java Agent DEvelopment Framework is a framework to develop
3 multi-agent systems in compliance with the FIPA specifications.
4 Copyright (C) 2000 CSELT S.p.A.
5
6 GNU Lesser General Public License
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation,
11 version 2.1 of the License.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22 *****************************************************************/
23
24 package sl;
25
26 import java.util.*;
27 import java.text.SimpleDateFormat;
28
29 /***
30 * This class contains a set of static methods that allow to convert
31 * to/from the Date Time format specified by ISO8601 and adopted by FIPA.
32
33
34 @author Fabio Bellifemine - CSELT
35 @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
36
37 */
38 public class ISO8601 {
39
40 /***
41 * This method converts a String, that represents a Date Time Token
42 * in IS8601 format, to a java.util.Date object.
43 */
44 public static Date toDate(String dateTimeToken) throws Exception {
45 if (dateTimeToken == null)
46 return new Date();
47 else {
48 int pos;
49 if(dateTimeToken.substring(0, 1).equals("+")) {
50
51 pos = 1;
52 long millisec = Integer.parseInt(dateTimeToken.substring(pos, pos + 4))*365*24*60*60*1000+
53 Integer.parseInt(dateTimeToken.substring(pos + 4, pos + 6))*30*24*60*60*1000+
54 Integer.parseInt(dateTimeToken.substring(pos + 6, pos + 8))*24*60*60*1000+
55 Integer.parseInt(dateTimeToken.substring(pos + 9, pos +11))*60*60*1000+
56 Integer.parseInt(dateTimeToken.substring(pos + 11, pos + 13))*60*1000+
57 Integer.parseInt(dateTimeToken.substring(pos + 13, pos + 15))*1000;
58 return(new Date((new Date()).getTime() + millisec));
59 } else {
60 pos = 0;
61 GregorianCalendar cal = new GregorianCalendar(
62 Integer.parseInt(dateTimeToken.substring(pos, pos + 4)),
63 Integer.parseInt(dateTimeToken.substring(pos + 4, pos + 6))-1,
64 Integer.parseInt(dateTimeToken.substring(pos + 6, pos + 8)),
65 Integer.parseInt(dateTimeToken.substring(pos + 9, pos +11)),
66 Integer.parseInt(dateTimeToken.substring(pos + 11, pos + 13)),
67 Integer.parseInt(dateTimeToken.substring(pos + 13, pos + 15))
68 );
69 return cal.getTime();
70 }
71 }
72 }
73
74 /***
75 * This method converts a <code>java.util.Date</code> into a String
76 * in ISO8601 format.
77 * @return a String, e.g. "19640625T073000000" to represent the 7:30 of the
78 * 25th of June of 1964.
79 */
80 public static String toString(Date d){
81 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS");
82 return formatter.format(d);
83 }
84
85 /***
86 * this method converts into a string in ISO8601 format representing
87 * relative time from the current time
88 * @param millisec is the number of milliseconds from now
89 * @return a String, e.g. "+00000000T010000000" to represent one hour
90 * from now
91 */
92 public static String toRelativeTimeString(long millisec) {
93 if (millisec > 0) {
94 long tmp = millisec/1000;
95 long msec = millisec - tmp*1000;
96 millisec = tmp;
97
98 tmp = millisec/60;
99 long sec = millisec - tmp*60;
100 millisec = tmp;
101
102 tmp = millisec/60;
103 long min = millisec - tmp*60;
104 millisec = tmp;
105
106 tmp = millisec/24;
107 long h = millisec - tmp*24;
108 millisec = tmp;
109
110 tmp = millisec/30;
111 long day = millisec - tmp*30;
112 millisec = tmp;
113
114 tmp = millisec/12;
115 long mon = millisec - tmp*12;
116 millisec = tmp;
117
118 long year = millisec;
119 return "+"+zeroPaddingNumber(year,4)+zeroPaddingNumber(mon,2)+
120 zeroPaddingNumber(day,2)+"T"+zeroPaddingNumber(h,2)+
121 zeroPaddingNumber(min,2)+zeroPaddingNumber(sec,2)+
122 zeroPaddingNumber(msec,3);
123 }
124 else
125 return "+00000000T000000000";
126 }
127
128
129 private static String zeroPaddingNumber(long value, int digits) {
130 String s = Long.toString(value);
131 int n=digits-s.length();
132 for (int i=0; i<n; i++)
133 s="0"+s;
134 return s;
135 }
136
137
138
139 /***
140 * The main is here only for debugging.
141 * You can test your conversion by executing the following command:
142 * <p>
143 * <code> java jade.lang.acl.ISO8601 <yourtoken> </code>
144 */
145 public static void main(String argv[]) {
146 System.out.println(argv[0]);
147 System.out.println("USAGE: java pacselt.ISO8601 DateTimetoken");
148 try {
149 System.out.println("ISO8601.toDate("+argv[0]+") returns:" + ISO8601.toDate(argv[0]));
150 } catch (Exception e) {
151 e.printStackTrace();
152 }
153
154 try {
155 System.out.println("ISO8601.toRelativeTimeString("+argv[0]+") returns:" + ISO8601.toRelativeTimeString(Long.parseLong(argv[0])));
156
157 Date d = new Date(Integer.parseInt(argv[0]));
158 System.out.println("ISO8601.toString("+d+") returns:" + ISO8601.toString(d));
159 } catch (Exception e1) {
160 }
161
162 Date d1 = new Date();
163 System.out.println("ISO8601.toString("+d1+") returns:" + ISO8601.toString(d1));
164 }
165 }