I am getting an Assertion error " Body Content Expected child but was null when asserting the andExpect XML. If I input as as a String "2020-10-01-5:00" it works fine but if I concatenate the date into a string like:
LocalDate startDate = LocalDate.now().minusDays(90);
String startDateLine = "<start-date>" + startDate + "-5:00</start-date>\n";
It throws the AssertionError. I have verified that the XML is correct before the call so I am unsure what about getting the date and converting to a string causes the test to fail.
Update
Do not add the offset string to the LocalDate string in order to convert it into an OffsetDateTime string. Shown below is the idiomatic way to convert a LocalDate to OffsetDateTime
LocalDate.of(2020, 10, 1)
.atStartOfDay()
.atOffset(ZoneOffset.of("-05:00"));
Demo:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2020, 10, 1);
LocalDateTime ldt = date.atStartOfDay();
OffsetDateTime odt = ldt.atOffset(ZoneOffset.of("-05:00"));
System.out.println(odt);
}
}
Output:
2020-10-01T00:00-05:00
ONLINE DEMO
You can get the String representation of an OffsetDateTime using the function OffsetDateTime#toString e.g.
String strOdt = odt.toString();
Original answer
Change your input to have the timezone offset in the format HH:mm e.g. -05:00 so that it conforms to ISO 8601 standards.
Use DateTimeFormatterBuilder with .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) to default the hour-of-day to 0.
Parse the given string to OffsetDateTime as it has timezone offset and OffsetDateTime is the best fit to represent Date-Time with timezone offset.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf =new DateTimeFormatterBuilder()
.appendPattern("u-M-d[H:m:s]XXX")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter(Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse("2020-10-01-05:00", dtf);
System.out.println(odt);
}
}
Output:
2020-10-01T00:00-05:00
ONLINE DEMO
Notice the optional pattern inside a square bracket.
Learn more about the modern Date-Time API* from Trail: Date Time.
Related
I tried this:
val x = java.time.Instant.parse("2022-12-12T09:51:09.681+0100")
But it throws an exception.
Exception in thread "main" java.time.format.DateTimeParseException: Text '2022-12-12T09:51:09.681+0100' could not be parsed at index 23
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.Instant.parse(Instant.java:395)
at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated$ScratchFileRunnerGenerated.<init>(tmp.kt:8)
at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt:13)
What is wrong with the timestamp?
If I compare it to
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
I can not see an error.
This does not work either:
java.time.OffsetDateTime.parse("2022-12-12T09:51:09.681+0100")
Apply DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx") while parsing the given date-time string into an OffsetDateTime and then convert the obtained OffsetDateTime into an Instant if required.
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx");
OffsetDateTime odt = java.time.OffsetDateTime.parse("2022-12-12T09:51:09.681+0100", parser);
System.out.println(odt);
// Convert to Instant
Instant instant = odt.toInstant();
System.out.println(instant);
}
}
Output:
2022-12-12T09:51:09.681+01:00
2022-12-12T08:51:09.681Z
Learn more about the modern Date-Time API from Trail: Date Time.
I am trying to create an instance of Instant from date and time strings. Date is formatted like this yyyy-MM-dd. So the values could look like this:
val date = "2021-11-25"
val time = "15:20"
I am trying to make a valid instant from this 2 strings like this:
val dateTime = "${date}T${time}:00"
val instantDateTime = ZonedDateTime.parse(dateTime,
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(defaultTimeZone)
).toInstant()
I have also tried with it:
val instantDateTime = Instant.from(DateTimeFormatter .ISO_OFFSET_DATE_TIME.withZone(defaultTimeZone).parse(dateTime))
But, that is not working, I get:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-11-25T15:20:00' could not be parsed at index 19
You can combine the date and time strings to create a date-time string in ISO 8601 format which you can parse into LocalDateTime and then convert into Instant by using the applicable ZoneId. Note that the modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
String strDate = "2021-11-25";
String strTime = "15:20";
String strDateTime = strDate + "T" + strTime;
LocalDateTime ldt = LocalDateTime.parse(strDateTime);
Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(instant);
}
}
Output:
2021-11-25T15:20:00Z
ONLINE DEMO
Some alternative approaches:
Create the instance of LocalDateTime can be as suggested by daniu i.e. parse the date and time strings individually and create the instance of LocalDateTime using them.
Demo:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
String strDate = "2021-11-25";
String strTime = "15:20";
LocalDateTime ldt = LocalDateTime.of(LocalDate.parse(strDate), LocalTime.parse(strTime));
Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(instant);
}
}
ONLINE DEMO
Create the instance of ZonedDateTime using ZonedDateTime#of(LocalDate, LocalTime, ZoneId) as suggested by Ole V.V.. Another variant that you can try with this approach is by using ZonedDateTime#of(LocalDateTime, ZoneId).
Demo:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
String strDate = "2021-11-25";
String strTime = "15:20";
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.parse(strDate), LocalTime.parse(strTime),
ZoneId.systemDefault());
// Alternatively
// ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(LocalDate.parse(strDate), LocalTime.parse(strTime)),
// ZoneId.systemDefault());
Instant instant = zdt.toInstant();
System.out.println(instant);
}
}
ONLINE DEMO
Combine the date and time strings to create a date-time string in ISO 8601 format and parse the same to ZonedDateTime using DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()).
Demo:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String strDate = "2021-11-25";
String strTime = "15:20";
DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault());
ZonedDateTime zdt = ZonedDateTime.parse(strDate + "T" + strTime, dtf);
Instant instant = zdt.toInstant();
System.out.println(instant);
}
}
ONLINE DEMO
Create an instance of LocalDateTime by parsing the date and time strings, and use the LocalDateTime#toInstant to get the required Instant.
Demo:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
String strDate = "2021-11-25";
String strTime = "15:20";
LocalDateTime ldt = LocalDateTime.of(LocalDate.parse(strDate), LocalTime.parse(strTime));
Instant instant = ldt.toInstant(ZoneId.systemDefault().getRules().getOffset(ldt));
System.out.println(instant);
}
}
ONLINE DEMO
Learn more about the modern Date-Time API* from Trail: Date Time. Check this answer and this answer to learn how to use java.time API with JDBC.
* 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. Note that Android 8.0 Oreo already provides support for java.time.
Your date string doesn't include a timezone, so it cannot be parsed directly to a ZonedDateTime (see javadoc for ZonedDateTime#parse).
Try parsing the string to a LocalDateTime instead (using LocalDate#parse).
You can then convert the the LocalDateTimeto an Instant using LocalDateTime#toInstant or to a ZonedDateTime using LocalDateTime#atZone
Given:
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDate date = LocalDate.parse(dateString, DATE_TIME_FORMATTER);
ZonedDateTime zonedDateTime = date.atStartOfDay((ZoneOffset.UTC));
System.out.println(zonedDateTime);
}
And output:
2018-07-30T00:00Z
...what is the pattern to print seconds? Stupid question no doubt but driving me a little nuts
I need:
2018-07-30T00:00:00Z
I changed java.time.LocalDate to java.time.LocalDateTime, you need it if you want to show also the seconds.
package com.test;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormatter {
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
ZonedDateTime zonedDateTime = date.atZone(ZoneOffset.UTC);
System.out.println(zonedDateTime);
}
}
Output is:
2018-07-30T13:36:17.820Z
LocalDate will keep just date. You need to parse LocalDateTime and convert to ZonedDateTime and you will have seconds as you expect.
var dateString = "2018-07-30T13:36:17.820";
var format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
var localDate = LocalDateTime.parse(dateString, format);
var zone = ZoneId.of( "America/Montreal" );
var zonedDateTime = localDate.atZone(zone);
System.out.println(zonedDateTime);
You will have to go a few steps:
parse the String to a LocalDateTime because it contains date and time of day
extract the date only
create a ZonedDateTime out of that by adding the start of day (LocalTime.MIN = 00:00:00) and a ZoneOffset.UTC
This code may do:
public static void main(String[] args) {
String dateString = "2018-07-30T13:36:17.820";
// parse a LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(dateString);
// extract the date part
LocalDate localDate = localDateTime.toLocalDate();
// make it a ZonedDateTime by applying a ZoneId
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, LocalTime.MIN, ZoneOffset.UTC);
// print the result
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
Output is
2018-07-30T00:00:00Z
There are several ways to do it, this is just one of them and it just slightly differs from most of the other answers (and comments :-) ).
tl;dr
You have used the wrong things in the wrong places.
You do not need a DateTimeFormatter explicitly in order to parse 2018-07-30T13:36:17.820 because it's already in ISO 8601 format which is also the default format used by LocalDateTime#parse. Moreover, this string has date and time instead of just date; therefore, it makes more sense to parse it into LocalDateTime instead of LocalDate. You can always get LocalDate from LocalDateTime using LocalDateTime#toLocalDate.
The ZonedDateTime#toString uses the LocalDateTime#toString which in turn uses LocalTime#toString for the time part which omits second and fraction-of-second if they are zero. If you need a string with zero second and fraction-of-second, you will need to use a DateTimeFormatter.
Demo:
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
String dateString = "2018-07-30T13:36:17.820";
LocalDateTime localDateTime = LocalDateTime.parse(dateString);// You do not need a DateTimeFormatter here
ZonedDateTime zonedDateTime = localDateTime.toLocalDate().atStartOfDay(ZoneOffset.UTC);
// Print zonedDateTime.toString()
System.out.println(zonedDateTime);
// Custom format
final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
System.out.println(DATE_TIME_FORMATTER.format(zonedDateTime));
}
}
Output:
2018-07-30T00:00Z
2018-07-30T00:00:00.000
Learn more about the modern date-time API from Trail: Date Time.
I have a date, assumed to be in GMT, which I want to convert to local time zone using the ISO_OFFSET_DATE_TIME formatting.
Basically, I want to go from:
2018-03-13 03:00:00.0
to:
2018-03-13T00:00:00-09:00
Obviously this would change, depending on your local time zone.
Any ideas on how I could do this?
You can leverage ZonedDateTime for this. You just need to read in the date as UTC and convert it as needed. You might get something like this:
String readPattern = "yyyy-MM-dd HH:mm:ss.S";
DateTimeFormatter readDateTimeFormatter = DateTimeFormatter.ofPattern(readPattern).withZone(ZoneOffset.UTC);
LocalDateTime utcLocalDateTime = LocalDateTime.parse("2018-03-13 03:00:00.0", readDateTimeFormatter);
ZonedDateTime localZonedDateTime = utcLocalDateTime.atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.systemDefault());
String writePattern = "yyyy-MM-dd HH:mm:ssXXX";
DateTimeFormatter writeDateTimeFormatter = DateTimeFormatter.ofPattern(writePattern);
System.out.println(writeDateTimeFormatter.format(localZonedDateTime));
For more info, see:
https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Parse the date-time string into LocalDateTime:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);
Combine this with UTC offset to create an OffsetDateTime:
OffsetDateTime odtUtc = ldt.atOffset(ZoneOffset.UTC);
Create its copy with offset set as -09:00 while keeping the instant same:
OffsetDateTime odtUtcMinus9 = odtUtc.withOffsetSameInstant(ZoneOffset.of("+09:00"));
Demo:
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);
System.out.println(ldt); // 2018-03-13T03:00
OffsetDateTime odtUtc = ldt.atOffset(ZoneOffset.UTC);
System.out.println(odtUtc); // 2018-03-13T03:00Z
OffsetDateTime odtUtcMinus9 = odtUtc.withOffsetSameInstant(ZoneOffset.of("+09:00"));
System.out.println(odtUtcMinus9); // 2018-03-13T12:00+09:00
}
}
Note that the timezone offset is a fixed thing i.e. it is independent of the DST. If you are looking for an automatic adjustment of timezone offset as per the DST, use ZonedDateTime. The methods are very much similar to what we have used in the last demo.
Demo:
import java.time.LocalDateTime;
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) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);
System.out.println(ldt); // 2018-03-13T03:00
ZonedDateTime zdtUtc = ldt.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc); // 2018-03-13T03:00Z[Etc/UTC]
ZonedDateTime zdtAmericaAdak = zdtUtc.withZoneSameInstant(ZoneId.of("America/Adak"));
System.out.println(zdtAmericaAdak); // 2018-03-12T18:00-09:00[America/Adak]
// A custom format
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX", Locale.ENGLISH);
String formatted = dtfOutput.format(zdtAmericaAdak);
System.out.println(formatted); // 2018-03-12 18:00:00.000-09:00
}
}
Learn more about java.time, the modern date-time API* from Trail: Date Time.
* 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.
My date-time format string is : yyyy-MM-dd'T'HH:mm:ss.SSSZ
I am using DateTimeFormatter from Joda Time to print my date in the above mentioned format.
Now, consider the date as
2016/04/01 23:00:00
then it should have printed
2016-04-01T23:00:00.000Z
But, it prints
2016-04-01T23:00:00.000+0200
Please help me in getting the date printed in the same format as specified in the string format.
2016-04-01T23:00:00.000Z != 2016-04-01T23:00:00.000+0200
Here is what Basil Bourque has commented on the wrong answer:
No, no, no. All you have done is append text, creating a falsity. If
your date-time represents a moment in a time zone that is two hours
ahead of UTC such as Europe/Helsinki, and you slap a Z on the end
which says Zulu and means UTC, you are now telling a lie, representing
value that is off by two hours. This is like replacing the dollar sign
in a price with a Euro currency symbol but failing to change the
number.
Just to illustrate what he has mentioned:
£100 != $100
The Z in 2016-04-01T23:00:00.000Z is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours). The same moment will be presented in different timezones with different values e.g.
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.parse("2016-04-01T23:00:00.000Z");
ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
ZonedDateTime zdtIndia = instant.atZone(ZoneId.of("Asia/Kolkata"));
ZonedDateTime zdtNepal = instant.atZone(ZoneId.of("Asia/Kathmandu"));
System.out.println(zdtNewYork);
System.out.println(zdtIndia);
System.out.println(zdtNepal);
// Or at a fixed timezone offset of +02:00 hours
OffsetDateTime odtWithTwoHoursOffset = instant.atOffset(ZoneOffset.of("+02:00"));
System.out.println(odtWithTwoHoursOffset);
}
}
Output:
2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00
To understand this concept a bit further, try converting a date-time from one timezone to another e.g. I have shown a conversion of a New York date-time into UTC. I have shown another conversion of a date-time with a timezone offset of +02:00 hours into UTC.
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// #######Example of converting a date-time from one timezone to another#####
ZonedDateTime zdtNewYork = ZonedDateTime.parse("2016-04-01T19:00-04:00[America/New_York]");
Instant instant = zdtNewYork.toInstant();
System.out.println(instant);
// Or as ZonedDateTime
ZonedDateTime zdtUtc = zdtNewYork.withZoneSameInstant(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc);
// Alternatively, this can be obtained from instant
zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc);
// ###########################################################################
System.out.println();
// #####Example of converting a date-time at a fixed timezone offset to UTC###
OffsetDateTime odtNewYork = OffsetDateTime.parse("2016-04-02T01:00+02:00");
instant = odtNewYork.toInstant();
System.out.println(instant);
// Alternatively
OffsetDateTime odtUtc = odtNewYork.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(odtUtc);
// Alternatively,
odtUtc = instant.atOffset(ZoneOffset.UTC);
System.out.println(odtUtc);
// ###########################################################################
}
}
Output:
2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z
According https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html Z has special meaning:
Z zone-offset
If you want to escape Z quote Z with ':
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
For example:
java.time.LocalDateTime date = java.time.LocalDateTime.now();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
java.lang.String text = date.format(formatter);
System.out.println(text);
prints
2016-06-11T18:39:41.962Z