In this post, we will see how to format Instant to String in java.
Table of Contents
Format Instant to String by associating time-zone to Instant
Instant does not contain time zone information, it only have timestamp to milliseconds from UNIX epoch i.e 1 Jan 1970 from UTC,so DateTimeFormatter
can not print date directly because date is always printed with time zone information.
In order to format Instant to String, we need to first associate timezone to formatter and it will work fine.
Get current timestamp in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Locale; public class FormatInstantToString { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ) .withLocale( Locale.UK ) .withZone( ZoneId.systemDefault() ); Instant instant = Instant.now(); String instantStr = formatter.format( instant ); System.out.println("Instant in String format: "+instantStr); } } |
Output:
If you do not provide timezone information, then DateTimeFormatter
will through an Exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.time.Instant; import java.time.format.DateTimeFormatter; public class FormatInstantToString { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Instant instant = Instant.now(); String instantStr = formatter.format(instant); System.out.println("Instant in String format: "+instantStr); } } |
Output:
at java.base/java.time.Instant.getLong(Instant.java:603)
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2704)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2343)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1848)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1822)
at org.arpit.java2blog.entry.FormatInstantToString.main(FormatInstantToString.java:15)
Further reading:
Format Instant to String in ISO-8601 format
You can use DateTimeFormatter with ISO_LOCAL_DATE_TIME
if you don’t want any explicit time-zone with time-zone implicitly UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; public class FormatInstantToString { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME .withZone(ZoneId.from(ZoneOffset.UTC)); Instant instant = Instant.now(); String instantStr = formatter.format(instant); System.out.println("Instant in String format: "+instantStr); } } |
Output:
Using Instant toString()
Instants are already in UTC and if you are not worried about format, then you can simply use toString()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog.entry; import java.time.Instant; public class FormatInstantToString { public static void main(String[] args) { Instant instant = Instant.now(); String instantStr = instant.toString(); System.out.println("Default String format for Instant: "+instantStr); } } |
Output:
if you don’t want T and Z in the String. You can change highlighted line to:
1 2 3 4 |
String instantStr = instant.toString().replaceAll("[TZ]", " "); // Output: Default String format for Instant: 2021-05-01 12:14:30.687870800 |
If you need time in milliseconds rather than nanosecond, you can use:
1 2 3 4 |
String instantStr = instant.truncatedTo(ChronoUnit.MILLIS).toString().replaceAll("[TZ]", " ") // Output: Default String format for Instant: 2021-05-01 12:15:12.073 |
That’s all about how to format Instant to String in java.