Local time validation with daylight - java

We have a requirement in java to validate the local time before it is passed to scheduler like quartz. System receives London local time say 01:30 AM but this time in not valid on March 26 2017 (daylight savings).
How do I write a code to output below results?
Input
12:30, 01:30, 02:30
Output
Mar 26 2017 12:30 -> Valid, GMT
Mar 26 2017 01:30 -> Invalid, NA
Mar 26 2017 02:30 -> Valid, BST

Assuming that you start with a LocalDateTime then you can test to see if it's valid for scheduling by seeing if a given timezone reports that there are valid offsets for the time. This info is available in the ZoneRules class.
Here's some sample code:
#Test
public void test() throws Exception {
TimeZone tz = TimeZone.getTimeZone("Europe/London");
ZoneId londonZone = tz.toZoneId();
String springAhead = "2017-03-26T01:30";
assertFalse(isValidForScheduling(londonZone, LocalDateTime.parse(springAhead)));
String fallBack = "2017-10-29T01:30";
assertFalse(isValidForScheduling(londonZone, LocalDateTime.parse(fallBack)));
}
private boolean isValidForScheduling(ZoneId zoneId, LocalDateTime ldt) {
ZoneRules rules = zoneId.getRules();
List<ZoneOffset> validOffsets = rules.getValidOffsets(ldt);
return validOffsets.size() == 1;
}
If the list of validOffsets is empty then it's not a valid time. If there is more than 1 entry then the time occurs multiple times in that zone (the case of putting the clocks back). If there's a single entry, then it's a regular time.
You'd probably want to fail on an empty list and warn on a list with multiple entries.

The below code errors only for March 26 2017 1AM-2AM. I didnt like the way it is written. Can it be refactored? This doesn't error on Oct 29 2017.
LocalDateTime localDateTime = LocalDateTime.of(2017, 3, 26, hours, minutes);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/London"));
if ( zonedDateTime.getHour() != localDateTime.getHour() ) {
System.out.println("MISMATCH!!!");
}

Related

ZonedDateTime timezone inconsistency issue?

I need to write one function which will create the future installments for the invoice. Below is the function which creates the list of future installment dates :-
public List<Date> getInstallmentDates(Invoice objectWithInvoiceDateField, int noOfInstallments, String instFreq)
{
//objectWithInvoiceDateField.getInvoiceDate this will return java.util.Date instance
ZonedDateTime invoiceDate = objectWithInvoiceDateField.getInvoiceDate.toInstant().atZone(ZoneId.systemDefault());
ZonedDateTime firstInstallment = ZonedDateTime.of( invoiceDate.getYear(), invoiceDate.getMonthValue() , invoiceDate.getDayOfMonth() , 0 , 0 , 0, 0 , ZoneId.systemDefault());
List<Date> installmentDates = new ArrayList();
installmentDates.add(Date.from(firstInstallment.toInstant()));//First Installment
/*Code for the subsequent installments*/
for (int i = 1; i < noOfInstallments; i++) {
ZonedDateTime subsequentInstallments = null;
if(instFreq.equalsIgnoreCase("Quarterly")) {
subsequentInstallments = firstInstallment.plusMonths(3*i);
}
else if(instFreq.equalsIgnoreCase("Semi-annual")){
subsequentInstallments = firstInstallment.plusMonths(6*i);
}
else
subsequentInstallments = firstInstallment.plusMonths(i);
installmentDates.add(Date.from(subsequentInstallments.toInstant()));
}
return installmentDates;
}
This works as expected except for the last iteration. Below is the output if I run this method from main method for
getInstallmentDates(invoice, 5, "Monthly");
Thu Jul 30 00:00:00 EDT 2020
Sun Aug 30 00:00:00 EDT 2020
Wed Sep 30 00:00:00 EDT 2020
Fri Oct 30 00:00:00 EDT 2020
Mon Nov 30 00:00:00 ***EST*** 2020
Can some one please help me understand why the timezone for last instance is changed to EST ?
Thanks in advance!
Presumably because you have used the timezone to be ZoneId.systemDefault(), and your system defaults to a timezone that honours daylight saving time. Assuming EDT is Eastern Daylight Time and EST is Eastern Standard Time, in 2020 the end of daylight saving happens on 1 November and therefore the timezone name changes.

Converting Date to LocalDate returning strange results around 200AD

