I am reading data from upstream system and it returns the date in string format like this,
String dateFromUpstream = 11-14-2022 10:41:12 EDT
Now, I want to convert this string to a date format of UTC timezone and then store it into my entity.
I tried the following way,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss z");
LocalDateTime date = ZonedDateTime.parse(dateFromUpstream, formatter).toLocalDateTime().atZone(ZoneId.of("UTC"));
But this doesn't change the date to UTC timezone. It still gives me the same date with UTC instead of EDT at the end of the string.
Anyone know how I can do this and then store into an entity?
Parse the given date-time string into a ZonedDateTime with the corresponding DateTimeFormatter and then convert the resulting ZonedDateTime into an Instant or another ZonedDateTime corresponding to UTC, using ZonedDateTime#withZoneSameInstant.
Demo:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String dateFromUpstream = "11-14-2022 10:41:12 EDT";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateFromUpstream, dtf);
Instant instant = zdt.toInstant();
System.out.println(instant);
// Or get a ZonedDateTime at UTC
ZonedDateTime zdtUTC = zdt.withZoneSameInstant(ZoneOffset.UTC);
System.out.println(zdtUTC);
// If you want LocalDateTime
LocalDateTime ldt = zdtUTC.toLocalDateTime();
System.out.println(ldt);
}
}
See this code run at Ideone.com.
Output:
2022-11-14T15:41:12Z
2022-11-14T15:41:12Z
2022-11-14T15:41:12
Learn more about the modern Date-Time API from Trail: Date Time.
Note: As suggested by Basil Bourque, you can convert the parsed date-time into an OffsetDateTime at UTC as shown below:
OffsetDateTime odtUTC = zdt.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC);
Related
i was trying to convet string time into ZonedDateTime but not comes up with solution . This is the string format of time "2022-12-23T07:20:00"
i have tried this approach
String stringDate = "2022-12-23T07:20:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
ZonedDateTime ztdOfDateOfPurchase = ZonedDateTime.parse(stringDate , dateTimeFormatter);
this error is coming=> java.time.format.DateTimeParseException: Text '2022-12-23T07:20:00' could not be parsed at index 19
Simply parse your date-time string using LocalDateTime#parse and add the applicable ZoneId to get the ZonedDateTime.
Note that java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format (e.g. your date-time string, 2022-12-23T07:20:00).
Demo:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.parse("2022-12-23T07:20:00");
// Replace ZoneId.systemDefault() with the applicable zone ID e.g.
// ZoneId.of("America/New_York")
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
System.out.println(zdt);
// Alternatively,
zdt = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdt);
}
}
Output in my timezone:
2022-12-23T07:20Z[Europe/London]
2022-12-23T07:20Z[Europe/London]
Learn more about the modern Date-Time API from Trail: Date Time.
"2021-09-17 11:48:06 UTC"
I want to parse the following string and create a LocalDateTime object or an Instant
I know you can write something like this
String dateTime = "2021-09-17 11:48:06 UTC";
LocalDateTime dt = LocalDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
How do I deal with the UTC part in the string?
Never hardcode the standard timezone text like UTC, GMT etc.
Never hardcode the standard timezone text like UTC, GMT etc. which DateTimeFormatter is already capable of handling in the best way.
Parse the given Date-Time string using the pattern, uuuu-MM-dd HH:mm:ss VV into a TemporalAccessor from which you can get the Instant as well as the LocalDateTime.
Demo:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2021-09-17 11:48:06 UTC";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss VV", Locale.ENGLISH);
TemporalAccessor temporalAccessor = dtf.parse(strDateTime);
Instant instant = Instant.from(temporalAccessor);
LocalDateTime ldt = LocalDateTime.from(temporalAccessor);
System.out.println(instant);
System.out.println(ldt);
}
}
Output:
2021-09-17T11:48:06Z
2021-09-17T11:48:06
ONLINE DEMO
Alternatively:
Parse the given Date-Time string using the pattern, uuuu-MM-dd HH:mm:ss VV into a ZonedDateTime from which you can get the Instant as well as the LocalDateTime.
Demo:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2021-09-17 11:48:06 UTC";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss VV", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
Instant instant = Instant.from(zdt);
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(zdt);
System.out.println(instant);
System.out.println(ldt);
}
}
Output:
2021-09-17T11:48:06Z[UTC]
2021-09-17T11:48:06Z
2021-09-17T11:48:06
ONLINE DEMO
Learn more about 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.
"2021-09-17 11:48:06 UTC" isn't a local date time: it's a date time, because it has a time zone. And because your time has a time zone, it doesn't match your pattern, which doesn't.
If your time strings always end with exactly "UTC", you can make that a literal in the pattern:
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'");
If you need to handle other time zones than UTC, you can use z:
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
but note that this is for parsing a zoned date time; from that, you can extract the local date time:
ZonedDateTime zdt = ZonedDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
LocalDateTime ldt =z dt.toLocalDateTime();
I have below code.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
LocalDateTime myDate = LocalDateTime.parse("2020-11-16T02:27:39.345Z", formatter);
But it throws below error in the second line. Not sure why it's complaining Z
java.time.format.DateTimeParseException: Text '2020-11-16T02:27:39.345Z' 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.LocalDateTime.parse(LocalDateTime.java:492)
LocalDateTime does not have timezone or zone-offset information whereas your date-time string has zone-offset. The letter, Z at the end of your date-time string stands for Zulu i.e. zone-offset of UTC. You can parse it into OffsetDateTime or ZonedDateTime or Instant directly (i.e. without using a custom DateTimeFormatter).
Demo:
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
String dateTimeString = "2020-11-16T02:27:39.345Z";
OffsetDateTime odt = OffsetDateTime.parse(dateTimeString);
System.out.println(odt);
ZonedDateTime zdt = ZonedDateTime.parse(dateTimeString);
System.out.println(zdt);
Instant instant = Instant.parse(dateTimeString);
System.out.println(instant);
}
}
Output:
2020-11-16T02:27:39.345Z
2020-11-16T02:27:39.345Z
2020-11-16T02:27:39.345Z
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.
I am pretty new to Java and I am a little stuck with using SimpleDateFormat and Calendar. I have a Date-Object and want to extract a GMT datestring like yyyy-MM-dd HH:mm:ss. I live in Germany and at the moment we are GMT +0200. My Date-Object's time is for example 2011-07-18 13:00:00. What I need now is 2011-07-18 11:00:00. The offset for my timezone should be calculated automatically.
I tried something like this, but I guess there is a fault somewhere:
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(new SimpleTimeZone(timeZone.getOffset(date.getTime()), "GMT"));
sd.setCalendar(cal);
return sd.format(date);
}
On some devices the datestring is returned like I want it to. On other devices the offset isn't calculated right and I receive the date and time from the input date-object. Can you give me some tips or advices? I guess my way off getting the default timezone does not work?
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
return sd.format(date);
}
You don't need to create a new SimpleTimeZone, because you aren't inventing a new timezone - there are 2 existing timezones that come into play in your program, GMT and your default one.
You also don't need to modify your existing date object, because you don't want to represent a different point in time - you only want a different way to display the same point in time.
All you need to do is tell the SimpleDateFormat which timezone to use in formatting.
private String toGmtString(Date date){
//date formatter
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//getting default timeZone
TimeZone timeZone = TimeZone.getDefault();
//getting current time
Calendar cal = Calendar.getInstance()
cal.setTime(date) ;
//adding / substracting curren't timezone's offset
cal.add(Calendar.MILLISECOND, -1 * timeZone.getRawOffset());
//formatting and returning string of date
return sd.format(cal.getTime());
}
java.time
Using java.time, the modern date-time API, there are many ways to do it:
Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using Instant#atZone and UTC timezone.
import java.time.Instant;
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) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Europe/Berlin")).toInstant();
ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using ZonedDateTime#ofInstant and UTC timezone.
import java.time.Instant;
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) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Europe/Berlin")).toInstant();
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant(instant, ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
Using ZonedDateTime#withZoneSameInstant:
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) {
String strDateTime = "2011-07-18 13:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
ZonedDateTime zdtPak = ldt.atZone(ZoneId.of("Europe/Berlin"));
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 2011-07-18 11:00:00
}
}
Using DateTimeFormatter#withZone and ZonedDateTime#withZoneSameInstant:
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 strDateTime = "2011-07-18 13:00:00";
// Using ZoneId.of("Europe/Berlin") for the demo. Change it to
// ZoneId.systemDefault()
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH)
.withZone(ZoneId.of("Europe/Berlin"));
ZonedDateTime zdtPak = ZonedDateTime.parse(strDateTime, dtfInput);
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
System.out.println(zdtUtc.format(dtfOutput)); // 2011-07-18 11:00:00
}
}
Learn more about 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.