Daylight Saving Time Ends method | Core Java Forum
M
Mustapha Rhouate Posted on 22/01/2021

Hi
I am trying to write step definition in java to calculate what daylith saving end for any given year?

like if I enter 2025 it will give me Nov 2, 2025 - Daylight Saving Time Ends

anybody has any idea?
Thanks in advance


M
Mustapha Rhouate Replied on 22/01/2021

    @And("^I generate date time of format \"([^\"]*)\" \"([-|+]?\\d+)\" days from today and save it to \"([^\"]*)\"$")
    public void iGenerateDateTimeOfFormatDaysFromToday(String format, int delta,String date) throws Throwable {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date currentDate = new Date();
        String Date = dateFormat.format(addDays(currentDate,delta));
        SetToGlobal.run(Date,date);
    }


M
Replied on 15/07/2021

Check this. It may help:

 

package com.programming.class8;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;
import java.util.Date;
import java.util.TimeZone;

public class DST {

	public static void main(String[] args) {

		ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Montreal")); // Represent a specific time zone, the
																				// history of past, present, and future
																				// changes to the offset-from-UTC used
																				// by the people of a certain region.
		ZoneId z = now.getZone();
		ZoneRules zoneRules = z.getRules(); // Obtain the list of those changes in offset.
		Boolean isDst = zoneRules.isDaylightSavings(now.toInstant()); // See if the people of this region are observing
																		// Daylight Saving Time at a specific
																		// moment.Specify the moment. Here we capture
																		// the current moment at runtime.Returns a
																		// boolean.

		System.out.println(isDst); // Is Day light saving enabled or not

		// Getting the time in different time zone
		LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 15, 12, 55);

		ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
		ZoneId montrealZoneId = ZoneId.of("America/Montreal");

		ZonedDateTime tokyoZonedDateTime = localDateTime.atZone(tokyoZoneId);

		// different zone, retaining the instant
		ZonedDateTime montrealZonedDateTime = tokyoZonedDateTime.withZoneSameInstant(montrealZoneId);
		System.out.println(montrealZonedDateTime);

		/**
		 * DST = Daylight Saving Time (Begins Daylight Saving Time at 2:00 a.m. on the
		 * second Sunday in March and reverts to standard time on the first Sunday in
		 * November)
		 * Time with our without day light saving
		 */
		
		SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
		SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'+|-hh:mm");
		String strWithDLS = "10/31/2021"; // MM/DD/YYYY
		String strWithOutDLS = "11/01/2021";
		
		try {
			Date dateWithDLS = sourceFormat.parse(strWithDLS);
			Date dateWithOutDLS = sourceFormat.parse(strWithOutDLS);
			System.out.println("Default Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS));
			System.out.println("Default Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS));

			/**
			 * Set time zone with EST (No difference in time) because 3 letter time zone not
			 * consider DST (Day light saving time)
			 */
			sourceFormat.setTimeZone(TimeZone.getTimeZone("EST"));
			dateWithDLS = sourceFormat.parse(strWithDLS);
			dateWithOutDLS = sourceFormat.parse(strWithOutDLS);
			System.out.println("EST Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS));
			System.out.println("EST Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS));

			/**
			 * Set time zone with America/New_York (1 hour difference in time) when you need
			 * to use day light saving always use with continent/region format.
			 */
			sourceFormat.setTimeZone(TimeZone.getTimeZone(ZoneId.of("America/New_York")));
			dateWithDLS = sourceFormat.parse(strWithDLS);
			dateWithOutDLS = sourceFormat.parse(strWithOutDLS);
			System.out.println("America/New_York Timezone with Day Light Saving :" + targetFormat.format(dateWithDLS));
			System.out.println(
					"America/New_York Timezone without Day Light Saving :" + targetFormat.format(dateWithOutDLS));

		} catch (ParseException ex) {
			ex.printStackTrace();
		}

	}

}