Java Date Time as ISO_DATE_TIME - java

I want to add a Z at the end of DateTimeFormatter ISO_DATE_TIME in Java not hard coded
String sample = "2018-05-11T13:35:11Z";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX][X]");
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
LocalDateTime newLocalDateTime = LocalDateTime.parse(sample, df1);
System.out.println(newLocalDateTime.toString());
Output is:
2018-05-11T13:35:11
I want the output to be 2018-05-11T13:35:11Z

You are calling toString() of your LocalDateTime, you should be calling format. Change
System.out.println(newLocalDateTime.toString());
to
System.out.println(newLocalDateTime.format(df1));
Outputs
2018-05-11T13:35:11Z

If you want the output to have a time zone offset like Z, you should use OffsetDateTime or ZonedDateTime.
LocalDateTime ldt = LocalDateTime.parse("2018-05-11T13:35:11");
OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC);
System.out.println(odt); // prints: 2018-05-11T13:35:11Z
ZonedDateTime zdt = ldt.atZone(ZoneOffset.UTC);
System.out.println(zdt); // prints: 2018-05-11T13:35:11Z
As you can see, the toString() method will return the date in the format you requested.

You shouldn’t use LocalDateTime. Use OffsetDateTime. And you may need no formatter.
String sample = "2018-05-11T13:35:11Z";
OffsetDateTime dateTime = OffsetDateTime.parse(sample)
.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(dateTime.toString());
Output from this snippet is the dsesired:
2018-05-11T13:35:11Z
The call to withOffsetSameInstant() makes sure that the date and time are in UTC even if the input had not been. I am using OffsetDateTime.toString() to produce the output string. I am exploiting the fact that both your sample string and your desired output are in ISO 8601 format. OffsetDateTime and the other classes from java.time parse ISO 8601 format as their default, that is, without any explicit formatter, and produce ISO 8601 format from their toString methods.
OffsetDateTime.toString() on one hand will leave out the seconds if they are 0, will on the other hand include a fraction of second if it is non-zero (all of this agress with ISO 8601). If you don’t want this, you do need a formatter. For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
System.out.println(dateTime.format(formatter));
You are correct, you should not hardcode Z as a literal in the format pattern string. Z signifies an offset of zero from UTC. Use format pattern letter X to output the offset so you are sure the offset is always correct. This will print an offset of zero as Z.
A LocalDateTime doesn’t keep a UTC offset, so when parsing into one your are losing information. Don’t do this. Parse into an OffsetDateTime to pick up all the information from the string.
Link: Wikipedia article: ISO 8601

Related

DateTimeFormatter String to LocalDateTime

I am trying to convert the value 2022-04-30 14:34:52.900426+00:00 an instance of LocalDateTime. I have written the following code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS'Z'");
I am getting the following error
could not be parsed at index 26
What should my formatter string be?
tl;dr
OffsetDateTime // Use `OffsetDateTime`, not `LocalDateTime`, to represent a date with time and offset.
.parse(
"2022-04-30 14:34:52.900426+00:00" // Almost in ISO 8601 format.
.replace( " " , "T" ) // Replace SPACE with T to comply with ISO 8691.
)
See this code run live at IdeOne.com.
Wrong class, use OffsetDateTime, not LocalDateTime
LocalDateTime is exactly the wing class to use in this case. That represents a date with time-of-day. But your input indicates a date with time-of-day and an offset-from-UTC. The +00:00 means an offset of zero hours-minutes-seconds from UTC.
So parse that input as a OffsetDateTime object instead.
Rather than define a formatting pattern, I suggest merely replacing the SPACE in the middle with a T to comply with the ISO 8601 standard used by default in the java.time classes when parsing/generating text.
String input = "2022-04-30 14:34:52.900426+00:00".replace( " " , "T" ) ;
OffsetDateTime odt = OffsetDateTime.parse( input ) ;
It's not working because the UTC Time offsets hasn't been written properly. It should look like this with a custom DateTimeFormatter:
//Custom DatTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZZZZZ");
formatter.parse("2022-04-30 14:34:52.900426+00:00");
You could either use the predefined ISO_OFFSET_DATE_TIME DatTimeFormatter only by replacing the space between date and time with a capital T, as the standard requires.
//Predefined DateTimeFormatter
DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2022-04-30T14:34:52.900426+00:00");
Besides, to answer your question under #Taco Jan Osinga's reply:
No, it is not correct to use "+00:00" to just match the datetime you're trying to parse. That custom DateTimeFormatter you would build would only match datetime referring to your local TimeZone; thus it won't work with datetime from different areas.
Have you tried "yyyy-MM-dd HH:mm:ss.SSSSSS+00:00"?

convert iso to utc format in java

