KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > Conversion


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.util.Calendar JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22
23 /**
24  * Utility helper class providing helper methods for the conversion of values.
25  *
26  * @author Kevin Roast
27  */

28 public final class Conversion
29 {
30    /**
31     * Private constructor
32     */

33    private Conversion()
34    {
35    }
36    
37    /**
38     * Convert a Date to an XML format date of the form <pre>yyyy-mm-ddThh:mm:ss</pre>
39     *
40     * @param date Non-null Date value to convert
41     *
42     * @return XML format date string
43     */

44    public static String JavaDoc dateToXmlDate(Date JavaDoc date)
45    {
46       Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
47       calendar.setTime(date);
48       
49       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(20);
50
51       // perform year conversion by hand to guarentee universal format
52
String JavaDoc month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
53       String JavaDoc day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
54       String JavaDoc hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
55       String JavaDoc minute = Integer.toString(calendar.get(Calendar.MINUTE));
56       String JavaDoc second = Integer.toString(calendar.get(Calendar.SECOND));
57       
58       buf.append(calendar.get(Calendar.YEAR))
59          .append('-');
60       
61       if (month.length() == 1)
62       {
63          buf.append('0');
64       }
65       buf.append(month)
66          .append('-');
67       
68       if (day.length() == 1)
69       {
70          buf.append('0');
71       }
72       buf.append(day)
73          .append('T');
74       
75       if (hour.length() == 1)
76       {
77          buf.append('0');
78       }
79       buf.append(hour)
80          .append(":");
81       
82       if (minute.length() == 1)
83       {
84          buf.append('0');
85       }
86       buf.append(minute)
87       .append(":");
88
89       if (second.length() == 1)
90       {
91          buf.append('0');
92       }
93       buf.append(second);
94       
95       return buf.toString();
96    }
97    
98    /**
99     * Convert an XML format date of the form <pre>yyyy-mm-ddThh:mm:ss</pre> to a Date object
100     *
101     * @param xml
102     *
103     * @return Date object or null if no XML date was provided
104     */

105    public static Date JavaDoc dateFromXmlDate(String JavaDoc xml)
106    {
107       if (xml == null || xml.length() != 19)
108       {
109          return null;
110       }
111       
112       int year = Integer.parseInt(xml.substring(0, 4));
113       int month = Integer.parseInt(xml.substring(5, 7));
114       int day = Integer.parseInt(xml.substring(8, 10));
115       int hours = Integer.parseInt(xml.substring(11, 13));
116       int mins = Integer.parseInt(xml.substring(14, 16));
117       int secs = Integer.parseInt(xml.substring(17, 19));
118
119       Calendar JavaDoc calendar = new GregorianCalendar JavaDoc(year, month - 1, day, hours, mins, secs);
120       
121       return calendar.getTime();
122    }
123 }
124
Popular Tags