001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.utils;
021    
022    import java.lang.ref.Reference;
023    import java.lang.ref.SoftReference;
024    import java.text.*;
025    import java.util.Date;
026    
027    /**
028     * Parses and formats ISO 8601 dates. See http://en.wikipedia.org/wiki/ISO_8601.
029     * This class is thread-safe.
030     *
031     * @since 2.7
032     */
033    public final class DateUtils {
034      public static final String DATE_FORMAT = "yyyy-MM-dd";
035      public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
036    
037      private static final ThreadSafeDateFormat dateFormat = new ThreadSafeDateFormat(DATE_FORMAT);
038      private static final ThreadSafeDateFormat dateTimeFormat = new ThreadSafeDateFormat(DATETIME_FORMAT);
039    
040      public static String formatDate(Date d) {
041        return dateFormat.format(d);
042      }
043    
044      public static String formatDateTime(Date d) {
045        return dateTimeFormat.format(d);
046      }
047    
048      public static Date parseDate(String s) {
049        try {
050          return dateFormat.parse(s);
051    
052        } catch (ParseException e) {
053          throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'", e);
054        }
055      }
056    
057      public static Date parseDateTime(String s) {
058        try {
059          return dateTimeFormat.parse(s);
060    
061        } catch (ParseException e) {
062          throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'", e);
063        }
064      }
065    
066      static class ThreadSafeDateFormat extends DateFormat {
067        private final String format;
068    
069        ThreadSafeDateFormat(String format) {
070          this.format = format;
071        }
072    
073        private final transient ThreadLocal cache = new ThreadLocal() {
074          public Object get() {
075            Reference softRef = (Reference)super.get();
076            if (softRef == null || softRef.get() == null) {
077              softRef = new SoftReference(new SimpleDateFormat(format));
078              super.set(softRef);
079            }
080            return softRef;
081          }
082        };
083    
084        private DateFormat getDateFormat() {
085          return (DateFormat) ((Reference)cache.get()).get();
086        }
087    
088        public StringBuffer format(Date date,StringBuffer toAppendTo, FieldPosition fieldPosition) {
089          return getDateFormat().format(date, toAppendTo, fieldPosition);
090        }
091    
092        public Date parse(String source, ParsePosition pos) {
093          return getDateFormat().parse(source, pos);
094        }
095      }
096    }