001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.utils;
021
022import java.io.NotSerializableException;
023import java.io.ObjectInputStream;
024import java.io.ObjectOutputStream;
025import java.lang.ref.Reference;
026import java.lang.ref.SoftReference;
027import java.text.DateFormat;
028import java.text.FieldPosition;
029import java.text.ParsePosition;
030import java.text.SimpleDateFormat;
031import java.util.Date;
032import javax.annotation.CheckForNull;
033import javax.annotation.Nullable;
034
035/**
036 * Parses and formats <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> dates.
037 * This class is thread-safe.
038 *
039 * @since 2.7
040 */
041public final class DateUtils {
042  public static final String DATE_FORMAT = "yyyy-MM-dd";
043  public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
044
045  private static final ThreadSafeDateFormat THREAD_SAFE_DATE_FORMAT = new ThreadSafeDateFormat(DATE_FORMAT);
046  private static final ThreadSafeDateFormat THREAD_SAFE_DATETIME_FORMAT = new ThreadSafeDateFormat(DATETIME_FORMAT);
047
048  private DateUtils() {
049  }
050
051  public static String formatDate(Date d) {
052    return THREAD_SAFE_DATE_FORMAT.format(d);
053  }
054
055  public static String formatDateTime(Date d) {
056    return THREAD_SAFE_DATETIME_FORMAT.format(d);
057  }
058
059  public static String formatDateTime(long ms) {
060    return THREAD_SAFE_DATETIME_FORMAT.format(new Date(ms));
061  }
062
063  public static String formatDateTimeNullSafe(@Nullable Date date) {
064    return date == null ? "" : THREAD_SAFE_DATETIME_FORMAT.format(date);
065  }
066
067  @CheckForNull
068  public static Date longToDate(@Nullable Long time) {
069    return time == null ? null : new Date(time);
070  }
071
072  @CheckForNull
073  public static Long dateToLong(@Nullable Date date) {
074    return date == null ? null : date.getTime();
075  }
076
077  /**
078   * @param s string in format {@link #DATE_FORMAT}
079   * @throws SonarException when string cannot be parsed
080   */
081  public static Date parseDate(String s) {
082    ParsePosition pos = new ParsePosition(0);
083    Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos);
084    if (pos.getIndex() != s.length()) {
085      throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'");
086    }
087    return result;
088  }
089
090  /**
091   * Parse format {@link #DATE_FORMAT}. This method never throws exception.
092   *
093   * @param s any string
094   * @return the date, {@code null} if parsing error or if parameter is {@code null}
095   * @since 3.0
096   */
097  @CheckForNull
098  public static Date parseDateQuietly(@Nullable String s) {
099    Date date = null;
100    if (s != null) {
101      try {
102        date = parseDate(s);
103      } catch (RuntimeException e) {
104        // ignore
105      }
106
107    }
108    return date;
109  }
110
111  /**
112   * @param s string in format {@link #DATETIME_FORMAT}
113   * @throws SonarException when string cannot be parsed
114   */
115
116  public static Date parseDateTime(String s) {
117    ParsePosition pos = new ParsePosition(0);
118    Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos);
119    if (pos.getIndex() != s.length()) {
120      throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'");
121    }
122    return result;
123  }
124
125  /**
126   * Parse format {@link #DATETIME_FORMAT}. This method never throws exception.
127   *
128   * @param s any string
129   * @return the datetime, {@code null} if parsing error or if parameter is {@code null}
130   */
131  @CheckForNull
132  public static Date parseDateTimeQuietly(@Nullable String s) {
133    Date datetime = null;
134    if (s != null) {
135      try {
136        datetime = parseDateTime(s);
137      } catch (RuntimeException e) {
138        // ignore
139      }
140
141    }
142    return datetime;
143  }
144
145  /**
146   * Adds a number of days to a date returning a new object.
147   * The original date object is unchanged.
148   *
149   * @param date  the date, not null
150   * @param numberOfDays  the amount to add, may be negative
151   * @return the new date object with the amount added
152   */
153  public static Date addDays(Date date, int numberOfDays) {
154    return org.apache.commons.lang.time.DateUtils.addDays(date, numberOfDays);
155  }
156
157  static class ThreadSafeDateFormat extends DateFormat {
158    private final String format;
159    private final ThreadLocal<Reference<DateFormat>> cache = new ThreadLocal<Reference<DateFormat>>() {
160      @Override
161      public Reference<DateFormat> get() {
162        Reference<DateFormat> softRef = super.get();
163        if (softRef == null || softRef.get() == null) {
164          SimpleDateFormat sdf = new SimpleDateFormat(format);
165          sdf.setLenient(false);
166          softRef = new SoftReference<DateFormat>(sdf);
167          super.set(softRef);
168        }
169        return softRef;
170      }
171    };
172
173    ThreadSafeDateFormat(String format) {
174      this.format = format;
175    }
176
177    private DateFormat getDateFormat() {
178      return cache.get().get();
179    }
180
181    @Override
182    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
183      return getDateFormat().format(date, toAppendTo, fieldPosition);
184    }
185
186    @Override
187    public Date parse(String source, ParsePosition pos) {
188      return getDateFormat().parse(source, pos);
189    }
190
191    private void readObject(ObjectInputStream ois) throws NotSerializableException {
192      throw new NotSerializableException();
193    }
194
195    private void writeObject(ObjectOutputStream ois) throws NotSerializableException {
196      throw new NotSerializableException();
197    }
198  }
199}