Java ISO Date Format
Due to the similarity and complexity of the built-in APIs, converting date objects in Java to strings is challenging. However, handling date objects is crucial to our daily work as developers, we are unable to escape this subject. Let's examine how to correctly convert various dates to strings.
I'll utilise ISO 8601, a global standard for the transfer of date- and time-related data, even as string format moving forward. The time and date are stated using ISO 8601 as follows:
2020-05-12T7:33:56+00:00
2021-09-25T7:33:56.000+00:00
This article describes how to convert Java dates to ISO-8601 strings using java.util. Date and Java.util. Java.time.ZonedDateTime -> string conversion calendar.
Unfortunately, Java 6 and previous versions of SimpleDateFormat do not support ISO 8601 conforming time zone formats. Time zone strings like "GMT+01:00" or "+0100," the latter of which complies with RFC # 822, are recognised by SimpleDateFormat.
Despite Java 7's addition of support for ISO 8601 time zone descriptors, SimpleDateFormat is still unable to correctly parse a complete date string because it does not handle optional components.
Regexp can be used to restructure your input text, but the replacement rules are not as straightforward as you may think from your inquiry.
- The string need not terminate with ":00" since certain time zones are not offset by whole hours from UTC.
- ISO8601 only permits the time zone's number of hours to be specified, therefore "+01" is similar to "+01:00."
- In place of "+00:00," ISO8601 supports the use of "Z" to denote UTC.
java.util.Date
An illustration of how to change a java.util.Date into an ISO 8601 date string is shown below. We're utilising the current time, which is the simplest use-case, thus this is a little tough. In other situations, I think utilising java.util.GregorianCalendar might be a better option. The differences are clear in the sentences that follow.
Date.java
class Date {
public static void main (String args[]){
Date d = new Date (System.currentTimeMillis());
// Conversion to date format
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
sdf.setTimeZone (TimeZone.getTimeZone("CET"));
String str = sdf.format(date);
// Output
System.out.println(str);
}
}
Output
"2023-01-1T21:00:00.000+01:00"
java.util.Calendar
Obtaining an instance of the Calendar is required before using it to create a date object. Please be aware of setting the millisecond field is essential; else, the millisecond value will be incorrect. There will be a non-zero value entered.
Date1.java
class Date1 {
public static void main (String args[]){
Calendar c = Calendar.getInstance();
calendar.set(2020, Calendar.FEBRUARY, 15, 21, 20, 15);
calendar.set(Calendar.MILLISECOND, 0);
Date d = calendar.getTime();
// Conversion
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
sdf.setTimeZone(TimeZone.getTimeZone("CET"));
String str = sdf.format(date);
System.out.println(str);
}
}
Output
"2020-02-15T21:20:15.000+01:00"
java.util.GregorianCalendar
It is preferable to use the gregorian calendar because we don't have to explicitly set the millisecond datepart to 0. To format the date, we must still utilise java.util.Date as a middleman.
Date2.java
class Date2 {
public static void main (String args[]){
GregorianCalendar c;
c = new GregorianCalendar(2020, Calendar.FEBRUARY, , 15, 21, 20);
Date d = calendar.getTime();
// Conversion
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
sdf.setTimeZone(TimeZone.getTimeZone("CET"));
String str = sdf.format(date);
System.out.println(str);
}
}
Output
"2020-02-15T21:20:15.000+01:00"
java.time.ZonedDateTime
The most elegant answer among the options is provided by the package java.time, previously known as Joda-Time. It employs a builder to incrementally create the date, time, and time zone. In order to format a date representation in a string, this object takes a formatter. You can utilise the digit rather than the static Java field since its monthly is a base-1 number, which suggests that January is equal to 1 rather than 0. Check out the code:
Date3.java
class Date3 {
public static void main (String args[]){
ZonedDateTime date = LocalDate .of(2017, 2, 16)
.atTime(20, 22, 28)
.atZone(ZoneId.of("CET"));
// Conversion
String str = DateTimeFormatter.ISO_DATE_TIME.format(date);
System.out.println(str);
}
}
Output
"2017-02-16T20:22:28+01:00[CET]"
Date9.java
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Date9{
public static String iso = "yyyyMMdd'T'HHmmss'Z'";
public static boolean Iso(String str) {
return str.matches("\\d{8}T\\d{6}Z");
}
public static void parseIsoDateTime(String str)
{
SimpleDateFormat date = new SimpleDateFormat(iso);
date.setLenient(false);
date.setTimeZone(TimeZone.getTimeZone("UTC"));
Date r = date.parse(str, new ParsePosition(0));
System.out.println(r);
}
}
Output:
“2023-01-08T08:36:15Z”
This article has covered a variety of date object creation techniques as well as how to convert such objects into ISO 8601 date representations. I showed how to convert popular date object types into strings, however the most of them are confusing and don't support time-zones very well. Joda-Time, however, has now been included into Java SE 8 as package java.time as a result of the adoption of JSR 310.