?
Solved

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

Posted on 2006-04-05
12
Medium Priority
?
902 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:JamixOy
  • 5
  • 4
  • 3
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16380423
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16380581
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
 

Author Comment

by:JamixOy
ID: 16380587
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
CEHJ earned 700 total points
ID: 16380654
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
 
LVL 92

Assisted Solution

by:objects
objects earned 700 total points
ID: 16380697
try something like:

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

you may need to add some rounding checks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16380721
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
 

Author Comment

by:JamixOy
ID: 16380825
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16380826
Sorry JamixOy - just realised i left calculations in. Of course all you need is 'offset', which is 5333472000000
0
 
LVL 92

Expert Comment

by:objects
ID: 16380847
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
 

Author Comment

by:JamixOy
ID: 16389422
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
 
LVL 92

Expert Comment

by:objects
ID: 16389451
glad i could help :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16389532
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question