*/
package org.sonar.api.utils;
+import javax.annotation.Nullable;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
return result;
}
+ /**
+ * Parse format {@link #DATE_FORMAT}. This method never throws exception.
+ *
+ * @param s any string
+ * @return the date, null if parsing error or null string
+ * @since 2.15
+ */
+ public static Date parseDateQuietly(@Nullable String s) {
+ Date date = null;
+ if (s != null) {
+ try {
+ date = parseDate(s);
+ } catch (RuntimeException e) {
+ // ignore
+ }
+
+ }
+ return date;
+ }
+
/**
* @param s string in format {@link #DATETIME_FORMAT}
* @throws SonarException when string cannot be parsed
*/
+
public static Date parseDateTime(String s) {
ParsePosition pos = new ParsePosition(0);
Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos);
return result;
}
+ /**
+ * Parse format {@link #DATETIME_FORMAT}. This method never throws exception.
+ *
+ * @param s any string
+ * @return the datetime, null if parsing error or null string
+ */
+ public static Date parseDateTimeQuietly(@Nullable String s) {
+ Date datetime = null;
+ if (s != null) {
+ try {
+ datetime = parseDateTime(s);
+ } catch (RuntimeException e) {
+ // ignore
+ }
+
+ }
+ return datetime;
+ }
+
static class ThreadSafeDateFormat extends DateFormat {
private final String format;
package org.sonar.api.utils;
import com.google.common.collect.Lists;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import java.util.Date;
import java.util.List;
public class DateUtilsTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
- public void shouldParseDate() {
+ public void parseDate_valid_format() {
Date date = DateUtils.parseDate("2010-05-18");
assertThat(date.getDate(), is(18));
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDate() {
+ @Test
+ public void parseDate_not_valid_format() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18");
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDateIfAdditionnalCharacters() {
+ @Test
+ public void parseDateQuietly() {
+ assertNull(DateUtils.parseDateQuietly("2010/05/18"));
+ Date date = DateUtils.parseDateQuietly("2010-05-18");
+ assertThat(date.getDate(), is(18));
+ }
+
+ @Test
+ public void parseDate_fail_if_additional_characters() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("1986-12-04foo");
}
@Test
- public void shouldParseDateTime() {
+ public void parseDateTime_valid_format() {
Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100");
assertThat(date.getMinutes(), is(50));
}
- @Test(expected = SonarException.class)
- public void shouldNotParseDateTime() {
+ @Test
+ public void parseDateTime_not_valid_format() {
+ thrown.expect(SonarException.class);
DateUtils.parseDate("2010/05/18 10:55");
}
- @Test(expected = SonarException.class)
- public void shouldNotParseIfAdditionnalCharacters() {
+ @Test
+ public void parseDateTime_fail_if_additional_characters() {
+ thrown.expect(SonarException.class);
DateUtils.parseDateTime("1986-12-04T01:02:03+0300foo");
}
+ @Test
+ public void parseDateTimeQuietly() {
+ assertNull(DateUtils.parseDateTimeQuietly("2010/05/18 10:55"));
+ Date date = DateUtils.parseDateTimeQuietly("2010-05-18T15:50:45+0100");
+ assertThat(date.getMinutes(), is(50));
+ }
+
@Test
public void shouldFormatDate() {
assertThat(DateUtils.formatDate(new Date()), startsWith("20"));
* See http://koders.com/java/fid8A231D75F2C6E6909FB26BCA11C12D08AD05FB50.aspx?s=ThreadSafeDateFormatTest
*/
@Test
- public void shouldBeThreadSafe() throws InterruptedException {
-
+ public void shouldBeThreadSafe() throws Exception {
final DateUtils.ThreadSafeDateFormat format = new DateUtils.ThreadSafeDateFormat("yyyy-MM-dd'T'HH:mm:ss,S z");
final Date now = new Date();
final List<Throwable> throwables = Lists.newArrayList();