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