FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
JavaDates & time zones

Check the Gregorian leap-year rule

Java: Use Year.isLeap rather than checking divisibility by four alone.

Java✓ Tested
JavaDates & time zones

Convert an instant to a regional timezone

Java: Use ZonedDateTime with a named zone to apply that zone’s offset rules.

Java✓ Tested
JavaDates & time zones

Convert epoch seconds into an instant

Java: Instant represents a UTC timeline point independent of the default timezone.

Java✓ Tested
JavaDates & time zones

Count days between two dates

Java: Use ChronoUnit.DAYS for an elapsed count rather than a Period day component.

Java✓ Tested
JavaDates & time zones

Find the last day of a month

Java: Use YearMonth so leap years and month lengths are handled by the calendar library.

Java✓ Tested
JavaDates & time zones

Find the next chosen weekday

Java: TemporalAdjusters.next excludes the starting day even if it already has that weekday.

Java✓ Tested
JavaDates & time zones

Generate a half-open range of dates

Java: datesUntil includes the starting date and excludes the end date.

Java✓ Tested
JavaDates & time zones

Measure a duration between instants

Java: Use Duration for elapsed timeline time and retrieve total seconds.

Java✓ Tested
JavaDates & time zones

Parse a date with an explicit input pattern

Java: Use DateTimeFormatter for a non-ISO date representation.

Java✓ Tested
JavaDates & time zones

Read an ISO week year and week number

Java: Use ISO week fields together because a week can belong to the previous calendar year.

Java✓ Tested