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 javax.annotation.CheckForNull; 023import javax.annotation.Nullable; 024 025import java.io.NotSerializableException; 026import java.io.ObjectInputStream; 027import java.io.ObjectOutputStream; 028import java.lang.ref.Reference; 029import java.lang.ref.SoftReference; 030import java.text.DateFormat; 031import java.text.FieldPosition; 032import java.text.ParsePosition; 033import java.text.SimpleDateFormat; 034import java.util.Date; 035 036/** 037 * Parses and formats <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> dates. 038 * This class is thread-safe. 039 * 040 * @since 2.7 041 */ 042public final class DateUtils { 043 public static final String DATE_FORMAT = "yyyy-MM-dd"; 044 public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; 045 046 private static final ThreadSafeDateFormat THREAD_SAFE_DATE_FORMAT = new ThreadSafeDateFormat(DATE_FORMAT); 047 private static final ThreadSafeDateFormat THREAD_SAFE_DATETIME_FORMAT = new ThreadSafeDateFormat(DATETIME_FORMAT); 048 049 private DateUtils() { 050 } 051 052 public static String formatDate(Date d) { 053 return THREAD_SAFE_DATE_FORMAT.format(d); 054 } 055 056 public static String formatDateTime(Date d) { 057 return THREAD_SAFE_DATETIME_FORMAT.format(d); 058 } 059 060 public static String formatDateTimeNullSafe(@Nullable Date date) { 061 return date == null ? "" : THREAD_SAFE_DATETIME_FORMAT.format(date); 062 } 063 064 @CheckForNull 065 public static Date longToDate(@Nullable Long time) { 066 return time == null ? null : new Date(time); 067 } 068 069 @CheckForNull 070 public static Long dateToLong(@Nullable Date date) { 071 return date == null ? null : date.getTime(); 072 } 073 074 /** 075 * @param s string in format {@link #DATE_FORMAT} 076 * @throws SonarException when string cannot be parsed 077 */ 078 public static Date parseDate(String s) { 079 ParsePosition pos = new ParsePosition(0); 080 Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos); 081 if (pos.getIndex() != s.length()) { 082 throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'"); 083 } 084 return result; 085 } 086 087 /** 088 * Parse format {@link #DATE_FORMAT}. This method never throws exception. 089 * 090 * @param s any string 091 * @return the date, null if parsing error or null string 092 * @since 3.0 093 */ 094 public static Date parseDateQuietly(@Nullable String s) { 095 Date date = null; 096 if (s != null) { 097 try { 098 date = parseDate(s); 099 } catch (RuntimeException e) { 100 // ignore 101 } 102 103 } 104 return date; 105 } 106 107 /** 108 * @param s string in format {@link #DATETIME_FORMAT} 109 * @throws SonarException when string cannot be parsed 110 */ 111 112 public static Date parseDateTime(String s) { 113 ParsePosition pos = new ParsePosition(0); 114 Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos); 115 if (pos.getIndex() != s.length()) { 116 throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'"); 117 } 118 return result; 119 } 120 121 /** 122 * Parse format {@link #DATETIME_FORMAT}. This method never throws exception. 123 * 124 * @param s any string 125 * @return the datetime, null if parsing error or null string 126 */ 127 public static Date parseDateTimeQuietly(@Nullable String s) { 128 Date datetime = null; 129 if (s != null) { 130 try { 131 datetime = parseDateTime(s); 132 } catch (RuntimeException e) { 133 // ignore 134 } 135 136 } 137 return datetime; 138 } 139 140 public static Date addDays(Date date, int numberOfDays) { 141 return org.apache.commons.lang.time.DateUtils.addDays(date, numberOfDays); 142 } 143 144 static class ThreadSafeDateFormat extends DateFormat { 145 private final String format; 146 private final ThreadLocal<Reference<DateFormat>> cache = new ThreadLocal<Reference<DateFormat>>() { 147 @Override 148 public Reference<DateFormat> get() { 149 Reference<DateFormat> softRef = super.get(); 150 if (softRef == null || softRef.get() == null) { 151 softRef = new SoftReference<DateFormat>(new SimpleDateFormat(format)); 152 super.set(softRef); 153 } 154 return softRef; 155 } 156 }; 157 158 ThreadSafeDateFormat(String format) { 159 this.format = format; 160 } 161 162 private DateFormat getDateFormat() { 163 return (DateFormat) ((Reference) cache.get()).get(); 164 } 165 166 @Override 167 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) { 168 return getDateFormat().format(date, toAppendTo, fieldPosition); 169 } 170 171 @Override 172 public Date parse(String source, ParsePosition pos) { 173 return getDateFormat().parse(source, pos); 174 } 175 176 private void readObject(ObjectInputStream ois) throws NotSerializableException { 177 throw new NotSerializableException(); 178 } 179 180 private void writeObject(ObjectOutputStream ois) throws NotSerializableException { 181 throw new NotSerializableException(); 182 } 183 } 184}