In the summer Ireland use IST, UTC+01:00 and Greenwich Mean Time in the winter. I get datetime without any GMT information from a source, and it's from Ireland. I need to convert it into GMT+0. But the challenge is Ireland timezone is different in different seasons.
5 Answers
You can’t (always)
Just to spell out what I think talex meant in the comment. On the last Sunday in October Ireland switches from summer time to standard time. The clocks in Ireland are turned back from 2 AM to 1 AM. So an hour after it was 1 AM it is again 1 AM. The times repeat. This is known as an overlap. (The opposite, when clocks are turned forward, is called a gap.) The implication? If for example the time you get is given as 1:30 in that night, you don’t know whether it’s from the first time it was 1:30 — 1:30 IST — or the second time it was 1:30 — 1:30 GMT. Or in code:
String dateTimeStr = "2021-10-31T01:30:00"; LocalDateTime ldt = LocalDateTime.parse(dateTimeStr); System.out.println(ldt.atZone(IRELAND).withEarlierOffsetAtOverlap()); System.out.println(ldt.atZone(IRELAND).withLaterOffsetAtOverlap());I used this constant:
private static final ZoneId IRELAND = ZoneId.of("Europe/Dublin");And the output is:
2021-10-31T01:30+01:00[Europe/Dublin] 2021-10-31T01:30Z[Europe/Dublin]
For timestamps not originating from those two hours in that night you will not have any problem.
To check whether you have an ambiguous timestamp compare the two ZonedDateTime objects that I printed in the above code. For the vast majority of cases they will be equal. Take for example:
String dateTimeStr = "2021-10-07T18:48:00";12021-10-07T18:48+01:00[Europe/Dublin] 2021-10-07T18:48+01:00[Europe/Dublin]
I would suggest you using ZonedDateTime (reference documentation) and as ZoneId you should use Europe/Dublin. It should observe the timezone changes according to seasons.
If You are sure the time is from Ireland You can hardcode ZoneId like: ZoneId.of("Europe/Dublin")
And then parse the time for Ireland like this:
LocalDateTime ldt = LocalDateTime.parse("2018-07-01T08:00");
ZoneId zoneId = ZoneId.of("Europe/Dublin");
ZonedDateTime zdt = ZonedDateTime.of(ldt, zoneId); More on ZonedDateTime here:
The existing answers are good. However, they have missed an important piece of information that the OP needs, and that information is:
The ZonedDateTime has been designed to adjust the timezone offset automatically.
It means that depending on whether the Date-Time is a summer/winter time, you will find it reflecting different timezone offsets (of course, if DST is applicable).
Demo:
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Europe/Dublin"); ZonedDateTime zdtSummerIST = LocalDate.of(2021, 6, 15).atStartOfDay(zoneId); ZonedDateTime zdtWinterIST = LocalDate.of(2021, 11, 15).atStartOfDay(zoneId); System.out.println(zdtSummerIST); System.out.println(zdtWinterIST); }
}Output:
2021-06-15T00:00+01:00[Europe/Dublin]
2021-11-15T00:00Z[Europe/Dublin]The Z in the output 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).
How do I convert a Date-Time string without timezone to ZonedDateTime?
Parse the Date-Time string without timezone into LocalDateTime which can be converted into a ZonedDateTime using LocalDateTime#atZone.
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) { ZoneId zoneId = ZoneId.of("Europe/Dublin"); String input = "07/10/2021 18:50:15"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss", Locale.ENGLISH); LocalDateTime ldt = LocalDateTime.parse(input, dtf); ZonedDateTime zdt = ldt.atZone(zoneId); System.out.println(zdt); }
}Output:
2021-10-07T18:50:15+01:00[Europe/Dublin]Learn more about the modern Date-Time API* from Trail: Date Time.
* 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. Note that Android 8.0 Oreo already provides support for java.time.
tl;dr
ZonedDateTime
.of( LocalDate.parse( "2022-06-23" ) , LocalTime.parse( "15:30" ) , ZoneId.of( "Europe/Dublin" )
)
.toInstant()
.atOffset( ZoneOffset.UTC
)See this code run live at IdeOne.com. We see that in the summer next year, the people of Ireland will have their clocks set to one hour ahead of UTC: 15:30 in Dublin time will be 14:30 in UTC time.
2022-06-23T14:30Z
Details
Your title asks:
How to know current timezone of Ireland in a given time in java
Ireland 🇮🇪 has only one time zone, per my reading of this Wikipedia article: Europe/Dublin.
You are confusing offsets with zones.
An offset-from-UTC is simply a number of hours-minutes-seconds ahead of, or behind, the modern temporal meridian.
A time zone is much more. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by their politicians.
A real time zone is named in Continent/Region format. The 2-4 character pseudo-zones such as IST are not time zones, and should be avoided as they are not standardized and are not even unique! To some, IST is Ireland Standard Time, while to others IST is India Standard Time.
You said:
I get datetime without any GMT information from a source, and it's from Ireland.
So take your date and time-of-day and add the time zone you presume was intended. You get a ZonedDateTime to represent a moment, a specific point on the timeline.
LocalDate ld = … ;
LocalTime lt = … ;
ZoneId z = ZoneId.of( "Europe/Dublin" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;Adjust to UTC (an offset of zero hours-minutes-seconds) by extracting a Instant.
Instant instant = zdt.toInstant() ;For more flexibility such as generating text in various formats, convert to OffsetDateTime.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;