Explain Codes LogoExplain Codes Logo

How to get current moment in ISO 8601 format with date, hour, and minute?

java
prompt-engineering
java-8
datetime
Nikita BarsukovbyNikita Barsukov·Jan 23, 2025
TLDR

Let's see the fast, easy, and modern way. LocalDateTime and DateTimeFormatter from java.time will do the job:

String isoDateTime = LocalDateTime.now() .truncatedTo(ChronoUnit.MINUTES) .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); System.out.println(isoDateTime);

This will give you something like "2023-04-05T14:20", minimalistic ISO 8601 formatted current datetime.

java.time for the win: Simplifying ISO 8601

Let's dig deeper and see how the Java Time API can tackle ISO 8601 formatting with grace.

Thread-safe and easy with java.time

Bid farewell to the cumbersome SimpleDateFormat. The java.time package provides thread-safe, easy-to-use classes. Get slick with Instant.now() and DateTimeFormatter.ISO_INSTANT:

// "Great Scott! This is heavy!" String isoInstant = DateTimeFormatter.ISO_INSTANT .format(Instant.now().truncatedTo(ChronoUnit.MINUTES));

Keeping time zones in check

Time to dive into timezones. ZonedDateTime is your ally:

// "UTC is the new black." String zonedISODateTime = ZonedDateTime.now(ZoneOffset.UTC) .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

ISO 8601? Customize it!

Unleash your creativity. With DateTimeFormatter, you can shape your own pattern:

// "I can make my own ISO 8601, with blackjack and...never mind." DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX"); String customIsoDateTime = LocalDateTime.now() .truncatedTo(ChronoUnit.MINUTES).format(customFormatter);

Remember, when dealing with timezones, X is the magic letter.

java.time alternatives: When you're stuck in the past

Are you still on Java 6 or 7? Don't worry, we've got you covered.

java.time, meet ThreeTen-Backport

If java.time sounds appealing but you're rocking Java 6 or 7, ThreeTen-Backport is your saving grace:

// "Backport? More like Back to the Future!" String backportIsoDateTime = LocalDateTime.now() .truncatedTo(ChronoUnit.MINUTES) .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

Still living it old style with SimpleDateFormat

If you're a stubborn mule and insist on SimpleDateFormat, behold:

// "Old is gold, but not always!" SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String formattedDate = dateFormat.format(new Date());

Nifty tricks and traps for young coders

Let's check out some good practices and common pitfalls in ISO 8601 date-time formatting.

Don't get caught! The pitfalls

  • Ignoring the timezone, leading to incorrect output.
  • Turning a blind eye to SimpleDateFormat's lack of thread-safety.

The life-saving tricks

  • Always set the timezone to UTC when needed—it's non-negotiable.
  • Embrace the modern java.time package. It's thread-safe and user-friendly.

Wrapping it up: Clean and maintainable formatting

Clean, readable code is a gift that keeps on giving.

Using constants to define formats

Why repeat when you can define constants?

// "I'm constant as the northern star!" public static final DateTimeFormatter ISO_MINUTE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX").withZone(ZoneOffset.UTC);

Striking a balance between detail and readability

Keep your dates and times as precise as necessary, but no more. Use truncatedTo() to cut off unnecessary precision and use constants for frequently reused patterns.