Solved

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

Posted on 2006-04-05
747 Views
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
Question by:JamixOy

LVL 92

Expert Comment

you could try something like this:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
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

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

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

LVL 86

Accepted Solution

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

try something like:

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

you may need to add some rounding checks.
0

LVL 86

Expert Comment

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

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

Sorry JamixOy - just realised i left calculations in. Of course all you need is 'offset', which is 5333472000000
0

LVL 92

Expert Comment

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

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

0

LVL 86

Expert Comment

:-)
0

## Featured Post

### Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.