AMTourky
AMTourky

Reputation: 1200

Java, how to deactivate daylight saving?

Egypt has no more daylight saving, I'm making a web app in a server, and trying to change the time to Egypt time, it worked, but with daylight saving mode, how can I deactivate it!!

Upvotes: 3

Views: 8365

Answers (3)

MagnoCorrea
MagnoCorrea

Reputation: 75

If you are working in an antiquated and restricted setting where upgrading the tz database in Java is not possible, to "deactivate" the DST, you can configure the GMT timezone as your default timezone. You can configure in the JVM arguments

 -Duser.timezone="GMT" 

Or by conding

  TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

Here a code for old Java versions (Java 7 and before) that checks the correctness of this solution. https://www.jdoodle.com/ia/VzO

import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class CheckDSTRemove {
    public static void main(String args[]) {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
        core();
    }
    private static void core() {
        boolean isSafe = true;
        for (int year = 1900; year <= 2100; year++) { //Some Years of interest
            for (int month = 1; month <= 12; month++) {
                int maxDay;
                switch (month) {
                case 1: //To run in any JVM
                case 3: 
                case 5: 
                case 7: 
                case 8: 
                case 12: {
                    maxDay = 31;
                    break;
                }
                case 4: 
                case 6: 
                case 9: 
                case 11: {
                    maxDay = 30;
                    break;
                }
                default://Feb
                    maxDay = 28;
                    if(year%4==0 && year%100 != 0 ) { //I've missing the 400 years rule
                        maxDay = 29;
                    }
                }
                for (int dia = 1; dia <= maxDay; dia++) {
                    String s = oldSchoolDayFormat(year, month, dia)+"000000";//To check the border time

                    Date d = toDate(s);
                    java.sql.Timestamp t = new java.sql.Timestamp(d.getTime());
                    String strHour = (t+"").substring(11, 13);
                    if(!"00".equals(strHour)) {// If in the first day of Dayligth saving, this number will be 01
                        System.out.println(s);
                        isSafe = false;
                    }

                }
            }
        }
        if(isSafe) {
            System.out.println("Success!!!");
        }
    }
    /**
     * Old School format that run in any JVM
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static String oldSchoolDayFormat(int year, int month, int day) {
        String strMonth = (month<10?"0"+month:""+month);
        String strDay = (day<10?"0"+day:""+day);
        return year + strMonth + strDay;
    }

    private static Date toDate(String strDate) {
        Date result = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            result = format.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

Upvotes: -1

jalopaba
jalopaba

Reputation: 8129

Related with Matt's answer, with a Java 1.6.0_31:

Date now = new Date();

TimeZone zoneEET = TimeZone.getTimeZone("EET"); // Traditionally used for Egypt
System.out.println(zoneEET.getDSTSavings());  // 1h for DST
System.out.println(zoneEET.getRawOffset());  // GMT+2
System.out.println(zoneEET.inDaylightTime(now)); // true
TimeZone.setDefault(zoneEET));
System.out.println(now);
System.out.println("");

TimeZone zoneEgypt = TimeZone.getTimeZone("Egypt"));
System.out.println(zoneEgypt.getDSTSavings()); // no DST
System.out.println(zoneEgypt.getRawOffset()); // GMT+2
System.out.println(zoneEgypt.inDaylightTime(now)); // false
TimeZone.setDefault(zoneEgypt));
System.out.println(now);

So it seems that from 1.6.0_26 there is a "special" zone named "Egypt" with no DST.

Upvotes: 2

Matt
Matt

Reputation: 11805

You can't turn off daylight savings in the JVM. Each timezone is encoded with an offset and whether or not daylight savings time is applied (and if so, between what dates).

You can check this with:

TimeZone.getDefault().useDaylightTime();
TimeZone.getDefault().inDaylightTime( new Date() );

However, you can update your timezone files in your JDK installation with the TZUpdater tool:

http://www.oracle.com/technetwork/java/javase/downloads/tzupdater-download-513681.html

PS: Here's the revision of the timezone file where the change for egypt was introduced. There have since been 10 additional updates.

Version         JRE Versions Introduced   TzUpdaterVersion Description

tzdata2011g 1.4.2_33 5.0u31 6u26 7     1.3.39                   Change of DST rules for Egypt to abandon DST this year.

Upvotes: 5

Related Questions