How to convert Clarion (Btrieve) date to Java date and the other way?

Hi,

How to convert Clarion (Btrieve) date to Java date and Java date to Clarion date?

BG: Clarion date is a integer number that indicates the days elapsed from Dec 28, 1800.

So today (05.04.2006 [dd.mm.yyyy] ) is in Clarion 74973.

/ mika
JamixOyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you could try something like this:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.add(Calendar.DATE, clarion - offset);
Date d = cal.getTime();

Java time starts 1/1/1970 so offset you be the number of days since Dec 28 1800 to that date
 
0
CEHJCommented:
Try

class Clarion {
      private static Clarion clarion;

      private static long offset;

      public Clarion() {
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("GMT"));
            cal.set(1800, Calendar.DECEMBER, 28, 0, 0, 0);
            long start = cal.getTime().getTime();
            cal.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
            long end = cal.getTime().getTime();
            offset = end - start;
      }

      public static long clarion(Date d) {
            if (clarion == null) {
                  clarion = new Clarion();
            }
            return (d.getTime() + offset) / (1000 * 60 * 60 * 24);
      }
}
0
JamixOyAuthor Commented:
Thank you, objects.

The offset is: 61730 days.

Would you have a solution how to do it backwards also? I mean conversion from Java date to Clarion date.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

CEHJCommented:
class Clarion {
      private static Clarion clarion;

      private static long offset;

      public Clarion() {
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("GMT"));
            cal.set(1800, Calendar.DECEMBER, 28, 0, 0, 0);// Dec 28, 1800.
            long start = cal.getTime().getTime();
            cal.set(1970, Calendar.JANUARY, 1, 0, 0, 0);// Dec 28, 1800.
            long end = cal.getTime().getTime();
            offset = end - start;
      }

      public static long toClarion(Date d) {
            if (clarion == null) {
                  clarion = new Clarion();
            }
            return (d.getTime() + offset) / (1000 * 60 * 60 * 24);
      }
      
      public static Date fromClarion(long clarion) {
            return new Date((clarion * 1000 * 60 * 60 * 24) - offset);
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
try something like:

clarion = 61730 + date.getTime() / (1000 * 60 * 60 * 24);

you may need to add some rounding checks.
0
CEHJCommented:
If you're unsure of how to call it:

            System.out.println(Clarion.toClarion(new Date()));
            System.out.printf("%tD\n", Clarion.fromClarion(74973L));

printed when i tried it a couple of minutes ago:

74973
04/05/06


0
JamixOyAuthor Commented:
Hi objects and CEHJ,

I'll need to test this, because my own solution fails in the rounding of divided date, giving sometimes wrong dates (+-1 day). It looks like both solutions are working great after a few tests. I will test these properly tomorrow.

Thank you,
Mika
0
CEHJCommented:
Sorry JamixOy - just realised i left calculations in. Of course all you need is 'offset', which is 5333472000000
0
objectsCommented:
for the rounding you need to first define exactly where the daily rollover point is, ie. at what point do you add another day. And round your date accoring to that time. Calendar can be used to do that by setting the time.


0
JamixOyAuthor Commented:
Thank you objects and CEHJ for your fast and accurate answers.

It looks like there is no need for rounding the divided date. I'll split the points for you.

/ mika
0
objectsCommented:
glad i could help :)
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.