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.SoftReference;
023    import java.text.*;
024    import java.util.Date;
025    
026    /**
027     * Parses and formats ISO 8601 dates. See http://en.wikipedia.org/wiki/ISO_8601.
028     * This class is thread-safe.
029     *
030     * @since 2.7
031     */
032    public final class DateUtils {
033      public static final String DATE_FORMAT = "yyyy-MM-dd";
034      public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
035    
036      private static final DateFormat dateFormat = new ThreadSafeDateFormat(DATE_FORMAT);
037      private static final DateFormat dateTimeFormat = new ThreadSafeDateFormat(DATETIME_FORMAT);
038    
039      public static String formatDate(Date d) {
040        return dateFormat.format(d);
041      }
042    
043      public static String formatDateTime(Date d) {
044        return dateTimeFormat.format(d);
045      }
046    
047      public static Date parseDate(String s) {
048        try {
049          return dateFormat.parse(s);
050    
051        } catch (ParseException e) {
052          throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'");
053        }
054      }
055    
056      public static Date parseDateTime(String s) {
057        try {
058          return dateTimeFormat.parse(s);
059    
060        } catch (ParseException e) {
061          throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'");
062        }
063      }
064    
065      static class ThreadSafeDateFormat extends DateFormat {
066        private final String format;
067    
068        ThreadSafeDateFormat(String format) {
069          this.format = format;
070        }
071    
072        private final ThreadLocal cache = new ThreadLocal() {
073          public Object get() {
074            SoftReference softRef = (SoftReference) super.get();
075            if (softRef == null || softRef.get() == null) {
076              softRef = new SoftReference(new SimpleDateFormat(format));
077              super.set(softRef);
078            }
079            return softRef;
080          }
081        };
082    
083        private DateFormat getDateFormat() {
084          return (DateFormat) ((SoftReference) cache.get()).get();
085        }
086    
087        public StringBuffer format(Date date,StringBuffer toAppendTo, FieldPosition fieldPosition) {
088          return getDateFormat().format(date, toAppendTo, fieldPosition);
089        }
090    
091        public Date parse(String source, ParsePosition pos) {
092          return getDateFormat().parse(source, pos);
093        }
094      }
095    }