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