I'm getting inconsistent results when converting Dates to LocalDates, around the year 200. Using the following code to do the conversion:
private LocalDate toLocalDate(Date localDate)
{
return LocalDateTime.ofInstant(localDate.toInstant(), ZoneId.systemDefault()).toLocalDate();
}
My ZoneId.systemDefault() is Africa/Harare, which matches the CAT used in the test. The test case I run is
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String dateString = "Tue Jan 01 00:00:00 CAT 200";
String dateString2 = "Tue Jan 01 00:00:00 CAT 201";
String dateString3 = "Wed Dec 31 00:00:00 CAT 200";
System.out.println(toLocalDate(simpleDateFormat.parse(dateString)));
System.out.println(toLocalDate(simpleDateFormat.parse(dateString2)));
System.out.println(toLocalDate(simpleDateFormat.parse(dateString3)));
My expected output for this would be
0200-01-01
0201-01-01
0200-12-31
Or, if not that, at least consistently incorrect values. The actual results are
0199-12-31
0201-01-01
0200-12-31
So it seems that the first one is being rolled back slightly, possibly the two hours corresponding to the CAT timezone? But why does this only happen on the one case? Doing the same experiment with the year 2000 does not produce the same error.
Stephen has provided an explanation in the comment. Basically, java.util.Date uses a calendar system which cuts over between the Julian calendar system and the Gregorian calendar system in 1582, skipping 10 days. So dates in 1582 or before will exhibit discrepancies - but the size of the discrepancy will vary over time - by 3 days every 400 years, on average. It so happens that between 200 and 400AD, you don't see this because that corresponds to when the discrepancy is 0.
Here's a short but complete program to demonstrate the problem:
import java.time.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
// Value obtained with Noda Time: should be 0199-12-31T22:00:00Z.
long millis = -55855792800000L;
Instant instant = Instant.ofEpochMilli(millis);
Date date = new Date(millis);
System.out.println(instant);
System.out.println(date);
}
}
Output on my machine:
0199-12-31T22:00:00Z
Tue Jan 01 22:00:00 GMT 200
This is all complicated by the problems in your initial code of assuming CAT and Africa/Harare are the same (at that point in time, Africa/Harare is regarded as having an offset of +02:10) and the incorrect day names in your strings - but it's the bug in Java which is causing the issue here.
I suggest you perform all your parsing using the java.time.format classes - then I'd hope you won't get this inconsistency.

Why the timestamp parsed by YAML is not correct?

Yaml file:
!!test.User
timestamp: 2012-11-22T01:02:03.567Z
Java class:
package test;
public class User {
public Date timestamp;
}
Parse it with snakeyaml:
String str = "2012-11-22T01:02:03.567Z";
// parse it manually
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(str);
System.out.println("manually: " + date);
// parse it by snakeyaml
Yaml yaml = new Yaml();
yaml.setBeanAccess(BeanAccess.FIELD);
InputStream input = new FileInputStream("C:\\test.yaml");
User myUser = yaml.loadAs(input, User.class);
System.out.println("by Yaml: " + user.timestamp);
It prints:
manually: Thu Nov 22 01:02:03 CST 2012
by Yaml: Thu Nov 22 09:02:03 CST 2012
You can see they are different. Why?
Your manual method gets the time in the current time zone, but it is actually expressed in UTC (as indicated by the Z time zone). So actually the value parsed by Yaml seems to be the correct one.
I am puzzled about the actual value though, CST (Central Standard Time USA) should be 6 hours behind UTC, but you get 8 hours ahead.

How to convert CET time to local time on device?

I have three different times:
time on server - "Wed, 19 Feb 2014 11:44 CET"
time of start meeting on server - "12:00h"
time on device- "13:49"
I need to get time of start meeting on device ...this-> "14:00h" or time to meeting this-> "11m"
I'm trying to get it by using :
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String gmt_time="12:00h";
SimpleDateFormat format = new SimpleDateFormat("HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d_date = null;
d_date = format.parse(gmt_time);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date fromGmt = new Date(d_date.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
String new_date=format.format(fromGmt);
But result in new_date is "15:00h" (I need "14:00h")
You assumption of CET = GMT on Wed, 19 Feb 2014 seems to be incorrect - refer to here.
CET is an hour ahead of GMT
11:44 CET would mean 10:44 GMT
Hence, when you calculate an offset from GMT time and your local time, it adds an hour to it.
Change format.setTimeZone(TimeZone.getTimeZone("GMT")); to format.setTimeZone(TimeZone.getTimeZone("CET")); and it should work as expected.
Found quickly solution
String cet_time="12.11.14"+" "+"12:00h";
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy' 'HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("CET"));
Date d_date = format.parse(cet_time);
( new SimpleDateFormat("dd.MM.yy")).format(fromGmt);//new date String
( new SimpleDateFormat("HH:mm'h'")).format(fromGmt);//new time String

Grab exact date without timezone interfering with XMLGregorianCalendar in Java

I have a XMLGregorianCalendar that I would like to convert to a Java Date object, but when I try to covert this:
2013-11-19T00:00:00-00:00
I always get a date with the value a day behind.
Mon Nov 18 17:00:00 MST 2013
I just want a date object containing 11/19/2013.
As commented above, the result you're getting is right - it's the same moment in time. Midnight UTC is 5PM MST the day before. Perhaps you should look into why your time is in "-00:00" instead of "-07:00" (MST)... but in the meanwhile, I suppose you could try this:
public static void main(String... args) throws DatatypeConfigurationException {
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-11-19T00:00:00-00:00");
Calendar c = xcal.toGregorianCalendar();
c.setTimeZone(TimeZone.getDefault());
Date d = c.getTime();
System.out.println(d);
}
prints out Tue Nov 19 00:00:00 EST 2013, and will work for other times of day, not just midnight.
I just did this,
Date startDate = new Date(request.getStartTime().getYear(), request.getStartTime().getMonth(), request.getStartTime().getDay(), 0, 0, 0);

Categories