I'm getting below exception while converting date string from ISO to UTC format in java, what I'm doing wrong here? Please find my code below:
Test.java
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
String date="2020-11-02T07:00:00.114";
String result = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")).atOffset(ZoneOffset.UTC).toString();
System.out.println("result: "+result);
}
}
Exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-11-02T07:00:00.114' could not be parsed, unparsed text found at index 19
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
at test.service.Test.main(Test.java:11)
tl;dr
LocalDateTime
.parse( "2020-11-02T07:00:00.114" )
.atOffset( ZoneOffset.UTC )
.toString()
2020-11-02T07:00:00.114Z
Details
You said:
converting date string from ISO to UTC format
That does not exactly make sense.
There is an ISO 8601 standard defining sensible formats for exchanging date-time values textually. These standard formats are used by default when parsing/generating text.
There is no such thing as "UTC format". UTC is the temporal prime meridian, against which all time zones are defined. The rules for a time zone specify an offset-from-UTC in effect as a number of hours-minutes-seconds ahead or behind UTC. But this has nothing to with "formats" of text.
As for parsing your input string as a LocalDateTime object, there is no need to bother with defining a formatting pattern. Your string complies with ISO 8601, so it can be parsed directly.
LocalDateTime ldt = LocalDateTime.parse( "2020-11-02T07:00:00.114" ) ;
Your string, and a LocalDateTime object, represent a date with a time-of-day. But both lack the context of a time zone or offset-from-UTC.
Apparently you know that the publisher of your input string intended that to be a moment as seen from an offset of zero hours-minutes-seconds from UTC. So we can assign a ZoneOffset to get OffsetDateTime object.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
To generate text representing that moment in ISO 8601 format, simply call toString.
String output = odt.toString() ;
See this code run live at Ideone.com.
2020-11-02T07:00:00.114Z
Notice the Z on the end, pronounced “Zulu”. That is an abbreviation for an offset of zero, +00:00.
Your pattern ends in ss, in other words: In seconds. Your input then has still more stuff.
DateTimeFormatter doesn't take a wild stab at it - it either matches properly or it does not parse. It looks like those are intended to be milliseconds; toss .SSS at the end of your pattern.
You are giving the format of:
yyyy-MM-dd'T'HH:mm:ss
However are specifying a string with fractions of a second 2020-11-02T07:00:00.114.
That is parsing whole seconds, but there is still more in the time string.
You need format of:
yyyy-MM-dd'T'HH:mm:ss.SSS

How to get Date in UTC+0 in Java? [duplicate]

This question already has answers here:
Why is string to date and back to string resulting in two different strings?
(2 answers)
Closed 1 year ago.
I am using the following code to get the date in ISO-8601 format. For UTC the value returned does not contain offset.
OffsetDateTime dateTime = OffsetDateTime.ofInstant(
Instant.ofEpochMilli(epochInMilliSec), zoneId);
return dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
For other time formats the response returned looks like:
2016-10-30T17:00:00-07:00
In case of UTC the value returned is:
2016-10-30T17:00:00Z
I want it to be:
2016-10-30T17:00:00+00:00
Note: Do not use UTC-0 as -00:00 is not ISO8601 compliant.
The built-in formatter uses Z when the offset is zero. The Z is short for Zulu and means UTC.
You'll have to use a custom formatter, using a java.time.format.DateTimeFormatterBuilder to set a custom text for when the offset is zero:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset, set "-00:00" when offset is zero
.appendOffset("+HH:MM", "-00:00")
// create formatter
.toFormatter();
System.out.println(dateTime.format(fmt));
This will print:
2016-10-30T17:00:00-00:00
Just reminding that -00:00 is not ISO8601 compliant. The standard allows only Z and +00:00 (and the variations +0000 and +00) when the offset is zero.
If you want +00:00, just change the code above to:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset
.appendPattern("xxx")
// create formatter
.toFormatter();
This formatter will produce the output:
2016-10-30T17:00:00+00:00
If you can accept +00:00 instead of -00:00, you can also use a simpler DateTimeFormatter:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxxx");
OffsetDateTime odt = OffsetDateTime.parse("2016-10-30T17:00:00Z");
System.out.println(fmt.format(odt));
I used x whereas the standard toString() method of OffsetDateTime uses X. The main difference between x and X is that one return +00:00 vs. Z for the other.

Formatting date in java

