diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-03-08 18:15:05 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-03-08 18:15:05 +0100 |
commit | 06770b3ba35667e66028fd969e356bbdae44d55d (patch) | |
tree | d00092380582ea5068828101002e221060e5fe13 /sonar-plugin-api | |
parent | 914d329ad4f75b0248c44fdfa0cea958a5388c8b (diff) | |
download | sonarqube-06770b3ba35667e66028fd969e356bbdae44d55d.tar.gz sonarqube-06770b3ba35667e66028fd969e356bbdae44d55d.zip |
Add methods formatDate(), formatDateTime(), parseDate() and parseDateTime() to DateUtils
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java | 47 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java | 66 |
2 files changed, 110 insertions, 3 deletions
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)); + } +} |