-6

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
SJay
  • 15
  • 4
  • 2
    I recommend searching this site (and others) first, and making an attempt. – achAmháin Aug 21 '18 at 13:29
  • 2
    Use `ZonedDateTime` from Java 8. It makes everything easier. – Guillaume F. Aug 21 '18 at 13:30
  • 2
    CEST, Central European is one hour offset, plus day time saving = 2 h. – Joop Eggen Aug 21 '18 at 13:38
  • If you have a Java `Date`, you don't actually have a time zone. That's just how it prints in your local system. A Java `Date` signifies milliseconds since January 1st, 1970 at midnight UTC. In any case it's recommended not to work with `Date` but with the `java.time` classes, and reading up about how to represent or print time zone information. – RealSkeptic Aug 21 '18 at 13:45
  • Thanks for the comments. 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. – SJay Aug 21 '18 at 13:50
  • Denmark (my country) follows Central European Time and also Central European Summer Time (CEST) in summer, and @JoopEggen is correct, this is at offset +0200. So in some sense you have already got what you are asking for. – Ole V.V. Aug 21 '18 at 18:14
  • 2
    @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](http://www.threeten.org/threetenbp/). Follow the link, add the library to your project and start coding. – Ole V.V. Aug 21 '18 at 18:17
  • 1
    @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 '18 at 19:35

2 Answers2

1

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.

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.

Parse

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

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 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(!).

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.


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

java.time, the modern Java date and time API

    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.

Java 8 is not an option

No big problem. java.time has been backported.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described). Link below.
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

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.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • V.V, Thank you for restructuring my update. Appreciate for the comments from everybody. – SJay Aug 22 '18 at 09:31