diff --git a/src/main/java/org/cactoos/time/DateAsText.java b/src/main/java/org/cactoos/time/DateAsText.java new file mode 100644 index 0000000000..7c79760326 --- /dev/null +++ b/src/main/java/org/cactoos/time/DateAsText.java @@ -0,0 +1,189 @@ +/** + * 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.io.IOException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; +import java.util.Date; +import java.util.Locale; +import org.cactoos.Text; +import org.cactoos.scalar.UncheckedScalar; +import org.cactoos.text.UncheckedText; + +/** + * Formatter for date instances. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public class DateAsText implements Text { + /** + * Scalar carrying the formatted date. + */ + private final UncheckedScalar formatted; + + /** + * Formats the milliseconds using the ISO format. + * @param milliseconds Milliseconds to format. + */ + public DateAsText(final long milliseconds) { + this( + ZonedDateTime.ofInstant( + Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC") + ), + DateTimeFormatter.ISO_DATE_TIME + ); + } + + /** + * Formats the milliseconds using the format and the default locale. + * @param milliseconds Milliseconds to format. + * @param format The format to use. + */ + public DateAsText(final long milliseconds, final String format) { + this( + ZonedDateTime.ofInstant( + Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC") + ), + format, + Locale.getDefault(Locale.Category.FORMAT) + ); + } + + /** + * Formats the milliseconds as date using the format and the locale. + * @param milliseconds Milliseconds to format as date. + * @param format The format to use. + * @param locale The locale to use for the format. + */ + public DateAsText(final long milliseconds, final String format, + final Locale locale) { + this( + ZonedDateTime.ofInstant( + Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC") + ), + DateTimeFormatter.ofPattern(format, locale) + ); + } + + /** + * Formats the date with ISO format using the system zone. + * @param date The date to format. + */ + public DateAsText(final Date date) { + this( + ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")), + DateTimeFormatter.ISO_DATE_TIME + ); + } + + /** + * Formats the date with to format using the default locale and the system + * zone. + * @param date The date to format. + * @param format The format to use. + */ + public DateAsText(final Date date, final String format) { + this( + ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")), + DateTimeFormatter.ofPattern( + format, + Locale.getDefault(Locale.Category.FORMAT) + ) + ); + } + + /** + * Formats the date using the format and locale using the system default + * zone. + * @param date The date to format. + * @param format The format to use. + * @param locale The locale to use. + */ + public DateAsText(final Date date, final String format, + final Locale locale) { + this( + ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")), + DateTimeFormatter.ofPattern(format, locale) + ); + } + + /** + * Formats date using ISO date time format. + * @param date The date to format. + */ + public DateAsText(final TemporalAccessor date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); + } + + /** + * Formats date using provided date time format string using default locale. + * @param date The date to format. + * @param format The format to use. + */ + public DateAsText(final TemporalAccessor date, final String format) { + this(date, format, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the provided format string using the provided + * locale. + * @param date The date to format. + * @param format The format string to use. + * @param locale The locale to use. + */ + public DateAsText(final TemporalAccessor date, final String format, + final Locale locale) { + this(date, DateTimeFormatter.ofPattern(format, locale)); + } + + /** + * Formats the date using the provided formatter. + * @param date The date to format. + * @param formatter The formatter to use. + */ + public DateAsText(final TemporalAccessor date, + final DateTimeFormatter formatter) { + this.formatted = new UncheckedScalar<>( + () -> formatter.format(date) + ); + } + + @Override + public final String asString() throws IOException { + return this.formatted.value(); + } + + @Override + public final int compareTo(final Text text) { + return new UncheckedText(this).compareTo(text); + } + +} + diff --git a/src/main/java/org/cactoos/time/DateOf.java b/src/main/java/org/cactoos/time/DateOf.java new file mode 100644 index 0000000000..46c1351b92 --- /dev/null +++ b/src/main/java/org/cactoos/time/DateOf.java @@ -0,0 +1,82 @@ +/** + * 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.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import org.cactoos.Scalar; +import org.cactoos.scalar.UncheckedScalar; + +/** + * Parser for {@link Date} instances. + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +public class DateOf implements Scalar { + + /** + * The parsed date. + */ + private final Scalar parsed; + + /** + * Parses the provided date as ISO formatted. + * @param date The date to parse. + */ + public DateOf(final String date) { + this(date, DateTimeFormatter.ISO_DATE_TIME); + } + + /** + * Parses the date using the provided format. + * @param date The date to parse. + * @param format The format to use. + */ + 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. + */ + public DateOf(final String date, final DateTimeFormatter formatter) { + this.parsed = new UncheckedScalar<>( + () -> Date.from( + LocalDateTime.from(formatter.parse(date)) + .toInstant(ZoneOffset.UTC) + ) + ); + } + + @Override + public final Date value() throws Exception { + return this.parsed.value(); + } + +} 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/main/java/org/cactoos/time/package-info.java b/src/main/java/org/cactoos/time/package-info.java new file mode 100644 index 0000000000..0d6a9af0a7 --- /dev/null +++ b/src/main/java/org/cactoos/time/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Time. + * + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +package org.cactoos.time; diff --git a/src/test/java/org/cactoos/time/DateAsTextTest.java b/src/test/java/org/cactoos/time/DateAsTextTest.java new file mode 100644 index 0000000000..d1967a9c0d --- /dev/null +++ b/src/test/java/org/cactoos/time/DateAsTextTest.java @@ -0,0 +1,175 @@ +/** + * 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.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for DateAsText. + * @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 DateAsTextTest { + + @Test + public final void testZonedDateTimeFormattedAsIsoDateTime() + throws Exception { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + MatcherAssert.assertThat( + "Can't format a ZonedDateTime with default/ISO format.", + new DateAsText(date).asString(), + Matchers.is("2017-12-13T14:15:16.000000017+01:00[Europe/Berlin]") + ); + } + + @Test + public final void testZonedDateTimeFormattedWithFormatString() + throws Exception { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + MatcherAssert.assertThat( + "Can't format a ZonedDateTime with format.", + new DateAsText(date, "yyyy-MM-dd HH:mm:ss").asString(), + Matchers.is("2017-12-13 14:15:16") + ); + } + + @Test + public final void testZonedDateTimeFormattedWithFormatStringWithLocale() + throws Exception { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + MatcherAssert.assertThat( + "Can't format a ZonedDateTime with format using locale.", + new DateAsText(date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH) + .asString(), + Matchers.is("2017 déc. 13. 14.15.16") + ); + } + + @Test + public final void testDateFormattedUsingIsoFormatter() throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + calendar.set(Calendar.MILLISECOND, 17); + MatcherAssert.assertThat( + "Can't format a java.util.Date with ISO format.", + new DateAsText(calendar.getTime()).asString(), + Matchers.is("2017-12-13T14:15:16.017Z[UTC]") + ); + } + + @Test + public final void testDateFormattedUsingCustomFormat() throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + MatcherAssert.assertThat( + "Can't format a java.util.Date with custom format.", + new DateAsText( + calendar.getTime(), "yyyy MM dd hh:mm:ss" + ).asString(), + Matchers.is("2017 12 13 02:15:16") + ); + } + + @Test + public final void testDateFormattedUsingCustomFormatDifferentLocale() + throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + MatcherAssert.assertThat( + "Can't format a java.util.Date with custom format.", + new DateAsText( + calendar.getTime(), "yyyy MMM dd hh:mm:ss", Locale.ITALIAN + ).asString(), + Matchers.is("2017 dic 13 02:15:16") + ); + } + + @Test + public final void testMillisFormattedUsingIsoFormatter() + throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + calendar.set(Calendar.MILLISECOND, 17); + MatcherAssert.assertThat( + "Can't format a java.util.Date with ISO format.", + new DateAsText(calendar.getTime().getTime()).asString(), + Matchers.is("2017-12-13T14:15:16.017Z[UTC]") + ); + } + + @Test + public final void testMillisFormattedUsingCustomFormat() + throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + MatcherAssert.assertThat( + "Can't format a java.util.Date with custom format.", + new DateAsText( + calendar.getTime().getTime(), + "yyyy MM dd hh:mm:ss" + ).asString(), + Matchers.is("2017 12 13 02:15:16") + ); + } + + @Test + public final void testMillisFormattedUsingCustomFormatDifferentLocale() + throws Exception { + final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + MatcherAssert.assertThat( + "Can't format a java.util.Date with custom format.", + new DateAsText( + calendar.getTime().getTime(), + "yyyy MMMM dd hh:mm:ss", + Locale.US + ).asString(), + Matchers.is("2017 December 13 02:15:16") + ); + } + +} diff --git a/src/test/java/org/cactoos/time/DateOfTest.java b/src/test/java/org/cactoos/time/DateOfTest.java new file mode 100644 index 0000000000..a3e30fab83 --- /dev/null +++ b/src/test/java/org/cactoos/time/DateOfTest.java @@ -0,0 +1,98 @@ +/** + * 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.sql.Date; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * 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") +public class DateOfTest { + + @Test + public final void testParsingIsoFormattedStringToDate() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a Date with default/ISO format.", + new DateOf( + "2017-12-13T14:15:16.000000017" + ).value(), + Matchers.is( + Date.from( + LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17) + .toInstant(ZoneOffset.UTC) + ) + ) + ); + } + + @Test + public final void testParsingCustomFormattedStringToDate() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a Date with custom format.", + new DateOf( + "2017-12-13 14:15:16.000000017", + "yyyy-MM-dd HH:mm:ss.n" + ).value(), + Matchers.is( + Date.from( + LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17) + .toInstant(ZoneOffset.UTC) + ) + ) + ); + } + + @Test + public final void testParsingCustomFormatterStringToDate() + throws Exception { + MatcherAssert.assertThat( + "Can't parse a Date with custom format.", + new DateOf( + "2017-12-13 14:15:16.000000017", + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n") + ).value(), + Matchers.is( + Date.from( + LocalDateTime.of(2017, 12, 13, 14, 15, 16, 17) + .toInstant(ZoneOffset.UTC) + ) + ) + ); + } + +} 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") + ) + ) + ); + } + +} diff --git a/src/test/java/org/cactoos/time/package-info.java b/src/test/java/org/cactoos/time/package-info.java new file mode 100644 index 0000000000..3085c2997b --- /dev/null +++ b/src/test/java/org/cactoos/time/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Time, tests. + * + * @author Sven Diedrichsen (sven.diedrichsen@gmail.com) + * @version $Id$ + * @since 1.0 + */ +package org.cactoos.time;