List of usage examples for java.time ZonedDateTime parse
public static ZonedDateTime parse(CharSequence text)
From source file:msi.gama.util.GamaDate.java
private static Temporal parse(final IScope scope, final String original, final DateTimeFormatter df) { if (original == null || original.isEmpty() || original.equals("now")) { return LocalDateTime.now(GamaDateType.DEFAULT_ZONE); }/*from w ww . java 2 s . c o m*/ Temporal result = null; if (df != null) { try { final TemporalAccessor ta = df.parse(original); if (ta instanceof Temporal) { return (Temporal) ta; } if (!ta.isSupported(ChronoField.YEAR) && !ta.isSupported(ChronoField.MONTH_OF_YEAR) && !ta.isSupported(ChronoField.DAY_OF_MONTH)) { if (ta.isSupported(ChronoField.HOUR_OF_DAY)) { return LocalTime.from(ta); } } if (!ta.isSupported(ChronoField.HOUR_OF_DAY) && !ta.isSupported(ChronoField.MINUTE_OF_HOUR) && !ta.isSupported(ChronoField.SECOND_OF_MINUTE)) { return LocalDate.from(ta); } return LocalDateTime.from(ta); } catch (final DateTimeParseException e) { e.printStackTrace(); } GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning( "The date " + original + " can not correctly be parsed by the pattern provided", scope), false); return parse(scope, original, null); } String dateStr; try { // We first make sure all date fields have the correct length and // the string is correctly formatted String string = original; if (!original.contains("T") && original.contains(" ")) { string = StringUtils.replaceOnce(original, " ", "T"); } final String[] base = string.split("T"); final String[] date = base[0].split("-"); String other; if (base.length == 1) { other = "00:00:00"; } else { other = base[1]; } String year, month, day; if (date.length == 1) { // ISO basic date format year = date[0].substring(0, 4); month = date[0].substring(4, 6); day = date[0].substring(6, 8); } else { year = date[0]; month = date[1]; day = date[2]; } if (year.length() == 2) { year = "20" + year; } if (month.length() == 1) { month = '0' + month; } if (day.length() == 1) { day = '0' + day; } dateStr = year + "-" + month + "-" + day + "T" + other; } catch (final Exception e1) { throw GamaRuntimeException.error("The date " + original + " is not correctly formatted. Please refer to the ISO date/time format", scope); } try { result = LocalDateTime.parse(dateStr); } catch (final DateTimeParseException e) { try { result = OffsetDateTime.parse(dateStr); } catch (final DateTimeParseException e2) { try { result = ZonedDateTime.parse(dateStr); } catch (final DateTimeParseException e3) { throw GamaRuntimeException.error( "The date " + original + " is not correctly formatted. Please refer to the ISO date/time format", scope); } } } return result; }
From source file:ddf.catalog.registry.transformer.RegistryTransformerTest.java
@Test public void testFullRegistryPackage() throws Exception { MetacardImpl metacard = convert("/csw-full-registry-package.xml"); Date date = Date.from(ZonedDateTime.parse("2015-11-01T06:15:30-07:00").toInstant()); assertThat(metacard.getAttribute(RegistryObjectMetacardType.LIVE_DATE).getValue(), is(date)); date = Date.from(ZonedDateTime.parse("2015-11-01T13:15:30Z").toInstant()); assertThat(metacard.getAttribute(RegistryObjectMetacardType.DATA_START_DATE).getValue(), is(date)); date = Date.from(ZonedDateTime.parse("2015-12-01T23:01:40Z").toInstant()); assertThat(metacard.getAttribute(RegistryObjectMetacardType.DATA_END_DATE).getValue(), is(date)); date = Date.from(ZonedDateTime.parse("2016-01-26T17:16:34.996Z").toInstant()); assertThat(metacard.getAttribute(Metacard.MODIFIED).getValue(), is(date)); assertThat(metacard.getAttribute(RegistryObjectMetacardType.LINKS).getValue(), is("https://some/link/to/my/repo")); assertThat(metacard.getAttribute(Metacard.GEOGRAPHY).getValue(), is("POINT (112.267472 33.467944)")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.REGION).getValue(), is("USA")); List<Serializable> serializableList = metacard.getAttribute(RegistryObjectMetacardType.DATA_SOURCES) .getValues();/* w w w.j ava2 s .c om*/ assertThat(serializableList.size(), is(2)); assertThat(serializableList, hasItem("youtube")); assertThat(serializableList, hasItem("myCamera")); serializableList = metacard.getAttribute(RegistryObjectMetacardType.DATA_TYPES).getValues(); assertThat(serializableList.size(), is(2)); assertThat(serializableList, hasItem("video")); assertThat(serializableList, hasItem("sensor")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.SECURITY_LEVEL).getValue(), is("role=guest")); assertThat(metacard.getAttribute(Metacard.TITLE).getValue(), is("Node Name")); assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValue(), is("A little something describing this node in less than 1024 characters")); assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE_VERSION).getValue(), is("2.9.x")); serializableList = metacard.getAttribute(RegistryObjectMetacardType.SERVICE_BINDING_TYPES).getValues(); assertThat(serializableList.size(), is(2)); assertThat(serializableList, hasItem("Csw_Federated_Source")); assertThat(serializableList, hasItem("soap13")); serializableList = metacard.getAttribute(RegistryObjectMetacardType.SERVICE_BINDINGS).getValues(); assertThat(serializableList.size(), is(2)); assertThat(serializableList, hasItem("REST")); assertThat(serializableList, hasItem("SOAP")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.ORGANIZATION_NAME).getValue(), is("Codice")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.ORGANIZATION_EMAIL).getValue(), is("emailaddress@something.com")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.ORGANIZATION_PHONE_NUMBER).getValue(), is("(555) 555-5555 extension 1234")); assertThat(metacard.getAttribute(RegistryObjectMetacardType.ORGANIZATION_ADDRESS).getValue(), is("1234 Some Street, Phoenix, AZ 85037, USA")); assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValue(), is("john doe, (111) 111-1111 extension 1234, emailaddress@something.com")); }
From source file:org.apache.james.jmap.methods.integration.GetVacationResponseTest.java
@Test public void accountIdIsNotSupported() { jmapGuiceProbe.modifyVacation(AccountId.fromString(USER), VacationPatch.builder().isEnabled(true).fromDate(ZonedDateTime.parse("2014-09-30T14:10:00+02:00")) .toDate(ZonedDateTime.parse("2014-10-30T14:10:00+02:00")) .textBody("Test explaining my vacations").build()); given().header("Authorization", accessToken.serialize()) .body("[[" + "\"getVacationResponse\", " + "{\"accountId\":\"1\"}, " + "\"#0\"" + "]]").when() .post("/jmap").then().statusCode(200).body(NAME, equalTo("error")) .body(ARGUMENTS + ".type", equalTo("Not yet implemented")); }
From source file:org.apache.james.jmap.methods.integration.SetVacationResponseTest.java
@Test public void setVacationResponseShouldHandleNamedTimeZone() { String bodyRequest = "[[" + "\"setVacationResponse\", " + "{" + "\"update\":{" + "\"singleton\" : {" + "\"id\": \"singleton\"," + "\"isEnabled\": \"true\"," + "\"textBody\": \"Message explaining my wonderful vacations\"," + "\"fromDate\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\"," + "\"toDate\":\"2016-04-07T02:01+07:00[Asia/Vientiane]\"" + "}" + "}" + "}, " + "\"#0\"" + "]]"; given().header("Authorization", accessToken.serialize()).body(bodyRequest).when().post("/jmap").then() .statusCode(200).body(NAME, equalTo("vacationResponseSet")) .body(ARGUMENTS + ".updated[0]", equalTo("singleton")); Vacation vacation = jmapGuiceProbe.retrieveVacation(AccountId.fromString(USER)); assertThat(vacation.getTextBody()).contains("Message explaining my wonderful vacations"); assertThat(vacation.isEnabled()).isTrue(); assertThat(vacation.getFromDate()).contains(ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]")); assertThat(vacation.getToDate()).contains(ZonedDateTime.parse("2016-04-07T02:01+07:00[Asia/Vientiane]")); }
From source file:org.codice.ddf.registry.transformer.RegistryTransformerTest.java
@Test public void testFullRegistryPackage() throws Exception { MetacardImpl metacard = convert("/csw-full-registry-package.xml"); Date date = Date.from(ZonedDateTime.parse("2015-11-01T06:15:30-07:00").toInstant()); assertThat(RegistryUtility.getStringAttribute(metacard, Core.CREATED, null), is(date.toString())); date = Date.from(ZonedDateTime.parse("2015-11-01T13:15:30Z").toInstant()); assertThat(RegistryUtility.getStringAttribute(metacard, DateTime.START, null), is(date.toString())); date = Date.from(ZonedDateTime.parse("2015-12-01T23:01:40Z").toInstant()); assertThat(RegistryUtility.getStringAttribute(metacard, DateTime.END, null), is(date.toString())); date = Date.from(ZonedDateTime.parse("2016-01-26T17:16:34.996Z").toInstant()); assertThat(RegistryUtility.getStringAttribute(metacard, Core.MODIFIED, null), is(date.toString())); assertThat(RegistryUtility.getStringAttribute(metacard, RegistryObjectMetacardType.LINKS, null), is("https://some/link/to/my/repo")); assertThat(RegistryUtility.getStringAttribute(metacard, Core.LOCATION, null), is("POINT (112.267472 33.467944)")); assertThat(RegistryUtility.getStringAttribute(metacard, RegistryObjectMetacardType.REGION, null), is("USA")); List<String> attributeValuesList = RegistryUtility.getListOfStringAttribute(metacard, RegistryObjectMetacardType.DATA_SOURCES); assertThat(attributeValuesList.size(), is(2)); assertThat(attributeValuesList, hasItem("youtube")); assertThat(attributeValuesList, hasItem("myCamera")); attributeValuesList = RegistryUtility.getListOfStringAttribute(metacard, Topic.KEYWORD); assertThat(attributeValuesList.size(), is(2)); assertThat(attributeValuesList, hasItem("video")); assertThat(attributeValuesList, hasItem("sensor")); assertThat(RegistryUtility.getStringAttribute(metacard, RegistryObjectMetacardType.SECURITY_LEVEL, null), is("role=guest")); assertThat(RegistryUtility.getStringAttribute(metacard, Metacard.TITLE, null), is("Node Name")); assertThat(RegistryUtility.getStringAttribute(metacard, Metacard.DESCRIPTION, null), is("A little something describing this node in less than 1024 characters")); assertThat(RegistryUtility.getStringAttribute(metacard, Metacard.CONTENT_TYPE_VERSION, null), is("2.9.x")); attributeValuesList = RegistryUtility.getListOfStringAttribute(metacard, RegistryObjectMetacardType.SERVICE_BINDING_TYPES); assertThat(attributeValuesList.size(), is(2)); assertThat(attributeValuesList, hasItem("Csw_Federated_Source")); assertThat(attributeValuesList, hasItem("soap13")); attributeValuesList = RegistryUtility.getListOfStringAttribute(metacard, RegistryObjectMetacardType.SERVICE_BINDINGS); assertThat(attributeValuesList.size(), is(2)); assertThat(attributeValuesList, hasItem("REST")); assertThat(attributeValuesList, hasItem("SOAP")); assertThat(RegistryUtility.getStringAttribute(metacard, Contact.POINT_OF_CONTACT_NAME, null), is("Codice")); assertThat(RegistryUtility.getListOfStringAttribute(metacard, Contact.POINT_OF_CONTACT_ADDRESS), hasItem("1234 Some Street, Phoenix, AZ 85037, USA")); assertThat(RegistryUtility.getListOfStringAttribute(metacard, Contact.POINT_OF_CONTACT_PHONE), hasItem("(555) 555-5555 ext 1234")); assertThat(RegistryUtility.getListOfStringAttribute(metacard, Contact.POINT_OF_CONTACT_EMAIL), hasItem("emailaddress@something.com")); assertThat(RegistryUtility.getStringAttribute(metacard, Metacard.POINT_OF_CONTACT, null), is("john doe, (111) 111-1111 ext 1234, emailaddress@something.com")); }
From source file:org.silverpeas.core.calendar.CalendarEventOccurrenceGenerationTest.java
@Test public void nextOccurrenceAboutNonRecurrentOneDayEventShouldWork() { CalendarEvent event = calendarEventForTest(Period.between(date(2017, 12, 12), date(2017, 12, 12))); ZonedDateTime from = ZonedDateTime.parse("2017-12-12T00:00:00+01:00"); CalendarEventOccurrence result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue());/*from www . j a v a2 s . c o m*/ from = ZonedDateTime.parse("2017-12-12T00:00:00-01:00"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue()); from = ZonedDateTime.parse("2017-12-11T23:59:59+10:00"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue()); assertThat(result.getStartDate(), is(date(2017, 12, 12))); assertThat(result.getEndDate(), is(date(2017, 12, 13))); from = ZonedDateTime.parse("2017-12-11T23:59:59Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue()); assertThat(result.getStartDate(), is(date(2017, 12, 12))); assertThat(result.getEndDate(), is(date(2017, 12, 13))); from = ZonedDateTime.parse("2017-12-12T00:00:00Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue()); }
From source file:org.silverpeas.core.calendar.CalendarEventOccurrenceGenerationTest.java
@Test public void nextOccurrenceAboutNonRecurrentSeveralDayEventShouldWork() { CalendarEvent event = calendarEventForTest(Period.between(date(2017, 12, 12), date(2017, 12, 15))); ZonedDateTime from = ZonedDateTime.parse("2017-12-12T00:00:00+01:00"); CalendarEventOccurrence result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue());/* w ww . j a v a2s. com*/ from = ZonedDateTime.parse("2017-12-12T00:00:00-01:00"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue()); from = ZonedDateTime.parse("2017-12-11T23:59:59+12:00"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue()); assertThat(result.getStartDate(), is(date(2017, 12, 12))); assertThat(result.getEndDate(), is(date(2017, 12, 15))); from = ZonedDateTime.parse("2017-12-11T23:59:59Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue()); assertThat(result.getStartDate(), is(date(2017, 12, 12))); assertThat(result.getEndDate(), is(date(2017, 12, 15))); from = ZonedDateTime.parse("2017-12-12T00:00:00Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue()); }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionDateTime.java
/** * test apply-days minus// w w w .j a v a 2 s. c om */ @Test @UseDataProvider("generateapplyminus") public final void applysminus(final Triple<IAction, Pair<ZonedDateTime, Integer>, String> p_value) { final List<ITerm> l_return = new ArrayList<>(); p_value.getLeft().execute(false, IContext.EMPTYPLAN, Stream.of("minus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft()) .map(CRawTerm::from).collect(Collectors.toList()), l_return); Assert.assertEquals(l_return.size(), 1); Assert.assertEquals(l_return.get(0).raw(), ZonedDateTime.parse(p_value.getRight())); }
From source file:org.silverpeas.core.calendar.CalendarEventOccurrenceGenerationTest.java
@Test public void nextOccurrenceAboutNonRecurrentHourEventOnOneDayShouldWork() { CalendarEvent event = calendarEventForTest( Period.between(dateTimeInUTC(2017, 12, 12, 13, 30), dateTimeInUTC(2017, 12, 12, 14, 45))); ZonedDateTime from = ZonedDateTime.parse("2017-12-12T13:30:00+01:00"); CalendarEventOccurrence result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue());/* ww w . j a v a2 s. co m*/ assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 12, 12, 13, 30))); assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 12, 12, 14, 45))); from = ZonedDateTime.parse("2017-12-12T13:29:59Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, notNullValue()); assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 12, 12, 13, 30))); assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 12, 12, 14, 45))); from = ZonedDateTime.parse("2017-12-12T13:30:00Z"); result = generator.generateNextOccurrenceOf(event, from); assertThat(result, nullValue()); }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionDateTime.java
/** * test apply-days plus//ww w .ja v a 2 s. c o m */ @Test @UseDataProvider("generateapplyplus") public final void applyplus(final Triple<IAction, Pair<ZonedDateTime, Integer>, String> p_value) { final List<ITerm> l_return = new ArrayList<>(); p_value.getLeft().execute(false, IContext.EMPTYPLAN, Stream.of("plus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft()).map(CRawTerm::from) .collect(Collectors.toList()), l_return); Assert.assertEquals(l_return.size(), 1); Assert.assertEquals(l_return.get(0).raw(), ZonedDateTime.parse(p_value.getRight())); }