I have a time range for which both the start time and end time are in UTC.
The local time stays the same for all the days in the date range. That is why if the end date is after daylight time saving is over, the UTC date changes by 1 hour. i.e. suppose the start date is 10/23 and end date is 11/11 at 2 AM US/pacific. So the range I have in UTC becomes 10/23 09:00 AM UTC to 11/11 10:00 AM UTC.
I want to get all the dates in the time range in UTC. If I go on adding one day from start date, after day light time saving the UTC time will remain same and that is why the local time will be off by 1 hour. i.e in the above example if I keep adding one day from 10/23 09:00 after day light time saving the date I will receive is (11/03 09:00 UTC), (11/04 09:00 UTC), (11/05 09 UTC) and so on. Which will be 01:00 AM PST. Is there any way in java to get date time within the time range so that till 11/02 I will get "11/02 09:00 UTC" and from 11/03 onwards I will get "11/03 10:00 UTC"?
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyClass {
public static void main(String args[]) throws Exception{
String date1 = "Wed Oct 23 11:30:00 GMT 2019";
String date2 = "Mon Nov 04 12:30:00 GMT 2019";
convert(date1, date2);
}
private static void convert(String date1, String date2) throws Exception{
SimpleDateFormat localDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
DateTimeFormatter formatter = DateTimeFormat.forPattern("E MMM dd HH:mm:ss z yyyy");
DateTime sDate = formatter.parseDateTime(date1);
DateTime eDate = formatter.parseDateTime(date2);
while(eDate.isAfter(sDate) || eDate.equals(sDate)) {
Date finaldate = sDate.toDate();
System.out.println(localDateFormat.format(finaldate));
sDate = sDate.plusDays(1);
}
}
}
The output for following code is:
Thu Oct 24 11:30:00 GMT 2019
Fri Oct 25 11:30:00 GMT 2019
Sat Oct 26 11:30:00 GMT 2019
Sun Oct 27 11:30:00 GMT 2019
Mon Oct 28 11:30:00 GMT 2019
Tue Oct 29 11:30:00 GMT 2019
Wed Oct 30 11:30:00 GMT 2019
Thu Oct 31 11:30:00 GMT 2019
Fri Nov 01 11:30:00 GMT 2019
Sat Nov 02 11:30:00 GMT 2019
Sun Nov 03 11:30:00 GMT 2019
Mon Nov 04 11:30:00 GMT 2019
But from Nov 03, I am trying to get 12:30 as the time in local timezone stays the same. I am trying to achieve is time so that time in local timezone will be constant. i.e. till Nov 02, 11:30 UTC converts to 4:30 PST. After daylight time saving is over, the time in local time zone will stay the same. So in UTC I am trying to get 12:30 after daylight time saving is over.