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.
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.
I would like to convert this to Danish time. Can someone help me how to do this please.
java.time, the modern Java date and time API
Output from this code is:
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.
Java 8 is not an option
No big problem. java.time has been backported.
org.threeten.bp
and subpackages.In the backport the classes are in package
org.threeten.bp
with subpackages, for exampleorg.threeten.bp.ZoneId
andorg.threeten.bp.format.DateTimeFormatter
.Links
java.time
.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.For Java 8 and later, these classes are built-in. For Java 6 & 7, see the ThreeTen-Backport project detailed below.
java.time
This has been covered many times already, so search Stack Overflow for more discussion.
Parse
Adjust
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.Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).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.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
.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.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.