XmlTimeTypeAdapter.java

  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. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.Locale;

  25. import javax.xml.bind.DatatypeConverter;

  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;

  28. /**
  29.  * The Class XmlTimeTypeAdapter.
  30.  */
  31. public final class XmlTimeTypeAdapter {

  32.     private static final String YYYY_MM_DD_HH_MM_SS = "2001-01-01 00:00:00";

  33.     /** The Constant YYYY_MM_DD. */
  34.     private static final String YYYY_MM_DD = "yyyy-MM-dd";

  35.     private static final Logger LOGGER = LoggerFactory
  36.             .getLogger(XmlTimeTypeAdapter.class);


  37.     /**
  38.      * Instantiates a new xml time type adapter.
  39.      */
  40.     private XmlTimeTypeAdapter() {
  41.         super();
  42.     }

  43.     /**
  44.      * Parses the date.
  45.      *
  46.      * @param s
  47.      *            the s
  48.      * @return the date
  49.      */
  50.     public static Date parseDate(final String s) {
  51.         if (s == null) {
  52.             return null;
  53.         }
  54.         // hack to supported swedish riksdags xml.
  55.         final String dateStr = s.trim();
  56.         if (YYYY_MM_DD_HH_MM_SS.length() == dateStr.length()
  57.                 || YYYY_MM_DD.length() == dateStr.length()) {
  58.             try {
  59.                 return new SimpleDateFormat(
  60.                         YYYY_MM_DD,Locale.ENGLISH).parse(dateStr.substring(0,YYYY_MM_DD.length()));
  61.             } catch (final ParseException e) {
  62.                 LOGGER.warn("Problem parsing date from str:{}",s,e);
  63.             }
  64.         }
  65.         return DatatypeConverter.parseDate(s).getTime();
  66.     }

  67.     /**
  68.      * Prints the date.
  69.      *
  70.      * @param dt
  71.      *            the dt
  72.      * @return the string
  73.      */
  74.     public static String printDate(final Date dt) {
  75.         if (dt == null) {
  76.             return null;
  77.         }
  78.         final Calendar c = Calendar.getInstance();
  79.         c.setTime(dt);
  80.         return DatatypeConverter.printDate(c);
  81.     }

  82.     /**
  83.      * Parses the time.
  84.      *
  85.      * @param s
  86.      *            the s
  87.      * @return the date
  88.      */
  89.     public static Date parseTime(final String s) {
  90.         if (s == null) {
  91.             return null;
  92.         }
  93.         return DatatypeConverter.parseTime(s).getTime();
  94.     }

  95.     /**
  96.      * Prints the time.
  97.      *
  98.      * @param dt
  99.      *            the dt
  100.      * @return the string
  101.      */
  102.     public static String printTime(final Date dt) {
  103.         if (dt == null) {
  104.             return null;
  105.         }
  106.         final Calendar c = Calendar.getInstance();
  107.         c.setTime(dt);
  108.         return DatatypeConverter.printTime(c);
  109.     }

  110.     /**
  111.      * Parses the date time.
  112.      *
  113.      * @param s
  114.      *            the s
  115.      * @return the date
  116.      */
  117.     public static Date parseDateTime(final String s) {
  118.         if (s == null) {
  119.             return null;
  120.         }
  121.         return DatatypeConverter.parseDateTime(s).getTime();
  122.     }

  123.     /**
  124.      * Prints the date time.
  125.      *
  126.      * @param dt
  127.      *            the dt
  128.      * @return the string
  129.      */
  130.     public static String printDateTime(final Date dt) {
  131.         if (dt == null) {
  132.             return null;
  133.         }
  134.         final Calendar c = Calendar.getInstance();
  135.         c.setTime(dt);
  136.         return DatatypeConverter.printDateTime(c);
  137.     }
  138. }