001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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.io.NotSerializableException;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.lang.ref.Reference;
026 import java.lang.ref.SoftReference;
027 import java.text.DateFormat;
028 import java.text.FieldPosition;
029 import java.text.ParsePosition;
030 import java.text.SimpleDateFormat;
031 import java.util.Date;
032
033 /**
034 * Parses and formats <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> dates.
035 * This class is thread-safe.
036 *
037 * @since 2.7
038 */
039 public final class DateUtils {
040 public static final String DATE_FORMAT = "yyyy-MM-dd";
041 public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
042
043 private static final ThreadSafeDateFormat THREAD_SAFE_DATE_FORMAT = new ThreadSafeDateFormat(DATE_FORMAT);
044 private static final ThreadSafeDateFormat THREAD_SAFE_DATETIME_FORMAT = new ThreadSafeDateFormat(DATETIME_FORMAT);
045
046 private DateUtils() {
047 }
048
049 public static String formatDate(Date d) {
050 return THREAD_SAFE_DATE_FORMAT.format(d);
051 }
052
053 public static String formatDateTime(Date d) {
054 return THREAD_SAFE_DATETIME_FORMAT.format(d);
055 }
056
057 /**
058 * @param s string in format {@link #DATE_FORMAT}
059 * @throws SonarException when string cannot be parsed
060 */
061 public static Date parseDate(String s) {
062 ParsePosition pos = new ParsePosition(0);
063 Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos);
064 if (pos.getIndex() != s.length()) {
065 throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'");
066 }
067 return result;
068 }
069
070 /**
071 * @param s string in format {@link #DATETIME_FORMAT}
072 * @throws SonarException when string cannot be parsed
073 */
074 public static Date parseDateTime(String s) {
075 ParsePosition pos = new ParsePosition(0);
076 Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos);
077 if (pos.getIndex() != s.length()) {
078 throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'");
079 }
080 return result;
081 }
082
083 static class ThreadSafeDateFormat extends DateFormat {
084 private final String format;
085
086 ThreadSafeDateFormat(String format) {
087 this.format = format;
088 }
089
090 private final ThreadLocal<Reference<DateFormat>> cache = new ThreadLocal<Reference<DateFormat>>() {
091 public Reference<DateFormat> get() {
092 Reference<DateFormat> softRef = super.get();
093 if (softRef == null || softRef.get() == null) {
094 softRef = new SoftReference<DateFormat>(new SimpleDateFormat(format));
095 super.set(softRef);
096 }
097 return softRef;
098 }
099 };
100
101 private DateFormat getDateFormat() {
102 return (DateFormat) ((Reference) cache.get()).get();
103 }
104
105 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
106 return getDateFormat().format(date, toAppendTo, fieldPosition);
107 }
108
109 public Date parse(String source, ParsePosition pos) {
110 return getDateFormat().parse(source, pos);
111 }
112
113 private void readObject(ObjectInputStream ois) throws NotSerializableException {
114 throw new NotSerializableException();
115 }
116
117 private void writeObject(ObjectOutputStream ois) throws NotSerializableException {
118 throw new NotSerializableException();
119 }
120 }
121 }