001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 public static Date addDays(Date date, int numberOfDays) { 146 return org.apache.commons.lang.time.DateUtils.addDays(date, numberOfDays); 147 } 148 149 static class ThreadSafeDateFormat extends DateFormat { 150 private final String format; 151 private final ThreadLocal<Reference<DateFormat>> cache = new ThreadLocal<Reference<DateFormat>>() { 152 @Override 153 public Reference<DateFormat> get() { 154 Reference<DateFormat> softRef = super.get(); 155 if (softRef == null || softRef.get() == null) { 156 SimpleDateFormat sdf = new SimpleDateFormat(format); 157 sdf.setLenient(false); 158 softRef = new SoftReference<DateFormat>(sdf); 159 super.set(softRef); 160 } 161 return softRef; 162 } 163 }; 164 165 ThreadSafeDateFormat(String format) { 166 this.format = format; 167 } 168 169 private DateFormat getDateFormat() { 170 return (DateFormat) ((Reference) cache.get()).get(); 171 } 172 173 @Override 174 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) { 175 return getDateFormat().format(date, toAppendTo, fieldPosition); 176 } 177 178 @Override 179 public Date parse(String source, ParsePosition pos) { 180 return getDateFormat().parse(source, pos); 181 } 182 183 private void readObject(ObjectInputStream ois) throws NotSerializableException { 184 throw new NotSerializableException(); 185 } 186 187 private void writeObject(ObjectOutputStream ois) throws NotSerializableException { 188 throw new NotSerializableException(); 189 } 190 } 191}