I am trying to format date string ex. 2014-11-24T18:30:00.000Z to 2014-11-24 using this code below:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.format(reqJsonObj.getString(FROM_DATE));
But it raises exception
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Use dateFormat.parse() instead of dateFormat.format(), since you want to parse your String into Date object. Then, when you have the Date object, format it to String with wanted format. #Jens already gave you the full code, so no need to copy it again here.
You have to parse the string first as a date and then format the date:
SimpleDateFormat dateFormatP = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date parsedDate = dateFormatP.parse(reqJsonObj.getString(FROM_DATE));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.format(parsedDate );
You need to give a date as the argument for format not a String. First create a SimpleDateFormat to parse the string to a date then give the date object to the other SimpleDateFormat to format the date.
USe this
Date date = formatter.parse(dateInString);
You specified one pattern, appropriate for the generating of a string you seek as your output. But you did define a pattern for parsing the input. You need to go from a string through parsing to a date-time object. Then use that object for generating a string.
Do not think of a string as a date-time but as a textual representation of a date-time value. Think of the date-time object as the actual value.
Avoid old date-time classes
You are using the old date-time classes that are poorly designed, confusing, and troublesome. Avoid classes such as java.util.Date/.Calendar.
java.time
Those old classes are supplanted by the java.time framework built into Java 8 and later.
An Instant is a moment on the timeline in UTC.
Instant now = Instant.now();
ISO 8601
The ISO 8601 standard defined sensible formats for textual representations of date-time values.
Both your input and output strings happen to comply with this standard.
The java.time classes use ISO 8601 as their default for parsing and generating strings. So no need for you to specify formatting patterns.
The Z on the end is short for Zulu which means UTC.
Instant instant = Instant.parse( "2014-11-24T18:30:00.000Z" );
What you want is a date-only value without time-of-day. The LocalDate class serves that purpose.
Determining a date requires an offset-from-UTC (or a full time zone). The Instant class is always in one particular offset, an offset of zero ( UTC), but is not really aware of that fact. Instant is only a basic building-block class.
The OffsetDateTime class is savvy about various offsets including UTC. We need to specify an explicit offset to make an OffsetDateTime. We will specify the handy constant for UTC, ZoneOffset.UTC.
OffsetDateTime odt = OffsetDateTime.of( instant , ZoneOffset.UTC );
The Instant and the OffsetDateTime are both a moment on the timeline in UTC. The difference is that OffsetDateTime is more flexible and has more features. We can ask the OffsetDateTime for our desired LocalDate object.
LocalDate localDate = odt.toLocalDate();
Now we simply call toString to generate output in standard ISO 8601 format.
String output = localDate.toString();
2014-11-24

How to convert java date without Z to a date with Z

I have the following date value 1995-12-31T23:59:59
but in order to parse this for a solr query I need it in the below format
1995-12-31T23:59:59Z
How can I parse this to get the added "Z" on the end in java 1.6 ?
The type must be java.util.date after the conversion - fyi
When I toString the date now and attempt to parse it with the SimpleDateFormat object it looks like this
"Mon Jan 01 00:00:00 CST 2001" - what is this format to convert it?
Use SimpleDateFormat:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date d = df.parse("1995-12-31T23:59:59Z");
System.out.println(d);
Put the 'Z' in single quotes to escape
"Z" is the time zone abbreviation for Zulu time zone i.e. UTC. If solr API accepts the date object, then you can just parse the date in the following way by setting preferred timezone:
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateParser.setTimeZone(TimeZone.getTimeZone("Z"));
Date date = df.parse("1995-12-31T23:59:59");
If you need to convert it back to string then use the method provided by nsfyn55:
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(dateFormatter.format());
Avoid Old Date-Time Classes
You are using the old java.util.Date/.Calendar and SimpleDateFormat classes. Avoid them.
The Date class has the poor design choice of its toString applying a default time zone when generating a String. So it seems like it has a time zone when in fact it does not (except one buried underneath that is ignored for regular use). Confusing, yes. Avoid it.
java.time
Instead use java.time built into Java 8 and later.
First parse as a LocalDateTime without any time zone or offset.
LocalDateTime ldt = LocalDateTime.parse( "1995-12-31T23:59:59Z" );
Apply a time zone or offset-from-UTC to give this LocalDateTime meaning, to make it an actual moment on the timeline. You have to know, or ask, what time zone or offset was intended by this string as no indication was embedded. For this example, I will arbitrarily assume Québec.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );
Your desired output has a Z on the end, for Zulu which means UTC.
In java.time an Instant represents a moment on the timeline in UTC. You can extract an Instant from the ZonedDateTime.
Instant instant = zdt.toInstant();
The Instant class’ toString method generates a string in your desired format. That format is one of the standard ISO 8601 formats.
String output = instant.toString();
Half-Open
I happened to notice that your example value was trying to get the end of 1995. There is a better way to do such search or comparison criteria.
In date-time work, the best practice is called Half-Open where the beginning of a span of time is inclusive while the ending is exclusive. So a week starts on Monday and runs up to, but not including, the next Monday.
Defining a year means starting at the first moment of the first day of 1995 and running up to but not including the first moment of the first day of the following year, 1996. Searching for any values within that range is done not with a BETWEEN but as: ( someEvent >= firstMomentOf1995 AND someEvent < firstMomentOf1996 ) ( not <= ).

Categories