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 THREAD_SAFE_DATE_FORMAT = new ThreadSafeDateFormat(DATE_FORMAT);
038      private static final ThreadSafeDateFormat THREAD_SAFE_DATETIME_FORMAT = new ThreadSafeDateFormat(DATETIME_FORMAT);
039    
040      private DateUtils() {
041      }
042    
043      public static String formatDate(Date d) {
044        return THREAD_SAFE_DATE_FORMAT.format(d);
045      }
046    
047      public static String formatDateTime(Date d) {
048        return THREAD_SAFE_DATETIME_FORMAT.format(d);
049      }
050    
051      public static Date parseDate(String s) {
052        try {
053          return THREAD_SAFE_DATE_FORMAT.parse(s);
054    
055        } catch (ParseException e) {
056          throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'", e);
057        }
058      }
059    
060      public static Date parseDateTime(String s) {
061        try {
062          return THREAD_SAFE_DATETIME_FORMAT.parse(s);
063    
064        } catch (ParseException e) {
065          throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'", e);
066        }
067      }
068    
069      static class ThreadSafeDateFormat extends DateFormat {
070        private final String format;
071    
072        ThreadSafeDateFormat(String format) {
073          this.format = format;
074        }
075    
076        private final transient ThreadLocal cache = new ThreadLocal() {
077          public Object get() {
078            Reference softRef = (Reference) super.get();
079            if (softRef == null || softRef.get() == null) {
080              softRef = new SoftReference(new SimpleDateFormat(format));
081              super.set(softRef);
082            }
083            return softRef;
084          }
085        };
086    
087        private DateFormat getDateFormat() {
088          return (DateFormat) ((Reference) cache.get()).get();
089        }
090    
091        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
092          return getDateFormat().format(date, toAppendTo, fieldPosition);
093        }
094    
095        public Date parse(String source, ParsePosition pos) {
096          return getDateFormat().parse(source, pos);
097        }
098      }
099    }