View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.model.common.xml;
20  
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.Locale;
26  
27  import javax.xml.bind.DatatypeConverter;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * The Class XmlTimeTypeAdapter.
34   */
35  public final class XmlTimeTypeAdapter {
36  
37  	private static final String YYYY_MM_DD_HH_MM_SS = "2001-01-01 00:00:00";
38  
39  	/** The Constant YYYY_MM_DD. */
40  	private static final String YYYY_MM_DD = "yyyy-MM-dd";
41  
42  	private static final Logger LOGGER = LoggerFactory
43  			.getLogger(XmlTimeTypeAdapter.class);
44  
45  
46  	/**
47  	 * Instantiates a new xml time type adapter.
48  	 */
49  	private XmlTimeTypeAdapter() {
50  		super();
51  	}
52  
53  	/**
54  	 * Parses the date.
55  	 *
56  	 * @param s
57  	 *            the s
58  	 * @return the date
59  	 */
60  	public static Date parseDate(final String s) {
61  		if (s == null) {
62  			return null;
63  		}
64  		// hack to supported swedish riksdags xml.
65  		final String dateStr = s.trim();
66  		if (YYYY_MM_DD_HH_MM_SS.length() == dateStr.length()
67  				|| YYYY_MM_DD.length() == dateStr.length()) {
68  			try {
69  				return new SimpleDateFormat(
70  						YYYY_MM_DD,Locale.ENGLISH).parse(dateStr.substring(0,YYYY_MM_DD.length()));
71  			} catch (final ParseException e) {
72  				LOGGER.warn("Problem parsing date from str:{}",s,e);
73  			}
74  		}
75  		return DatatypeConverter.parseDate(s).getTime();
76  	}
77  
78  	/**
79  	 * Prints the date.
80  	 *
81  	 * @param dt
82  	 *            the dt
83  	 * @return the string
84  	 */
85  	public static String printDate(final Date dt) {
86  		if (dt == null) {
87  			return null;
88  		}
89  		final Calendar c = Calendar.getInstance();
90  		c.setTime(dt);
91  		return DatatypeConverter.printDate(c);
92  	}
93  
94  	/**
95  	 * Parses the time.
96  	 *
97  	 * @param s
98  	 *            the s
99  	 * @return the date
100 	 */
101 	public static Date parseTime(final String s) {
102 		if (s == null) {
103 			return null;
104 		}
105 		return DatatypeConverter.parseTime(s).getTime();
106 	}
107 
108 	/**
109 	 * Prints the time.
110 	 *
111 	 * @param dt
112 	 *            the dt
113 	 * @return the string
114 	 */
115 	public static String printTime(final Date dt) {
116 		if (dt == null) {
117 			return null;
118 		}
119 		final Calendar c = Calendar.getInstance();
120 		c.setTime(dt);
121 		return DatatypeConverter.printTime(c);
122 	}
123 
124 	/**
125 	 * Parses the date time.
126 	 *
127 	 * @param s
128 	 *            the s
129 	 * @return the date
130 	 */
131 	public static Date parseDateTime(final String s) {
132 		if (s == null) {
133 			return null;
134 		}
135 		return DatatypeConverter.parseDateTime(s).getTime();
136 	}
137 
138 	/**
139 	 * Prints the date time.
140 	 *
141 	 * @param dt
142 	 *            the dt
143 	 * @return the string
144 	 */
145 	public static String printDateTime(final Date dt) {
146 		if (dt == null) {
147 			return null;
148 		}
149 		final Calendar c = Calendar.getInstance();
150 		c.setTime(dt);
151 		return DatatypeConverter.printDateTime(c);
152 	}
153 }