gusaindpk
gusaindpk

Reputation: 1253

uk time zone format for the day light saving

I am using GMT+1 for uk time zone in my code. My problem is for daylight saving it will not work. Can i use BST in place of that so that it will automatically take care of the daylight saving.

Upvotes: 2

Views: 2953

Answers (3)

user2357
user2357

Reputation: 472

No, "BST" is not the right name:

assert TimeZone.getTimeZone("BST").getDisplayName().equals("Bangladesh Time");

Upvotes: 2

user96279
user96279

Reputation: 111

I agree with the above answers. To offer some clarity, please see the code below:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;

public class TstELTZ 
{

    public TstELTZ()
    {}
    
    public static void main(String[] args)
    {
        final String tzTitle = "Europe/London";
        
        // Create default time zone & zone Id
        final TimeZone tz = TimeZone.getTimeZone(tzTitle);
        TimeZone.setDefault(tz);
        ZoneId defaultZoneId = ZoneId.systemDefault();

        System.out.println("Time Zone Entry Name = \"" + tzTitle + "\"");
        System.out.println("Time Zone Name       = \"" + tz.getDisplayName() + "\"");
        System.out.println("TZ Observes Daylight Saving = " + tz.observesDaylightTime());
        System.out.println("TZ DS savings = " + tz.getDSTSavings() + " millisecs\n");

        // Create Feb instance of Date from day, month, year info
        LocalDate localDate1 = LocalDate.of(2025, 2, 12);
        ZonedDateTime zonedDateTime1 = localDate1.atStartOfDay(defaultZoneId);
        Date date1 = Date.from(zonedDateTime1.toInstant());

        // Create June instance of Date from day, month, year info
        LocalDate localDate2 = LocalDate.of(2025, 6, 12);
        ZonedDateTime zonedDateTime2 = localDate2.atStartOfDay(defaultZoneId);
        Date date2 = Date.from(zonedDateTime2.toInstant());
        
        // Get the offsets in hours
        int hrsOffset1 = tz.getOffset(date1.getTime()) / 3600000; 
        int hrsOffset2 = tz.getOffset(date2.getTime()) / 3600000;
                        
        // Determine if dates are in a daylight saving period or not.
        boolean isInDaylightSaving1 = tz.inDaylightTime(date1);
        boolean isInDaylightSaving2 = tz.inDaylightTime(date2);

        System.out.println("Date1 = " + date1.toString());
        System.out.println("D1 Offset = " + (hrsOffset1 >= 0 ? "+" : "") + hrsOffset1 + " hour(s)");
        System.out.println("D1 In Daylight Saving = " + isInDaylightSaving1 + "\n");
        
        System.out.println("Date2 = " + date2.toString());
        System.out.println("D2 Offset = " + (hrsOffset2 >= 0 ? "+" : "") + hrsOffset2 + " hour(s)");
        System.out.println("D2 In Daylight Saving = " + isInDaylightSaving2);
        
        System.out.println("\nDone!");      
    }
}

The output of this code gives:

Time Zone Entry Name = "Europe/London"
Time Zone Name       = "Greenwich Mean Time"
TZ Observes Daylight Saving = true
TZ DS savings = 3600000 millisecs

Date1 = Wed Feb 12 00:00:00 GMT 2025
D1 Offset = +0 hour(s)
D1 In Daylight Saving = false

Date2 = Thu Jun 12 00:00:00 BST 2025
D2 Offset = +1 hour(s)
D2 In Daylight Saving = true

Done!

Please note that the time zone of date1 is output as GMT. The time zone of date2 is output as BST.

I have written this code in February. I am not sure if the Display Name will become "British Summer Time" in June or not. I presume it will not.

I hope this helps.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502086

No, you should use the time zone identifier of "Europe/London" as that is the time zone observed by the UK. It should then be accurate for both standard time and daylight saving time.

(As a general note, if you're doing any significant date and time work in Java, it's worth checking out Joda Time - a much nicer API than the built-in one.)

Upvotes: 5

Related Questions