From 8c4f1fadf86e65de60a8807e1dfee7c8d691bb1d Mon Sep 17 00:00:00 2001 From: Morten Haraldsen Date: Sat, 23 Dec 2023 21:11:48 +0100 Subject: [PATCH] Update README.md with compilable samples --- README.md | 98 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 2501a69..99df2b7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Internet Time Utility + # Internet Time Utility [![Maven Central](https://img.shields.io/maven-central/v/com.ethlo.time/itu.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.ethlo.time%22%20a%3A%22itu%22) [![javadoc](https://javadoc.io/badge2/com.ethlo.time/itu/javadoc.svg)](https://javadoc.io/doc/com.ethlo.time/itu/latest/com/ethlo/time/ITU.html) @@ -44,70 +44,96 @@ Add dependency com.ethlo.time itu - 1.7.0 + 1.7.3 ``` Below you find some samples of usage of this library. Please check out the [javadoc](https://javadoc.io/doc/com.ethlo.time/itu/latest/com/ethlo/time/ITU.html) for more details. - ```java import java.time.OffsetDateTime; import com.ethlo.time.DateTime; import com.ethlo.time.ITU; -// Parse a string -final OffsetDateTime dateTime = ITU.parseDateTime("2012-12-27T19:07:22.123456789-03:00"); +class Test { + void smokeTest() { + // Parse a string + final OffsetDateTime dateTime = ITU.parseDateTime("2012-12-27T19:07:22.123456789-03:00"); -// Format with no fraction digits -final String formatted = ITU.formatUtc(dateTime); // 2012-12-27T22:07:22Z + // Format with seconds (no fraction digits) + final String formatted = ITU.formatUtc(dateTime); // 2012-12-27T22:07:22Z -// Format with microsecond precision -final String formattedMicro = ITU.formatUtcMicro(dateTime); // 2012-12-27T22:07:22.123457Z + // Format with microsecond precision + final String formattedMicro = ITU.formatUtcMicro(dateTime); // 2012-12-27T22:07:22.123457Z -// Parse lenient, raw data -final DateTime dateTime = ITU.parse("2012-12-27T19:07Z"); + // Parse lenient, raw data + final DateTime dateTime = ITU.parseLenient("2012-12-27T19:07Z"); + } +} ``` ### Handle leap-seconds ```java -try -{ - final OffsetDateTime dateTime = ITU.parseDateTime("1990-12-31T15:59:60-08:00"); -} -catch (LeapSecondException exc) -{ - // The following helper methods are available let you decide how to progress - exc.getSecondsInMinute(); // 60 - exc.getNearestDateTime() // 1991-01-01T00:00:00Z - exc.isVerifiedValidLeapYearMonth() // true +import com.ethlo.time.ITU; +import com.ethlo.time.LeapSecondException; +import java.time.OffsetDateTime; + +class Test { + void testParseDateTime() { + try { + final OffsetDateTime dateTime = ITU.parseDateTime("1990-12-31T15:59:60-08:00"); + } catch (LeapSecondException exc) { + // The following helper methods are available let you decide how to progress + exc.getSecondsInMinute(); // 60 + exc.getNearestDateTime(); // 1991-01-01T00:00:00Z + exc.isVerifiedValidLeapYearMonth(); // true + } + } } ``` ### Handle different granularity (ISO format) Validate to different required granularity: ```java -ITU.isValid("2017-12-06", TemporalType.LOCAL_DATE_TIME); -``` +import com.ethlo.time.ITU; +import com.ethlo.time.TemporalType; + +class Test { + void test() { + ITU.isValid("2017-12-06", TemporalType.LOCAL_DATE_TIME); + } +} +``` Allowing handling different levels of granularity: ```java -return ITU.parse("2017-12-06", new TemporalHandler<>() -{ - @Override - public OffsetDateTime handle(final LocalDate localDate) - { - return localDate.atTime(OffsetTime.of(LocalTime.of(0, 0), ZoneOffset.UTC)); - } +import com.ethlo.time.ITU; +import com.ethlo.time.TemporalHandler; - @Override - public OffsetDateTime handle(final OffsetDateTime offsetDateTime) - { - return offsetDateTime; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.ZoneOffset; +import java.time.temporal.TemporalAccessor; + +class Test { + TemporalAccessor extract() { + return ITU.parse("2017-12-06", new TemporalHandler<>() { + @Override + public OffsetDateTime handle(final LocalDate localDate) { + return localDate.atTime(OffsetTime.of(LocalTime.of(0, 0), ZoneOffset.UTC)); + } + + @Override + public OffsetDateTime handle(final OffsetDateTime offsetDateTime) { + return offsetDateTime; + } + }); } -}); -``` +} +``` ## Q & A