Java Date and utcTimeOffset [closed]

Java Date and utcTimeOffset [closed]



I have a Java Date (java.util.Date) - Tue Jul 31 00:53:43 CEST 2018 and a utcTimeOffset = +0200, which says that give, date is +2 hours from UTC.


java.util.Date



This is a legacy code and Java 8 is not an option. What I am getting is text representation of date as 20180730131847 and utcTimeOffset = +0200 from a third-party API.


20180730131847


+0200



I would like to convert this to Danish time. Can someone help me how to do this please.



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





I recommend searching this site (and others) first, and making an attempt.
– achAmháin
Aug 21 at 13:29





Use ZonedDateTime from Java 8. It makes everything easier.
– Guillaume F.
Aug 21 at 13:30


ZonedDateTime





CEST, Central European is one hour offset, plus day time saving = 2 h.
– Joop Eggen
Aug 21 at 13:38





@GuillaumeF. is correct that ZonedDateTime is the good and correct class to use. You don’t need Java 8. java.time, the modern Java date and time API that includes ZonedDateTime, has been backported to Java 6 and 7 in the ThreeTen Backport library. Follow the link, add the library to your project and start coding.
– Ole V.V.
Aug 21 at 18:17


ZonedDateTime


ZonedDateTime





@SarangaJayatilake Thanks for your additional information in response to the comments. It’s always best to add supplementary information in the question itself to keep all information in one place. This time I added it for you.
– Ole V.V.
Aug 21 at 19:35




2 Answers
2


ZoneId danishTime = ZoneId.of("Europe/Copenhagen");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
DateTimeFormatter offsetFormatter = DateTimeFormatter.ofPattern("XX");

String dateTimeString = "20180730131847";
String offsetString = "+0200";
ZoneOffset offset = ZoneOffset.from(offsetFormatter.parse(offsetString));
ZonedDateTime dateTime = LocalDateTime.parse(dateTimeString, dateTimeFormatter)
.atOffset(offset)
.atZoneSameInstant(danishTime);
System.out.println("Danish time: " + dateTime);



Output from this code is:



Danish time: 2018-07-30T13:18:47+02:00[Europe/Copenhagen]



The time zone to use for Denmark is Europe/Copenhagen. While the Faroe islands and Greenland use other time zones and are in a national community (“rigsfællesskab”) with Denmark under the same queen, they are not part of Denmark proper, so can be ignored when Danish time is asked for. Since Danish summer time agrees with your example offset of +0200, in this case we get the same time out as we put in. With a date in winter, for example, this would not have been the case because Danish standard time is at offset +0100.



No big problem. java.time has been backported.


org.threeten.bp



In the backport the classes are in package org.threeten.bp with subpackages, for example org.threeten.bp.ZoneId and org.threeten.bp.format.DateTimeFormatter.


org.threeten.bp


org.threeten.bp.ZoneId


org.threeten.bp.format.DateTimeFormatter


java.time





V.V, Thank you for restructuring my update. Appreciate for the comments from everybody.
– SJay
Aug 22 at 9:31



tl;dr



Your input is in a poor format, but in this case can be parsed using the modern java.time classes. Note that unfortunate use of 3-4 letter pseudo-zone such as CEST are not standardized and are not unique so they cannot always be parsed as your intended zone.


CEST



For Java 8 and later, these classes are built-in. For Java 6 & 7, see the ThreeTen-Backport project detailed below.


ZonedDateTime.parse( // Parse an input string as a date-time moment.
"Tue Jul 31 00:53:43 CEST 2018" ,
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu", Locale.US ) // Specify `Locale` to determine human language and cultural norms to be used in translation.
) // Returns a `ZonedDateTime` object.
.withZoneSameInstant( // Adjust from one zone to another.
ZoneId.of( "Europe/Copenhagen" ) // Always use `Continent/Region` time zone names, never 3-4 letter pseudo-codes such as `CEST`.
)
.toString() // Generate a `String` with text in standard ISO 8601 format wisely extended by appending the name of the time zone in square brackets.



2018-07-31T00:53:43+02:00[Europe/Copenhagen]



java.time



This has been covered many times already, so search Stack Overflow for more discussion.


String input = "Tue Jul 31 00:53:43 CEST 2018";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu", Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );



zdt.toString(): 2018-07-31T00:53:43+02:00[Europe/Paris]



Adjust to a Denmark time zone. I will arbitrarily choose the Europe/Copenhagen. Denmark may have other zones, such as for Greenland or other parts of the kingdom.


Europe/Copenhagen



Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).


continent/region


America/Montreal


Africa/Casablanca


Pacific/Auckland


EST


IST


ZoneId z = ZoneId.of( "Europe/Copenhagen" ) ;
ZonedDateTime zdtCopenhagen = zdt.withZoneSameInstant( z ); // Same moment, different wall-clock time.



2018-07-31T00:53:43+02:00[Europe/Copenhagen]



Notice that on that date and hour, Paris and Copenhagen time zones shared the same offset-from-UTC, so they perceive the same time-of-day.



ISO 8601



Your input string is using a terrible format. It is difficult to parse, assumes English language, and includes redundant information.



When exchanging date-time values as text, use only the standard ISO 8601 formats.



Note that the ZonedDateTime class wisely extends that format to append the name of the time zone in square brackets. This is seen in output above.


ZonedDateTime



About java.time



The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.


java.util.Date


Calendar


SimpleDateFormat



The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.


java.sql.*



Where to obtain the java.time classes?



The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Interval


YearWeek


YearQuarter

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)