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 static final long serialVersionUID = -8856468429474634301L;
071        private final String format;
072    
073        ThreadSafeDateFormat(String format) {
074          this.format = format;
075        }
076    
077        private final transient ThreadLocal cache = new ThreadLocal() {
078          public Object get() {
079            Reference softRef = (Reference) super.get();
080            if (softRef == null || softRef.get() == null) {
081              softRef = new SoftReference(new SimpleDateFormat(format));
082              super.set(softRef);
083            }
084            return softRef;
085          }
086        };
087    
088        private DateFormat getDateFormat() {
089          return (DateFormat) ((Reference) cache.get()).get();
090        }
091    
092        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
093          return getDateFormat().format(date, toAppendTo, fieldPosition);
094        }
095    
096        public Date parse(String source, ParsePosition pos) {
097          return getDateFormat().parse(source, pos);
098        }
099      }
100    }