From 06770b3ba35667e66028fd969e356bbdae44d55d Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Tue, 8 Mar 2011 18:15:05 +0100 Subject: [PATCH] Add methods formatDate(), formatDateTime(), parseDate() and parseDateTime() to DateUtils --- .../java/org/sonar/api/utils/DateUtils.java | 47 ++++++++++++- .../org/sonar/api/utils/DateUtilsTest.java | 66 +++++++++++++++++++ 2 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java index 510cb0c0161..a77865c2dbf 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java @@ -19,10 +19,51 @@ */ package org.sonar.api.utils; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * @since 2.7 */ -public interface DateUtils { - String DATE_FORMAT = "yyyy-MM-dd"; - String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; +public final class DateUtils { + public static final String DATE_FORMAT = "yyyy-MM-dd"; + public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; + + /** + * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically. + */ + public static String formatDate(Date d) { + return new SimpleDateFormat(DATE_FORMAT).format(d); + } + + /** + * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically. + */ + public static String formatDateTime(Date d) { + return new SimpleDateFormat(DATETIME_FORMAT).format(d); + } + + /** + * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically. + */ + public static Date parseDate(String s) { + return parse(s, DATE_FORMAT); + } + + /** + * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically. + */ + public static Date parseDateTime(String s) { + return parse(s, DATETIME_FORMAT); + } + + private static Date parse(String s, String format) { + try { + return new SimpleDateFormat(format).parse(s); + + } catch (ParseException e) { + throw new SonarException("The date '" + s + "' does not respect format '" + format + "'"); + } + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java new file mode 100644 index 00000000000..8ecebab8dca --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java @@ -0,0 +1,66 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.utils; + +import org.junit.Test; + +import java.util.Date; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.OrderingComparisons.greaterThan; +import static org.hamcrest.text.StringStartsWith.startsWith; +import static org.junit.Assert.assertThat; + +public class DateUtilsTest { + + @Test + public void shouldParseDate() { + Date date = DateUtils.parseDate("2010-05-18"); + assertThat(date.getDate(), is(18)); + } + + @Test(expected = SonarException.class) + public void shouldNotParseDate() { + DateUtils.parseDate("2010/05/18"); + } + + @Test + public void shouldParseDateTime() { + Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100"); + assertThat(date.getMinutes(), is(50)); + } + + @Test(expected = SonarException.class) + public void shouldNotParseDateTime() { + DateUtils.parseDate("2010/05/18 10:55"); + } + + @Test + public void shouldFormatDate() { + assertThat(DateUtils.formatDate(new Date()), startsWith("20")); + assertThat(DateUtils.formatDate(new Date()).length(), is(10)); + } + + @Test + public void shouldFormatDateTime() { + assertThat(DateUtils.formatDateTime(new Date()), startsWith("20")); + assertThat(DateUtils.formatDateTime(new Date()).length(), greaterThan(20)); + } +} -- 2.39.5