DateTimeFormatter String to LocalDateTime - java

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"?

Related

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

Java: convert string to different time zone

I've tried all sorts of different conversions with different Java formatters but I'm still not having any luck with something that seems simple.
I have a string that is a date/time in UTC. I'm trying to convert that to another time zone. Is any one able to tell me why the below isn't working? The time zone is changing but it's not changing the right way.
Updated: (though it doesn't seem like I'm setting the time zone to UTC properly as the conversion isn't correct either).
String dateInput = "2021-02-16 20:57:43";
SimpleDateFormat mdyUtc = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
mdyUtc.setTimeZone(TimeZone.getTimeZone("UTC");
Date utcOutput = mdyUtc.parse(dateInput);
SimpleDateFormat mdyOffset = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
mdyOffset.setTimeZone(TimeZone.getTimeZone("GMT-10:00");
Date localOutput = mdyOffset.parse(dateInput);
System.out.print("UTC date = " + utcOutput);
System.out.print("Changed date = " + localOutput);
Output:
UTC date = Tue Feb 16 15:57:43 EST 2021
Changed date = Wed Feb 17 01:57:43 EST 2021
java.time
The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API*.
Using the modern date-time API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String dateInput = "2021-02-16 20:57:43";
// Replace ZoneId.systemDefault() with ZoneOffset.UTC if this date-time is in UTC
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH)
.withZone(ZoneId.systemDefault());
ZonedDateTime zdt = ZonedDateTime.parse(dateInput, dtf);
ZonedDateTime result = zdt.withZoneSameInstant(ZoneId.of("GMT-10:00"));
System.out.println(result);
}
}
Output:
2021-02-16T10:57:43-10:00[GMT-10:00]
ONLINE DEMO
Learn more about the modern date-time API from Trail: Date Time.
Can I get java.util.Date from ZonedDateTime?
If at all you need to use java.util.Date, you can convert ZonedDateTime into it as follows:
Date date = Date.from(result.toInstant());
Note that the java.util.Date object is not a real date-time object like the modern date-time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). When you print an object of java.util.Date, its toString method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat and obtain the formatted string from it.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
tl;dr
LocalDateTime // Represent a date with time-of-day but lacking the context of a time zone or offset-from-UTC.
.parse( // Interpret some text in order to build a date-time object.
"2021-02-16 20:57:43".replace( " " , "T" ) // Convert to standard ISO 8601 string to parse by default without needing to specify a formatting pattern.
) // Returns a `LocalDateTime` object.
.atOffset( // Place that date with time into the context of an offset. Determines a moment, a specific point on the timeline.
ZoneOffset.UTC // A constant for an offset of zero hours-minutes-seconds.
) // Returns an `OffsetDateTime` object.
.atZoneSameInstant( // Adjust the view of this moment as seen in the wall-clock time of some other time zone. Still the same moment, same point on the timeline.
ZoneId.of( "Pacific/Honolulu" ) // Use a time zone, if known, rather than a mere offset.
) // Returns a `ZonedDateTime` object.
.toString() // Generate text representing this moment in standard ISO 8601 format extended to append the time zone name in square brackets.
See this code run live at IdeOne.com.
2021-02-16T10:57:43-10:00[Pacific/Honolulu]
Details
The Answer by Avinash is correct, using a DateTimeFormatter with an assigned ZoneId. That works, but I prefer keeping the zone assignment separate from the formatter, to be more explicit to someone reading the code. This is only about my preference, not about correctness; both Answers are equally correct.
Parse your input as a LocalDateTime, as the input represents a date with time-of-day but lacks any indication of offset or time zone.
By default, the java.time classes use standard text formats defined in ISO 8601. If an input complies, no need to specify a formatting pattern. To comply, replace your input’s SPACE character in the middle with a T.
String input = "2021-02-16 20:57:43".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
You said you know for certain that input was meant to represent a date with time as seen in UTC, having an offset-from-UTC of zero hours-minutes-seconds. So we can apply an offset of zero using ZoneOffset to produce a OffsetDateTime.
Also, I suggest you educate the publisher of your data feed about using ISO 8601 formats to communicate that offset-of-zero fact by appending a Z (as well as using T in the middle).
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; // Place date with time into context of an offset of zero.
Lastly, you said you want to adjust that moment to another time zone. Apply a ZoneId to get a ZonedDateTime object.
Actually, you specified an offset of "GMT-10:00". But it is better to use a time zone if known rather than a mere offset. A time zone is a history of past, present, and future changes to the offset used by the people of a particular region.
I will guess you want Hawaii time, Pacific/Honolulu.
ZoneId z = ZoneId.of( "Pacific/Honolulu" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
The java.util.Date API is deprecated; you should look into the new Date and Time APIs around LocalTime et al.
That said, if you want to keep the old code: It is a bit brittle. Your initial date input does not specify a time zone, so you'll probably get the system's time zone. You should specify a time zone --- if the expected input is UTC, say so.
Then you need to specify the time zone either in an hour offset or with a name, not both.
When I change your code to use
mdyOffset.setTimeZone(TimeZone.getTimeZone("-10:00"));
I get
Changed date = Tue Feb 16 14:57:43 CST 2021
which seems to fit, as I'm on CST (currently 6 hours after GMT), so 20:57:43 minus 6 is 14:57:43. Again, this is displayed in my local time zone. You may have to use a DateFormat to adjust the output as needed.

Java Date Time as ISO_DATE_TIME

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

Java converting string, having millisecond to date object

I have 2 Strings
2012-06-25 15:02:22.948
+0530
I need a new string which adds the 5:30 to the time in the first string.
I thought I can do this by converting both strings to date objects and then adding. But i dont know how to do it, as when i use
yyyy MM dd hh:mm:ss as the date format for the first string, I get an error.
Thanks!
The format of the string 2012-06-25 15:02:22.948 is not yyyy MM dd hh:mm:ss, so it's not surprising that you get "an error" (what error is it? the more specific you are, the better people can help you!).
Try yyyy-MM-dd HH:mm:ss.SSS. See the API documentation of SimpleDateFormat to understand the exact syntax of the format string.
Note: Upper and lower case is important in the format string. hh means 12-hour clock, HH means 24-hour clock. If you use hh, parsing 15 for the hours won't work. You also didn't include the milliseconds SSS in the format string.
You can merge both you string String1+string2 and can use format yyyy-MM-dd HH:mm:ss.SSSZ to parse the date. You can see more documentation here
You're getting an exception because the your date format String is wrong. You're giving a date string on the form
"yyyy-MM-dd hh:mm:ss.S"
See SimpleDateFormat javadoc
Try this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = format.parse("2012-06-25 15:02:22.948");
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(date.getTime());
int time = Integer.parseInt("0530");
int hour = time / 100;
int minute = time % 100;
calendar.add(Calendar.HOUR_OF_DAY, hour);
calendar.add(Calendar.MINUTE, minute);
String newDateInString = format.format(calendar.getTime());
The other answers are correct but outdated.
java.time
The old date-time classes (java.util.Date/.Calendar etc.) bundled with the earliest versions of Java are now legacy.
Those old classes have been supplanted by the java.time package. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
LocalDateTime
The LocalDateTime class represent a date-time without time zone. Use those for the first piece.
Your format is close to standard ISO 8601 format, just replace the SPACE with a T.
String input = "2012-06-25 15:02:22.948";
String inputStandardized = input.replace( " " , "T" );
LocalDateTime ldt = LocalDateTime.parse( inputStandardized );
Offset from UTC
The other piece is the offset-from-UTC. We use the ZoneOffset class for this.
ZoneOffset offset = ZoneOffset.of( "+0530" );
Without an offset or time zone the LocalDateTime is not an actual moment on the timeline but rather a rough idea about a possible moment. Now we add your offset-from-UTC to mark an actual moment, represented by the OffsetDateTime class.
OffsetDateTime odt = OffsetDateTime.of( ldt , offset );
Zoned
A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). So better to use a time zone than a mere offset.
For example, if the context of this data is known to be time in India, use a time zone such as Asia/Kolkata to get a ZonedDateTime.
ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );

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