Java Code Examples for org.joda.time.format.ISODateTimeFormat#dateTimeParser()

The following examples show how to use org.joda.time.format.ISODateTimeFormat#dateTimeParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSyslogParser.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testRfc5424DateParsing() {
  final String[] examples = {
    "1985-04-12T23:20:50.52Z", "1985-04-12T19:20:50.52-04:00",
    "2003-10-11T22:14:15.003Z", "2003-08-24T05:14:15.000003-07:00",
    "2012-04-13T11:11:11-08:00", "2012-04-13T08:08:08.0001+00:00"
  };

  SyslogParser parser = new SyslogParser();
  DateTimeFormatter jodaParser = ISODateTimeFormat.dateTimeParser();

  for (String ex : examples) {
    Assert.assertEquals(
        "Problem parsing date string: " + ex,
        jodaParser.parseMillis(ex),
        parser.parseRfc5424Date(ex));
  }
}
 
Example 2
Source File: TestBucketPath.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateRace() {
  Clock mockClock = mock(Clock.class);
  DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
  long two = parser.parseMillis("2013-04-21T02:59:59-00:00");
  long three = parser.parseMillis("2013-04-21T03:00:00-00:00");
  when(mockClock.currentTimeMillis()).thenReturn(two, three);

  // save & modify static state (yuck)
  Clock origClock = BucketPath.getClock();
  BucketPath.setClock(mockClock);

  String pat = "%H:%M";
  String escaped = BucketPath.escapeString(pat,
      new HashMap<String, String>(),
      TimeZone.getTimeZone("UTC"), true, Calendar.MINUTE, 10, true);

  // restore static state
  BucketPath.setClock(origClock);

  Assert.assertEquals("Race condition detected", "02:50", escaped);
}
 
Example 3
Source File: GitLabManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static java.util.Date convertStringToDate(String isoDate) {
	
	isoDate = isoDate.replaceAll("\"", "");
	DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
	DateTime date = parser.parseDateTime(isoDate);
	return date.toDate();
}
 
Example 4
Source File: StructuredSyslogServerEvent.java    From syslog4j-graylog2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public DateTimeFormatter getDateTimeFormatter() {
    if (dateTimeFormatter == null) {
        this.dateTimeFormatter = ISODateTimeFormat.dateTimeParser();
    }

    return dateTimeFormatter;
}
 
Example 5
Source File: SoapTransportCommandProcessorTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
/**
 * Tests List and Date parameters in and out
 * @throws Exception
 */
@Test
public void testProcess_ListDate() throws Exception {
	// Set up the input
	when(request.getInputStream()).thenReturn(
			new TestServletInputStream(buildSoapMessage(null, listOpIn, null, null)));
       when(request.getScheme()).thenReturn("http");

	// Resolve the input command
	soapCommandProcessor.process(command);
	assertEquals(1, ev.getInvokedCount());

	DateTimeFormatter xmlFormat = ISODateTimeFormat.dateTimeParser();

	// Assert that we resolved the expected arguments
	Object[] args = ev.getArgs();
	assertNotNull(args);
	assertEquals(1, args.length);
	List<Date> listArg = (List<Date>)args[0];
	assertEquals(2, listArg.size());
	assertEquals(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297+01:00").toDate(), listArg.get(0));
	assertEquals(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297Z").toDate(), listArg.get(1));

	// Assert that the expected result is sent
	assertNotNull(ev.getObserver());
	List<Date> response = new ArrayList<Date>();
	response.add(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297+01:00").toDate());
	response.add(xmlFormat.parseDateTime("248556211-09-30T12:12:53.297Z").toDate());

	ev.getObserver().onResult(new ExecutionResult(response));
	assertEquals(CommandStatus.Complete, command.getStatus());
	assertSoapyEquals(buildSoapMessage(null, listOpOut, null, null), testOut.getOutput());
       verify(logger).logAccess(eq(command), isA(ExecutionContext.class), anyLong(), anyLong(),
                                           any(MediaType.class), any(MediaType.class), any(ResponseCode.class));

       verifyTracerCalls(listOpKey);
}
 
Example 6
Source File: ISODateParser.java    From hangout with MIT License 5 votes vote down vote up
public ISODateParser(String timezone) {

		this.formatter = ISODateTimeFormat.dateTimeParser();

		if (timezone != null) {
			this.formatter = this.formatter.withZone(DateTimeZone
					.forID(timezone));
		} else {
			this.formatter = this.formatter.withOffsetParsed();
		}
	}
 
Example 7
Source File: BitbucketManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static java.util.Date convertStringToDate(String isoDate) {

		isoDate = isoDate.replaceAll("\"", "");
		DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
		DateTime date = parser.parseDateTime(isoDate);
		return date.toDate();
	}
 
Example 8
Source File: BitbucketIssue.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static Date convertStringToDate(String isoDate) {

		isoDate = isoDate.replaceAll("\"", "");
		DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
		DateTime date = parser.parseDateTime(isoDate);
		return date.toDate();
	}
 
Example 9
Source File: GitLabComment.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private static java.util.Date convertStringToDate(String isoDate) {
	
	isoDate = isoDate.replaceAll("\"", "");
	DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
	DateTime date = parser.parseDateTime(isoDate);
	return date.toDate();
}
 
Example 10
Source File: GitLabIssue.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private static java.util.Date convertStringToDate(String isoDate) {
	
	if(isoDate.isEmpty())
		return null;
	isoDate = isoDate.replaceAll("\"", "");
	DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
	DateTime date = parser.parseDateTime(isoDate);
	return date.toDate();
}
 
Example 11
Source File: MantisReaderUtils.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public static Date convertStringToDate(String isoDate) {
	isoDate = isoDate.replaceAll("\"", "");
	DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
	DateTime date = parser.parseDateTime(isoDate);
	return date.toDate();
}
 
Example 12
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 13
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 14
Source File: TimestampOptionHandler.java    From newts with Apache License 2.0 4 votes vote down vote up
@Override
protected Timestamp parse(String argument) throws NumberFormatException, CmdLineException {
    DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
    DateTime dateTime = parser.parseDateTime(argument);
    return Timestamp.fromEpochMillis(dateTime.getMillis());
}
 
Example 15
Source File: GitHubReaderUtils.java    From scava with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Converts ISO 8601 Timestamp from String to Java.util.Date
 * 
 * @param isoDate ISO 8601 in string format. For example '2014-11-07T22:01:45Z' (this has the time zone omitted hence 'Z')
 * @return date Java.util.Date
 * @throws Exception
 */
public static Date convertStringToDate(String isoDate){
	DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
	DateTime date = parser.parseDateTime(isoDate);
	return date.toDate();
}
 
Example 16
Source File: StringConverter.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the millis, which is the ISO parsed string value.
 * 
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, non-null result of getChronology
 * @return the millisecond value
 * @throws IllegalArgumentException if the value if invalid
 */
public long getInstantMillis(Object object, Chronology chrono) {
    String str = (String) object;
    DateTimeFormatter p = ISODateTimeFormat.dateTimeParser();
    return p.withChronology(chrono).parseMillis(str);
}
 
Example 17
Source File: StringConverter.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the millis, which is the ISO parsed string value.
 * 
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, non-null result of getChronology
 * @return the millisecond value
 * @throws IllegalArgumentException if the value if invalid
 */
public long getInstantMillis(Object object, Chronology chrono) {
    String str = (String) object;
    DateTimeFormatter p = ISODateTimeFormat.dateTimeParser();
    return p.withChronology(chrono).parseMillis(str);
}