diff --git a/src/main/java/org/cactoos/time/DateOf.java b/src/main/java/org/cactoos/time/DateOf.java index c6df6efc18..8f1d8dcf86 100644 --- a/src/main/java/org/cactoos/time/DateOf.java +++ b/src/main/java/org/cactoos/time/DateOf.java @@ -24,222 +24,63 @@ package org.cactoos.time; import java.time.LocalDateTime; -import java.time.OffsetDateTime; -import java.time.ZoneId; import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; -import org.cactoos.Func; +import java.util.Date; import org.cactoos.Scalar; import org.cactoos.scalar.UncheckedScalar; /** - * Parser for date instances. + * Parser for {@link Date} instances. * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) * @version $Id$ - * @param Type of the date to parse to. * @since 1.0 */ -public class DateOf implements Scalar { +public class DateOf implements Scalar { /** * The parsed date. */ - private UncheckedScalar parsed; + private Scalar parsed; /** - * Parses the date using the provided format. + * Parses the provided date as ISO formatted. * @param date The date to parse. - * @param format The format to use. - * @param converter The converter to convert * {@link TemporalAccessor} into type T */ - public DateOf(final String date, final String format, - final Func converter) { - this(date, DateTimeFormatter.ofPattern(format), converter); + public DateOf(final String date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); } /** - * Parses the provided date as ISO formatted. + * Parses the date using the provided format. * @param date The date to parse. - * @param converter The converter to convert + * @param format The format to use. * {@link TemporalAccessor} into type T */ - public DateOf(final String date, - final Func converter) { - this(date, DateTimeFormatter.ISO_DATE_TIME, converter); + public DateOf(final String date, final String format) { + this(date, DateTimeFormatter.ofPattern(format)); } /** * Parsing the date using the provided formatter. * @param date The date to parse. * @param formatter The formatter to use. - * @param converter The converter to convert * {@link TemporalAccessor} into type T */ - public DateOf(final String date, final DateTimeFormatter formatter, - final Func converter) { + public DateOf(final String date, final DateTimeFormatter formatter) { this.parsed = new UncheckedScalar<>( - () -> converter.apply(formatter.parse(date)) + () -> Date.from( + LocalDateTime.from(formatter.parse(date)) + .toInstant(ZoneOffset.UTC) + ) ); } @Override - public final T value() throws Exception { + public final Date value() throws Exception { return this.parsed.value(); } - /** - * Convenience class for parsing to {@link java.util.Date} instances. - */ - public static class Date implements Scalar { - /** - * Internal parser to create {@link LocalDateTime} instance. - */ - private final Local internal; - /** - * Parses ISO date string to create {@link java.util.Date} instances. - * @param date The date to parse. - */ - public Date(final String date) { - this(new Local(date)); - } - /** - * Parses date using the provided format to create - * {@link java.util.Date} instances. - * @param date The date to parse. - * @param format The format to use. - */ - public Date(final String date, final String format) { - this(new Local(date, format)); - } - /** - * Parses the date using the formatter to create - * {@link java.util.Date} instances. - * @param date The date to parse. - * @param formatter The formatter to use. - */ - public Date(final String date, final DateTimeFormatter formatter) { - this(new Local(date, formatter)); - } - /** - * Private stor. - * @param local The {@link Local} to use. - */ - private Date(final Local local) { - this.internal = local; - } - - @Override - public final java.util.Date value() throws Exception { - return java.util.Date.from( - this.internal.value().toInstant(ZoneOffset.UTC) - ); - } - } - /** - * Convenience class for parsing to {@link LocalDateTime} instances. - */ - public static class Local extends DateOf { - /** - * Parses date using the provided format to create - * {@link LocalDateTime} instances. - * @param date The date to parse. - * @param format The format to use. - */ - public Local(final String date, final String format) { - super(date, format, LocalDateTime::from); - } - /** - * Parses ISO date to create {@link LocalDateTime} instances. - * @param date The date to parse. - */ - public Local(final String date) { - super(date, LocalDateTime::from); - } - /** - * Parses the date using the formatter to create - * {@link LocalDateTime} instances. - * @param date The date to parse. - * @param formatter The formatter to use. - */ - public Local(final String date, final DateTimeFormatter formatter) { - super(date, formatter, LocalDateTime::from); - } - } - - /** - * Convenience class for parsing to {@link OffsetDateTime} instances. - */ - public static class Offset extends DateOf { - /** - * Parses date using the provided format to create - * {@link OffsetDateTime} instances. - * @param date The date to parse. - * @param format The format to use. - * @param offset The offset to use. - */ - public Offset(final String date, final String format, - final ZoneOffset offset) { - super(date, - DateTimeFormatter.ofPattern(format) - .withZone(offset.normalized()), - temporal -> ZonedDateTime.from(temporal).toOffsetDateTime() - ); - } - /** - * Parses ISO date to create {@link OffsetDateTime} instances. - * @param date The date to parse. - */ - public Offset(final String date) { - super(date, OffsetDateTime::from); - } - /** - * Parses the date using the formatter to create - * {@link OffsetDateTime} instances. - * @param date The date to parse. - * @param formatter The formatter to use. - */ - public Offset(final String date, final DateTimeFormatter formatter) { - super(date, formatter, OffsetDateTime::from); - } - } - - /** - * Convenience class for parsing to {@link ZonedDateTime} instances. - */ - public static class Zoned extends DateOf { - /** - * Parses date to create {@link ZonedDateTime} instances. - * @param date The date to parse. - */ - public Zoned(final String date) { - super(date, ZonedDateTime::from); - } - /** - * Parses date using the provided format to create - * {@link ZonedDateTime} instances. - * @param date The date to parse. - * @param format The format to use. - * @param zone The zone to use. - */ - public Zoned(final String date, final String format, - final ZoneId zone) { - super(date, - DateTimeFormatter.ofPattern(format).withZone(zone), - ZonedDateTime::from - ); - } - /** - * Parses the date using the formatter to create - * {@link ZonedDateTime} instances. - * @param date The date to parse. - * @param formatter The formatter to use. - */ - public Zoned(final String date, final DateTimeFormatter formatter) { - super(date, formatter, ZonedDateTime::from); - } - } - } diff --git a/src/main/java/org/cactoos/time/LocalDateTimeOf.java b/src/main/java/org/cactoos/time/LocalDateTimeOf.java new file mode 100644 index 0000000000..5c706e8d0f --- /dev/null +++ b/src/main/java/org/cactoos/time/LocalDateTimeOf.java @@ -0,0 +1,79 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import org.cactoos.Scalar; +import org.cactoos.scalar.UncheckedScalar; + +/** + * Parser for {@link LocalDateTime} instances. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +public class LocalDateTimeOf implements Scalar { + /** + * The parsed date. + */ + private final UncheckedScalar parsed; + + /** + * Parses ISO date to create {@link LocalDateTime} instances. + * @param date The date to parse. + */ + public LocalDateTimeOf(final String date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); + } + + /** + * Parses date using the provided format to create + * {@link LocalDateTime} instances. + * @param date The date to parse. + * @param format The format to use. + */ + public LocalDateTimeOf(final String date, final String format) { + this(date, DateTimeFormatter.ofPattern(format)); + } + + /** + * Parses the date using the formatter to create + * {@link LocalDateTime} instances. + * @param date The date to parse. + * @param formatter The formatter to use. + */ + public LocalDateTimeOf(final String date, + final DateTimeFormatter formatter) { + this.parsed = new UncheckedScalar<>( + () -> LocalDateTime.from(formatter.parse(date)) + ); + } + + @Override + public final LocalDateTime value() throws Exception { + return this.parsed.value(); + } + +} diff --git a/src/main/java/org/cactoos/time/OffsetDateTimeOf.java b/src/main/java/org/cactoos/time/OffsetDateTimeOf.java new file mode 100644 index 0000000000..1d43d3665c --- /dev/null +++ b/src/main/java/org/cactoos/time/OffsetDateTimeOf.java @@ -0,0 +1,85 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import org.cactoos.Scalar; +import org.cactoos.scalar.UncheckedScalar; + +/** + * Parser for {@link OffsetDateTime} instances. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +public class OffsetDateTimeOf implements Scalar { + /** + * The parsed date. + */ + private final UncheckedScalar parsed; + + /** + * Parses ISO date to create {@link OffsetDateTime} instances. + * @param date The date to parse. + */ + public OffsetDateTimeOf(final String date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); + } + + /** + * Parses date using the provided format to create + * {@link OffsetDateTime} instances. + * @param date The date to parse. + * @param format The format to use. + * @param offset The offset to use. + */ + public OffsetDateTimeOf(final String date, final String format, + final ZoneOffset offset) { + this(date, + DateTimeFormatter.ofPattern(format).withZone(offset.normalized()) + ); + } + + /** + * Parses the date using the formatter to create + * {@link OffsetDateTime} instances. + * @param date The date to parse. + * @param formatter The formatter to use. + */ + public OffsetDateTimeOf(final String date, + final DateTimeFormatter formatter) { + this.parsed = new UncheckedScalar<>( + () -> ZonedDateTime.from(formatter.parse(date)).toOffsetDateTime() + ); + } + + @Override + public final OffsetDateTime value() throws Exception { + return this.parsed.value(); + } + +} diff --git a/src/main/java/org/cactoos/time/ZonedDateTimeOf.java b/src/main/java/org/cactoos/time/ZonedDateTimeOf.java new file mode 100644 index 0000000000..10541cba25 --- /dev/null +++ b/src/main/java/org/cactoos/time/ZonedDateTimeOf.java @@ -0,0 +1,84 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import org.cactoos.Scalar; +import org.cactoos.scalar.UncheckedScalar; + +/** + * Parser for {@link ZonedDateTime} instances. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +public class ZonedDateTimeOf implements Scalar { + /** + * The parsed date. + */ + private final UncheckedScalar parsed; + + /** + * Parses date to create {@link ZonedDateTime} instances. + * @param date The date to parse. + */ + public ZonedDateTimeOf(final String date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); + } + + /** + * Parses date using the provided format to create + * {@link ZonedDateTime} instances. + * @param date The date to parse. + * @param format The format to use. + * @param zone The zone to use. + */ + public ZonedDateTimeOf(final String date, final String format, + final ZoneId zone) { + this(date, + DateTimeFormatter.ofPattern(format).withZone(zone) + ); + } + + /** + * Parses the date using the formatter to create + * {@link ZonedDateTime} instances. + * @param date The date to parse. + * @param formatter The formatter to use. + */ + public ZonedDateTimeOf(final String date, + final DateTimeFormatter formatter) { + this.parsed = new UncheckedScalar<>( + () -> ZonedDateTime.from(formatter.parse(date)) + ); + } + + @Override + public final ZonedDateTime value() throws Exception { + return this.parsed.value(); + } + +} diff --git a/src/test/java/org/cactoos/time/DateOfTest.java b/src/test/java/org/cactoos/time/DateOfTest.java index 6deba7d1ac..a3e30fab83 100644 --- a/src/test/java/org/cactoos/time/DateOfTest.java +++ b/src/test/java/org/cactoos/time/DateOfTest.java @@ -25,24 +25,21 @@ import java.sql.Date; import java.time.LocalDateTime; -import java.time.OffsetDateTime; -import java.time.ZoneId; import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; /** - * Tests for DateAsText. + * Tests for DateOf. * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) * @version $Id$ * @since 1.0 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) */ -@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) +@SuppressWarnings("PMD.AvoidDuplicateLiterals") public class DateOfTest { @Test @@ -50,7 +47,7 @@ public final void testParsingIsoFormattedStringToDate() throws Exception { MatcherAssert.assertThat( "Can't parse a Date with default/ISO format.", - new DateOf.Date( + new DateOf( "2017-12-13T14:15:16.000000017" ).value(), Matchers.is( @@ -67,7 +64,7 @@ public final void testParsingCustomFormattedStringToDate() throws Exception { MatcherAssert.assertThat( "Can't parse a Date with custom format.", - new DateOf.Date( + new DateOf( "2017-12-13 14:15:16.000000017", "yyyy-MM-dd HH:mm:ss.n" ).value(), @@ -85,7 +82,7 @@ public final void testParsingCustomFormatterStringToDate() throws Exception { MatcherAssert.assertThat( "Can't parse a Date with custom format.", - new DateOf.Date( + new DateOf( "2017-12-13 14:15:16.000000017", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n") ).value(), @@ -98,131 +95,4 @@ public final void testParsingCustomFormatterStringToDate() ); } - @Test - public final void testParsingIsoFormattedStringToZonedDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a ZonedDateTime with default/ISO format.", - new DateOf.Zoned( - "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" - ).value(), - Matchers.is( - ZonedDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") - ) - ) - ); - } - - @Test - public final void testParsingFormattedStringWithZoneToZonedDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a ZonedDateTime with custom format and zone.", - new DateOf.Zoned( - "2017-12-13 14:15:16", - "yyyy-MM-dd HH:mm:ss", - ZoneId.of("Europe/Berlin") - ).value(), - Matchers.is( - ZonedDateTime.of( - LocalDateTime.of(2017, 12, 13, 14, 15, 16), - ZoneId.of("Europe/Berlin") - ) - ) - ); - } - - @Test - public final void testParsingFormattedStringWithFormatterToZonedDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a ZonedDateTime with custom format and zone.", - new DateOf.Zoned( - "2017-12-13 14:15:16", - DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") - .withZone(ZoneId.of("Europe/Berlin")) - ).value(), - Matchers.is( - ZonedDateTime.of( - LocalDateTime.of(2017, 12, 13, 14, 15, 16), - ZoneId.of("Europe/Berlin") - ) - ) - ); - } - - @Test - public final void testParsingIsoFormattedStringToOffsetDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a OffsetDateTime with default/ISO format.", - new DateOf.Offset( - "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" - ).value(), - Matchers.is( - OffsetDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) - ) - ) - ); - } - - @Test - public final void testParsingFormattedStringWithOffsetToOffsetDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a OffsetDateTime with custom format.", - new DateOf.Offset( - "2017-12-13 14:15:16", - "yyyy-MM-dd HH:mm:ss", - ZoneOffset.ofHours(1) - ).value(), - Matchers.is( - OffsetDateTime.of( - LocalDateTime.of(2017, 12, 13, 14, 15, 16), - ZoneOffset.ofHours(1) - ) - ) - ); - } - - @Test - public final void testParsingIsoFormattedStringToLocalDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a LocalDateTime with default/ISO format.", - new DateOf.Local( - "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" - ).value(), - Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) - ); - } - - @Test - public final void testParsingFormattedStringWithFormatToLocalDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a LocalDateTime with custom format.", - new DateOf.Local( - "2017-12-13 14:15:16.000000017", - "yyyy-MM-dd HH:mm:ss.n" - ).value(), - Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) - ); - } - - @Test - public final void testParsingFormattedStringWithFormatterToLocalDateTime() - throws Exception { - MatcherAssert.assertThat( - "Can't parse a LocalDateTime with custom formatter.", - new DateOf.Local( - "2017-12-13 14:15:16.000000017", - DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n") - ).value(), - Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) - ); - } - } diff --git a/src/test/java/org/cactoos/time/LocalDateTimeOfTest.java b/src/test/java/org/cactoos/time/LocalDateTimeOfTest.java new file mode 100644 index 0000000000..104a0a5712 --- /dev/null +++ b/src/test/java/org/cactoos/time/LocalDateTimeOfTest.java @@ -0,0 +1,81 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for {@link LocalDateTimeOf}. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public class LocalDateTimeOfTest { + + @Test + public final void testParsingIsoFormattedStringToLocalDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a LocalDateTime with default/ISO format.", + new LocalDateTimeOf( + "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" + ).value(), + Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) + ); + } + + @Test + public final void testParsingFormattedStringWithFormatToLocalDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a LocalDateTime with custom format.", + new LocalDateTimeOf( + "2017-12-13 14:15:16.000000017", + "yyyy-MM-dd HH:mm:ss.n" + ).value(), + Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) + ); + } + + @Test + public final void testParsingFormattedStringWithFormatterToLocalDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a LocalDateTime with custom formatter.", + new LocalDateTimeOf( + "2017-12-13 14:15:16.000000017", + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n") + ).value(), + Matchers.is(LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17)) + ); + } + +} diff --git a/src/test/java/org/cactoos/time/OffsetDateTimeOfTest.java b/src/test/java/org/cactoos/time/OffsetDateTimeOfTest.java new file mode 100644 index 0000000000..5a95ad2f92 --- /dev/null +++ b/src/test/java/org/cactoos/time/OffsetDateTimeOfTest.java @@ -0,0 +1,79 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for {@link OffsetDateTimeOf}. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public class OffsetDateTimeOfTest { + + @Test + public final void testParsingIsoFormattedStringToOffsetDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a OffsetDateTime with default/ISO format.", + new OffsetDateTimeOf( + "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" + ).value(), + Matchers.is( + OffsetDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) + ) + ) + ); + } + + @Test + public final void testParsingFormattedStringWithOffsetToOffsetDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a OffsetDateTime with custom format.", + new OffsetDateTimeOf( + "2017-12-13 14:15:16", + "yyyy-MM-dd HH:mm:ss", + ZoneOffset.ofHours(1) + ).value(), + Matchers.is( + OffsetDateTime.of( + LocalDateTime.of(2017, 12, 13, 14, 15, 16), + ZoneOffset.ofHours(1) + ) + ) + ); + } + +} diff --git a/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java b/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java new file mode 100644 index 0000000000..f7693a12fc --- /dev/null +++ b/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java @@ -0,0 +1,99 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.time; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for {@link ZonedDateTimeOf}. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public class ZonedDateTimeOfTest { + + @Test + public final void testParsingIsoFormattedStringToZonedDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a ZonedDateTime with default/ISO format.", + new ZonedDateTimeOf( + "2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]" + ).value(), + Matchers.is( + ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ) + ) + ); + } + + @Test + public final void testParsingFormattedStringWithZoneToZonedDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a ZonedDateTime with custom format and zone.", + new ZonedDateTimeOf( + "2017-12-13 14:15:16", + "yyyy-MM-dd HH:mm:ss", + ZoneId.of("Europe/Berlin") + ).value(), + Matchers.is( + ZonedDateTime.of( + LocalDateTime.of(2017, 12, 13, 14, 15, 16), + ZoneId.of("Europe/Berlin") + ) + ) + ); + } + + @Test + public final void testParsingFormattedStringWithFormatterToZonedDateTime() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a ZonedDateTime with custom format and zone.", + new ZonedDateTimeOf( + "2017-12-13 14:15:16", + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + .withZone(ZoneId.of("Europe/Berlin")) + ).value(), + Matchers.is( + ZonedDateTime.of( + LocalDateTime.of(2017, 12, 13, 14, 15, 16), + ZoneId.of("Europe/Berlin") + ) + ) + ); + } + +}