I had to get the current date, add 20 years, and transferred it in an XML object.
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "XMLCommande", propOrder = {
...
"dateLivSouhaitee",
...
})
public class XMLCommande {
...
#XmlElement(name = "date_liv_souhaitee", required = true)
#XmlSchemaType(name = "date")
protected XMLGregorianCalendar dateLivSouhaitee;
...
}
No date format is specified, it's all by default :
XMLCommande xmlMessage = new XMLCommande();
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.add(Calendar.YEAR, 20);
ligne.setDateLivSouhaitee(DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar2));
The problem, is, for some unknown reason, that's sometimes I have a "Z" at the end of the date, but sometimes don't :
<date_liv_souhaitee>2041-05-26Z</date_liv_souhaitee>
<date_liv_souhaitee>2041-05-26+02:00</date_liv_souhaitee>
It's the same server, why sometimes I have the Z with "+02:00" and sometimes don't ?
How can I force the format to be always :
<date_liv_souhaitee>2041-05-26+02:00</date_liv_souhaitee>
The Z in the date-time string 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).
In the other case, you have +02:00 timezone offset added in the date-time string i.e. the corresponding date-time in UTC will be the given date-time minus 2 hours. You can convert the UTC date-time into date-time with +02:00 timezone offset e.g.
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Main {
public static void main(String[] args) throws DatatypeConfigurationException {
ZonedDateTime zdtUtc = LocalDate.of(2041, 5, 26).atStartOfDay(ZoneOffset.UTC);
System.out.println(zdtUtc);
ZonedDateTime zdtOffsetTwoHrs = zdtUtc.withZoneSameInstant(ZoneOffset.of("+02:00"));
System.out.println(zdtOffsetTwoHrs);
GregorianCalendar gregorianCalendar = GregorianCalendar.from(zdtOffsetTwoHrs);
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(gregorianCalendar);
System.out.println(xmlGregorianCalendar);
}
}
Output:
2041-05-26T00:00Z
2041-05-26T02:00+02:00
2041-05-26T02:00:00.000+02:00
Why do I sometimes get the Z and sometimes +02:00?
If both come from creating a GregorianCalendar using the no-arg constructor and converting it to XMLGregorianCalendar, then the best explanation is that someone is modifying the default time zone of your JVM. A part of your own program may do that or some other program running in the same JVM. To demonstrate:
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("Europe/Paris")));
GregorianCalendar gc = new GregorianCalendar();
System.out.println(DatatypeFactory.newInstance().newXMLGregorianCalendar(gc));
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
gc = new GregorianCalendar();
System.out.println(DatatypeFactory.newInstance().newXMLGregorianCalendar(gc));
Output from these code lines was:
2021-05-26T19:41:29.744+02:00
2021-05-26T17:41:29.776Z
new GregorianCalendar() creates a GregorianCalendar that has the default time zone of the JVM at the time of creation. As Arvind Kumar Avinash already explained, offset 0 from UTC is rendered as Z in accordance with the ISO 8601 standard.
How can I force +02:00 always?
I recommend that you use java.time, the modern Java date and time API, for your date work. The OffsetDateTime class represents a date and time with a UTC offset, so just set the offset to +2.
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.ofHours(2));
OffsetDateTime in20Years = now.plusYears(20);
String dateStringWithOffset0200 = in20Years.format(DateTimeFormatter.ISO_OFFSET_DATE);
System.out.println(dateStringWithOffset0200);
2041-05-26+02:00
If you do need an XMLGregorianCalendar, build one from the string we just got:
XMLGregorianCalendar xmlgc = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(dateStringWithOffset0200);
System.out.println(xmlgc);
2041-05-26+02:00
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
I need to find equality of hour, minutes in date (1 date in DST, 1 date after DST) in java. Both dates are in UTC. DST ends in UK 2020-10-26 2 AM, so in above example, hour and minute are equal.
Date1 = 2020-10-22T07:00:00+0000
Date2 = 2020-10-26T08:00:00+0000
ZoneId zoneUk = ZoneId.of("Europe/London");
ZonedDateTime a = ZonedDateTime.parse("2020-10-22T07:00:00+00:00").withZoneSameInstant(zoneUk);
ZonedDateTime b = ZonedDateTime.parse("2020-10-26T08:00:00+00:00").withZoneSameInstant(zoneUk);
Notice the additional : in the time offset or it can't be parsed.
System.out.println(a); // 2020-10-22T08:00+01:00[Europe/London]
System.out.println(b); // 2020-10-26T08:00Z[Europe/London]
System.out.println(a.toLocalTime()); // 08:00
System.out.println(b.toLocalTime()); // 08:00
System.out.println(a.toLocalTime().equals(b.toLocalTime())); // true
Your date-time strings have Zone-Offset of +0000 and therefore parsing them into OffsetDateTime (using an appropriate DateTimeFormatter) will be a more natural choice. Once you have parsed them into OffsetDateTime, convert them to the ZonedDateTime corresponding to the time-zone of the UK. As a final step, you need to get the local time part of the ZonedDateTime.
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// The given date-time strings
String strDate1 = "2020-10-22T07:00:00+0000";
String strDate2 = "2020-10-26T08:00:00+0000";
// Define the formatter for the given date-time strings
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("u-M-d'T'H:m:sZ");
// ZoneId of the UK
ZoneId tzLondon = ZoneId.of("Europe/London");
// Get the corresponding date-time in the UK
ZonedDateTime zdt1 = OffsetDateTime.parse(strDate1, formatter).atZoneSameInstant(tzLondon);
ZonedDateTime zdt2 = OffsetDateTime.parse(strDate2, formatter).atZoneSameInstant(tzLondon);
System.out.println(zdt1);
System.out.println(zdt2);
// Get local date from ZonedDateTime
LocalTime lt1 = zdt1.toLocalTime();
LocalTime lt2 = zdt2.toLocalTime();
System.out.println(lt1);
System.out.println(lt2);
}
}
Output:
2020-10-22T08:00+01:00[Europe/London]
2020-10-26T08:00Z[Europe/London]
08:00
08:00
I have a datetime as 2011-01-11 01:51:10 and timezone as America/Los_Angeles
I want to get a localised date time for this value. This is what I do
val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("y-M-d H:m:s");
val m1: LocalDateTime = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
println("DateTime: " + m1.atZone(ZoneId.of("America/Los_Angeles")))
The value that I get is
DateTime: 2011-01-11T01:51:10-08:00[America/Los_Angeles]
How do I convert it into localized datetime with -08:00 offset applied to it and no [America/Los_Angeles]?
You first have to specify which timezone that the time which you have parsed is in. Then specify an other one to convert into.
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("y-M-d H:m:s");
LocalDateTime m1 = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
ZonedDateTime z1 = m1.atZone(ZoneId.of("UTC"));
ZonedDateTime z2 = z1.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
System.out.println(z2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
Looks like you are using java.time API which has a ZonedDateTime. You should probably use it instead of LocalDateTime, since that LocalDateTime does not have a time zone. From the docs:
A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
And then, ZonedDateTime docs states that:
A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
This class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by a ZoneOffset.
Using a ZonedDateTime, your code would be like:
import java.time._
import java.time.format._
val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)
The result you will see at the console will be:
zdt: java.time.ZonedDateTime = 2011-01-11T01:51:10-08:00[America/Los_Angeles]
That happens because you are using the default toString method of ZonedDateTime and looks like the DateTimeFormatter.ISO_OFFSET_DATE_TIME is exactly what you want. So your code should be:
import java.time._
import java.time.format._
val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)
val formatted: String = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
Please look into my complete answer for this. Answer
String dateTime = "MM/dd/yyyy HH:mm:ss";
String date = "09/17/2017 20:53:31";
Integer gmtPSTOffset = -8;
ZoneOffset offset = ZoneOffset.ofHours(gmtPSTOffset);
// String to LocalDateTime
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(dateTime));
// Set the generated LocalDateTime's TimeZone. In this case I set it to UTC
ZonedDateTime ldtUTC = ldt.atZone(ZoneOffset.UTC);
System.out.println("UTC time with Timezone : "+ldtUTC);
// Convert above UTC to PST. You can pass ZoneOffset or ZoneId for 2nd parameter
LocalDateTime ldtPST = LocalDateTime.ofInstant(ldtUTC.toInstant(), offset);
System.out.println("PST time without offset : "+ldtPST);
// If you want UTC time with timezone
ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdtPST = ldtUTC.toLocalDateTime().atZone(zoneId);
System.out.println("PST time with Offset and TimeZone : "+zdtPST);
probably what you want is to get UTC time and then apply timezone offset to it.
It's quite easy to do with Joda time. For example:
DateTime.now().minus(timezoneOffset)
where timezoneOffset is int that will represent time shift at your location. Correct me if I'm wrong.
how to add user timezone to utc
i am getting utc date like this
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm:ss");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime(getval[2]);
DateTime dateTimeUtc = dateTime.toDateTime(DateTimeZone.UTC);
Now i want to get user Timezone and add it to utc to convert that to localtime
UPDATE
i was able to get the user timezone but could add it to the utc
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm:ss");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime(getval[2]);
java.util.Calendar now = java.util.Calendar.getInstance();
java.util.TimeZone timeZone = now.getTimeZone();
DateTimeZone dtZone = DateTimeZone.forID(timeZone.getID());
DateTime dateTimeUtc = dateTime.toDateTime(DateTimeZone.UTC);
ofm.setDate(dateTimeUtc.toDateTime(dtZone).toDate());
This below code may help you to get the time zone of the user
//get Calendar instance
Calendar now = Calendar.getInstance();
//get current TimeZone using getTimeZone method of Calendar class
TimeZone timeZone = now.getTimeZone();
//display current TimeZone using getDisplayName() method of TimeZone class
System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
also the below link helps you to convert user's timezone to UTC
link
java.time
The Joda-Time project was succeeded by the java.time framework defined in JSR 310. Here is the modern solution using those new classes found in Java 8 and later.
Your input format is nearly compliant with the ISO 8601 standard. The data is just missing the T between the date portion and the time-of-day portion, and is missing a Z on the end to indicate UTC. See if you can educate the publisher of your data about this important standard.
String input = "2019-01-23T01:23:45.123456789Z" ;
The java.time classes use the standard formats by default. So no need to specify a formatting pattern.
Instant instant = Instant.parse( input ) ;
instant.toString() = 2019-01-23T01:23:45.123456789Z
If you can get the input format changed, define a formatting pattern to match.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-ddHH:mm:ss" ) ;
Lacking any indicator of time zone or offset, we must parse as a LocalDateTime. Note that such an object does not represent a moment, is not a specific point on the timeline.
String input = "2019-01-2301:23:45" ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
ldt.toString() = 2019-01-23T01:23:45
You claim to be sure this date and time were intended to represent a moment in UTC. So we can apply an offset using the constant ZoneOffset.UTC to produce a OffsetDateTime.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
odt.toString() = 2019-01-23T01:23:45Z
Then you said you want to adjust this into a specific time zone. Same moment, same point on the timeline, but different wall-clock time.
Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
zdt.toString() = 2019-01-23T02:23:45+01:00[Africa/Tunis]
As you can see, Tunisia on that date was running an hour ahead of UTC. So the time-of-day appears to be 2 AM rather than 1 AM.
Here's a small example that gets the difference from a list of time zones (in hours):
import java.util.Date;
import java.util.TimeZone;
public class StackOverflowTimeZone {
public static void main(String[] a) {
Date date = new Date();
for(int index = 0; index < TimeZone.getAvailableIDs().length; index++) {
System.out.println(TimeZone.getAvailableIDs()[index] + " offset from UTC: " + TimeZone.getTimeZone(TimeZone.getAvailableIDs()[index]).getOffset(date.getTime()) / (60 * 60 * 1000) + " hours.");
}
}
}
The abstract class TimeZone was designed to get the offset of a designated time zone from Coordinated Universal Time (UTC). There is a list of time zones that can be found by using the method TimeZone.getAvailableIDs(). After getting the offset, you will need to do a few small calculuations in order to find out whether the designated time zone is ahead or behind UTC. The sign (+/-) of your output should correlate to whether that designated time zone is ahead or behind UTC.
I need to convert from one timezone to another timezone in my project.
I am able to convert from my current timezone to another but not from a different timezone to another.
For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.
However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.
tl;dr
ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" )) // Current moment in a particular time zone.
.withZoneSameInstant( ZoneId.of( "Asia/Kolkata" )) // Same moment adjusted into another time zone.
Details
The java.util.Date class has no time zone assigned†, yet it's toString implementation confusingly applies the JVM's current default time zone.
Avoid java.util.Date & .Calendar
This is one of many reasons to avoid the notoriously troublesome java.util.Date, .Calendar, and SimpleDateFormat classes bundled with Java. Avoid them. Instead use either:
The java.time package built into Java 8 and inspired by Joda-Time.
Joda-Time
java.time
Java 8 and later has the java.time package built-in. This package was inspired by Joda-Time. While they share some similarities and class names, they are different; each has features the other lacks. One notable difference is that java.time avoids constructors, instead uses static instantiation methods. Both frameworks are led by the same man, Stephen Colbourne.
Much of the java.time functionality has been back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted to Android in the ThreeTenABP project.
In the case of this Question, they work in the same fashion. Specify a time zone, and call a now method to get current moment, then create a new instance based on the old immutable instance to adjust for time zone.
Note the two different time zone classes. One is a named time zone including all the rules for Daylight Saving Time and other such anomalies plus an offset from UTC while the other is only the offset.
ZoneId zoneMontréal = ZoneId.of("America/Montreal");
ZonedDateTime nowMontréal = ZonedDateTime.now ( zoneMontréal );
ZoneId zoneTokyo = ZoneId.of("Asia/Tokyo");
ZonedDateTime nowTokyo = nowMontréal.withZoneSameInstant( zoneTokyo );
ZonedDateTime nowUtc = nowMontréal.withZoneSameInstant( ZoneOffset.UTC );
Joda-Time
Some example code in Joda-Time 2.3 follows. Search StackOveflow for many more examples and much discussion.
DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" );
DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );
DateTime nowLondon = DateTime.now( timeZoneLondon ); // Assign a time zone rather than rely on implicit default time zone.
DateTime nowKolkata = nowLondon.withZone( timeZoneKolkata );
DateTime nowNewYork = nowLondon.withZone( timeZoneNewYork );
DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC ); // Built-in constant for UTC.
We have four representations of the same moment in the timeline of the Universe.
†Actually the java.util.Date class does have a time zone buried within its source code. But the class ignores that time zone for most practical purposes. So, as shorthand, it’s often said that j.u.Date has no time zone assigned. Confusing? Yes. Avoid the mess that is j.u.Date and go with Joda-Time and/or java.time.
Some examples
Convert time between timezone
Converting Times Between Time Zones
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TimeZoneExample {
public static void main(String[] args) {
// Create a calendar object and set it time based on the local
// time zone
Calendar localTime = Calendar.getInstance();
localTime.set(Calendar.HOUR, 17);
localTime.set(Calendar.MINUTE, 15);
localTime.set(Calendar.SECOND, 20);
int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);
// Print the local time
System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second);
// Create a calendar object for representing a Germany time zone. Then we
// wet the time of the calendar with the value of the local time
Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);
// Print the local time in Germany time zone
System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}
}
Date date = new Date();
String formatPattern = ....;
SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
TimeZone T1;
TimeZone T2;
// set the Calendar of sdf to timezone T1
sdf.setTimeZone(T1);
System.out.println(sdf.format(date));
// set the Calendar of sdf to timezone T2
sdf.setTimeZone(T2);
System.out.println(sdf.format(date));
// Use the 'calOfT2' instance-methods to get specific info
// about the time-of-day for date 'date' in timezone T2.
Calendar calOfT2 = sdf.getCalendar();
The "default" time zone can be avoided entirely by just setting the time zone appropriately for the Calendar object. However, I would personally suggest that you use Joda Time as a far superior API for date and time operations in Java. Amongst other things, time zone conversion is very simple in Joda.
It's not clear what your current code looks like and why you're only able to convert via the default time zone, but in Joda Time you'd just specify the time zone explicitly when creating (say) a DateTime object, and then use withZone(DateTimeZone zone).
If you could tell us more about how you're getting input data, we could give a fuller example.
You can use the following code snippet
String dateString = "14 Jul 2014 00:11:04 CEST";
date = formatter.parse(dateString);
System.out.println(formatter.format(date));
// Set the formatter to use a different timezone - Indochina Time
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
System.out.println("ICT time : "+formatter.format(date));
If you don't want to use Joda, here is a deterministic way using the built in libraries.
First off I recommend that you force your JVM to default to a timezone. This addresses the issues you might run into as you move your JVM from one machine to another that are set to different timezones but your source data is always a particular timezone. For example, lets say your data is always PDT/PST time zone, but you run on a box that is set to UTC timezone.
The following code snippet sets the default timezone in my JVM:
//You can either pass the JVM a parameter that
//enforces a TZ: java -Duser.timezone=UTC or you can do it
//programatically like this
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
TimeZone.setDefault(tz);
Now lets say your source date is coming in as PDT/PST but you need to convert it to UTC. These are the steps:
DateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStrInPDT = "2016-05-19 10:00:00";
Date dateInPDT = dateFormat.parse(dateStrInPDT);
String dateInUtc = dateFormatUtc.format(dateInPDT);
System.out.println("Date In UTC is " + dateInUtc);
The output would be:
Date In UTC is 2016-05-19 17:00:00
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*.
Also, quoted below is a notice at the Home Page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
How to switch to the modern API?
Convert java.util.Date to Instant using Date#toInstant e.g.
Date date = new Date();
Instant instant = date.toInstant();
System.out.println(instant); // 2021-05-30T13:10:01.890Z
What's Instant got to do with my requirement?
An Instant represents an instantaneous point on the timeline in UTC. The Z in the sample output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours). Its zero-timezone offset makes it independent of timezones i.e. an instant is the same at every place in the world. It's analogous to water in the physical world.
You can mix a timezone (i.e. ZoneId) with an Instant by calling Instant.atZone to get the corresponding Date-Time in that timezone (i.e. ZonedDateTime).
Similarly, you can mix a timezone offset (i.e. ZoneOffset) with an Instant by calling Instant#atOffset to get the corresponding Date-Time with that timezone offset (i.e. OffsetDateTime).
In the reverse way, you can also get an Instant by calling toInstant on the ZonedDateTime or OffsetDateTime.
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
System.out.println(instant);
// The corresponding Date-Time in Chicago
ZonedDateTime zdtChicago = instant.atZone(ZoneId.of("America/Chicago"));
System.out.println(zdtChicago);
// The corresponding Date-Time in Kolkata
ZonedDateTime zdtKolkata = instant.atZone(ZoneId.of("Asia/Kolkata"));
System.out.println(zdtKolkata);
// The corresponding Date-Time at timezone offset of -05:00 hours
OffsetDateTime odtAtOffsetMinus0500 = instant.atOffset(ZoneOffset.of("-05:00"));
System.out.println(odtAtOffsetMinus0500);
// The corresponding Date-Time at timezone offset of +05:30 hours
OffsetDateTime odtAtOffset0530 = instant.atOffset(ZoneOffset.of("+05:30"));
System.out.println(odtAtOffset0530);
}
}
Output:
2021-05-30T13:44:26.599Z
2021-05-30T08:44:26.599-05:00[America/Chicago]
2021-05-30T19:14:26.599+05:30[Asia/Kolkata]
2021-05-30T08:44:26.599-05:00
2021-05-30T19:14:26.599+05:30
So far you have learnt a simple way to convert an Instant (which you have created directly or obtained from a java.util.Date or a ZonedDateTime or an OffsetDateTime) to a Date-Time in any timezone or at any timezone offset.
Alternatively
There is another way to convert a ZonedDateTime from one timezone to another. Again, there is a similar method to convert an OffsetDateTime from one timezone offset to another.
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) {
// Current Date-Time in Chicago
ZonedDateTime zdtChicago = ZonedDateTime.now(ZoneId.of("America/Chicago"));
System.out.println(zdtChicago);
// The corresponding Date-Time in Kolkata
ZonedDateTime zdtKolkata = zdtChicago.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
System.out.println(zdtKolkata);
// Current Date-Time at a timezone offset of -05:00 hours
OffsetDateTime odtAtOffsetMinus0500 = OffsetDateTime.now(ZoneOffset.of("-05:00"));
System.out.println(odtAtOffsetMinus0500);
// The corresponding Date-Time at timezone offset of +05:30 hours
OffsetDateTime odtAtOffset0530 = odtAtOffsetMinus0500.withOffsetSameInstant(ZoneOffset.of("+05:30"));
System.out.println(odtAtOffset0530);
}
}
Output:
2021-05-30T10:03:59.895923-05:00[America/Chicago]
2021-05-30T20:33:59.895923+05:30[Asia/Kolkata]
2021-05-30T10:03:59.897782-05:00
2021-05-30T20:33:59.897782+05:30
When to use ZonedDateTime and when to use OffsetDateTime?
If you are dealing with a fixed timezone offset value e.g. 02:00 hours, use OffsetDateTime. It is also supported by all JDBC drivers. Check this answer to learn more about it.
If you want the timezone offset to change automatically based on DST, use ZonedDateTime. Unfortunately, ZonedDateTime is not supported by JDBC.
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.
You could use the java.time.ZoneDateTime#ofInstant() method:
import java.time.*;
public class TimeZonesConversion {
static ZonedDateTime convert(ZonedDateTime time, ZoneId newTimeZone) {
return ZonedDateTime.ofInstant(
time.toInstant(),
newTimeZone);
};
public static void main(String... args) {
ZonedDateTime mstTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("-07"));
ZonedDateTime localTime = convert(mstTime, Clock.systemDefaultZone().getZone());
System.out.println("MST(" + mstTime + ") = " + localTime);
}
}
Depends on what you really mean by "converting".
It MAY be as simple as setting the time zone in the FORMATTER, and not mucking with Calendar at all.
Calendar cal = Calendar.getInstance();
TimeZone tzUTC = TimeZone.getTimeZone( "UTC" );
TimeZone tzPST = TimeZone.getTimeZone( "PST8PDT" );
DateFormat dtfmt = new SimpleDateFormat( "EEE, yyyy-MM-dd KK:mm a z" );
dtfmt.setTimeZone( tzUTC );
System.out.println( "UTC: " + dtfmt.format( cal.getTime() ));
dtfmt.setTimeZone( tzPST );
System.out.println( "PST: " + dtfmt.format( cal.getTime() ));
This is not the answer, but could help someone trying to generate dates with same timezone and apply another timezone's offset.
It is useful when your application server is running in one timezone and your database in another.
public static Date toGreekTimezone (Date date) {
ZoneId greek = ZoneId.of(EUROPE_ATHENS);
ZonedDateTime greekDate = ZonedDateTime.ofInstant(date.toInstant(), greek);
ZoneId def = ZoneId.systemDefault();
ZonedDateTime defDate = greekDate.withZoneSameLocal(def);
return Date.from(defDate.toInstant());
}
You can do something like this to get the current time in another time zone.
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.setTimeInMillis(local.getTimeInMillis());
here a story:
my user in US enters a date in a web page. My server gets this as a java.util.Date object. Date objects have no notion of time zone.
so let's say user entered 11PM(== 4AM london time). For her this was 11PM US time.
Your server gets this and interprets this as 11PM of JVM's timezone.
but what you need is a Date object that represents 4AM.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStringInUS = sdf.format("2020-05-04 23:00:00");
SimpleDateFormat dateFormatInUS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat dateFormatInUK = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatInUS.setTimeZone(TimeZone.getTimeZone("America/New_York"));
dateFormatInUK.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date dateInUS = dateFormatInUS.parse(timeStringInUS);
Date dateInUK = sdf.parse(dateFormatInUK.format(dateInUS));
public static String convertTimeBasedOnTimeZoneAndTimePattern(String dateTime,
String fromTimeZone, String toTimeZone, String originalTimePattern, String timePattern) {
DateTimeFormatter formatterNew = DateTimeFormatter.ofPattern(timePattern);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(originalTimePattern);
TemporalAccessor temporalAccessor = formatter.parse(dateTime);
ZoneId z = ZoneId.of(fromTimeZone);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, z);
Instant instant = Instant.from(zonedDateTime);
ZonedDateTime fromZonedDateTime = instant.atZone(ZoneId.of(toTimeZone));
String fromZoneDateTime = fromZonedDateTime.format(formatterNew);
return fromZoneDateTime;}
To convert any time to the specific timezone (for example: UTC -> local timezone and vise versa) with any time pattern you can use java.time library.
This method will take time patterns (original and required format) and timezone (original time zone and required timezone) will give String as output. you can convert String to date by using SimpleDateFormatter or also use parse method of the ZoneDateTime/Instant class.
To convert String to date:
public static final DATE_FORMAT="yyyy-MM-dd HH:mm:ss.SSSSS";
public static Date convertStringToDate(String date) {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
Date parsedDate = null;
try {
parsedDate = formatter.parse(date);
} catch (Exception e) {
throw new DateTimeParseException("Please provide date time in proper format", null, 0, null);
}
return parsedDate;
}
To convert date to String:
public String convertTextDateToDate(Date textDate) {
// SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", //Locale.ENGLISH);
SimpleDateFormat date = new SimpleDateFormat(DATE_FORMAT);
String dateFormatted = date.format(textDate);
return dateFormatted;
}