IsoDate.java

30 views
Skip to first unread message

soltani...@gmail.com

unread,
Oct 9, 2021, 8:59:51 AM10/9/21
to jPOS Users
Hi every one
I have one question about in "parseISODate" Method in "IsoDate.java" Class.
I call this method like follow:
Datedate=parseISODate("0409141751",System.currentTimeMillis(),TimeZone.getDefaul;

outeput:  Sat Apr 09 14:17:51 IRDT 2022

and I call again this method with other parameter  like follow:

Datedate=parseISODate("0410141751",System.currentTimeMillis(),TimeZone.getDefaul;

output: Sat Apr 10 14:17:51 IRDT 2021

After 09 October ,This Method Show 2021 but before 09 October ,This Method Show 2022.
Why does this Method Show different years?

-------------------------------------------
package org.jpos.iso;
public class ISODate {

public static Date parseISODate(String d, long currentTime, TimeZone timeZone) {
int YY = 0;
if (d.length() == 14) {
YY = Integer.parseInt(d.substring(0, 4));
d = d.substring(4);
} else if (d.length() == 12) {
YY = 2000 + Integer.parseInt(d.substring(0, 2));
d = d.substring(2);
}

int MM = Integer.parseInt(d.substring(0, 2)) - 1;
int DD = Integer.parseInt(d.substring(2, 4));
int hh = Integer.parseInt(d.substring(4, 6));
int mm = Integer.parseInt(d.substring(6, 8));
int ss = Integer.parseInt(d.substring(8, 10));
Calendar cal = new GregorianCalendar();
cal.setTimeZone(timeZone);
Date now = new Date(currentTime);
cal.setTime(now);
cal.set(2, MM);
cal.set(5, DD);
cal.set(11, hh);
cal.set(12, mm);
cal.set(13, ss);
cal.set(14, 0);
if (YY != 0) {
cal.set(1, YY);
return cal.getTime();
} else {
Date thisYear = cal.getTime();
cal.set(1, cal.get(1) - 1);
Date previousYear = cal.getTime();
cal.set(1, cal.get(1) + 2);
Date nextYear = cal.getTime();
if (Math.abs(now.getTime() - previousYear.getTime()) < Math.abs(now.getTime() - thisYear.getTime())) {
thisYear = previousYear;
} else if (Math.abs(now.getTime() - thisYear.getTime()) > Math.abs(now.getTime() - nextYear.getTime())) {
thisYear = nextYear;
}

return thisYear;
}
}
}

murtuza chhil

unread,
Oct 9, 2021, 10:09:52 PM10/9/21
to jPOS Users
Ran this jpos 2.1.6 and used your timezone IRDT (Iran) that has a problem. Your default system timezone appears to be that of Iran so all date times are based on that.

```java

        Date date = ISODate.parseISODate("0409141751", System.currentTimeMillis(), TimeZone.getTimeZone("IRDT"));

        System.out.println(date);

        date = ISODate.parseISODate("0410141751", System.currentTimeMillis(), TimeZone.getTimeZone("IRDT"));

        System.out.println(date);

```
Output
```

Sat Apr 09 19:47:51 IST 2022

Sat Apr 10 19:47:51 IST 2021

```
Then ran it with my default IST (India) and it works
```java

        Date date = ISODate.parseISODate("0409141751", System.currentTimeMillis(), TimeZone.getDefault());

        System.out.println(date);

        date = ISODate.parseISODate("0410141751", System.currentTimeMillis(), TimeZone.getDefault());

        System.out.println(date);

```

Output
```

Sat Apr 09 14:17:51 IST 2022

Sun Apr 10 14:17:51 IST 2022

```


When I use IST,(no dst) I would have expected the year to be 2021, but its 2022.
When I use IRDT or US/Central, I would have expected the year to be 2021, but its 2021 and 2022.

Also changing the hour from 14 to 2 to see if the 24 hour clcok time made a difference, it did and it printed both dates with year 2022.
So, something seems off and I currently cannot put a finger on it.
Since there is no year in the date jpos is trying to determine the year. Its math is failing using the previous year, current year and next year to determine where your date year should fall in. The presence of daylight savings time is probably making the issue worse.


If you don't get a solution in this thread, you may want to open an issue on jpos github with all the details.

-chhil

soltani...@gmail.com

unread,
Oct 11, 2021, 6:51:26 AM10/11/21
to jPOS Users
Thanks,
I realized that if difference between "current Date" of system and "input date" of "parseISODate" Method is more than "183" (365/2), ISODate.parseISODate return next year(2022)
but, if difference between "current Date" of system and "input date" of "parseISODate" Method is less than "183" (365/2), ISODate.parseISODate return current year(2021)


Date date=parseISODate("0410141751", System.currentTimeMillis(),TimeZone.getDefault());   // current date is 10/11/2021    difference between "current Date" of system and "input date" of "parseISODate" Method is "184" ( it's    more  than  "183")
System.out.println(date);    

Output : Sun Apr 10 14:17:51 IRDT 2022


Date date=parseISODate("0412141751", System.currentTimeMillis(),TimeZone.getDefault());   // current date is 10/11/2021    difference between "current Date" of system and "input date" of "parseISODate" Method is "182" ( it's   less than "183")
System.out.println(date);    
     
Output : Mon Apr 12 14:17:51 IRDT 2021



But I do not know why this method is implemented in this way?

Best regards



chhil

unread,
Oct 11, 2021, 8:00:02 AM10/11/21
to jpos-...@googlegroups.com
A 6-month approximation was being aimed at for the year. It just could have been any number of months.
At what point in time can one decide if the date is from which year ( a future date without the year, should it be given a prior year ). In conversing with Alejandro (jPOS author), the time when this was written, SAF entries which were a year old could be received.
I personally don't know what the correct behaviour should be, maybe an error percentage weight provided by the user for the guesstimate or maybe the user needs to provide the year :).

Maybe it will be easier to track this with more inputs by creating a github issue.

-chhil

--
--
jPOS is licensed under AGPL - free for community usage for your open-source project. Licenses are also available for commercial usage. Please support jPOS, contact: sa...@jpos.org
---
You received this message because you are subscribed to a topic in the Google Groups "jPOS Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jpos-users/I5nDqmJQTBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jpos-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/97d6f5e2-460d-4a94-aebb-66ce5b4ebb3en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages