Help in CalenderAPI

H

H2O

Hi can any one help me out to convert the current date to julian date
using java api???
i have tryied hard for the solution using GregorianCalender and
SimpleDateFormat but still not able to conver the date to the julian
date

i have used the follwing method to do so ...
GregorianCalendar calendar = new GregorianCalendar();

SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy.MM.dd G 'at' hh:mm:ss z");
Date date = new Date(Long.MAX_VALUE);
System.out.println("Formated date " + sdf.format(date));
calendar.setGregorianChange(date);
Date date1 = calendar.getGregorianChange();
System.out.println("Formated date1 " + sdf.format(date1));

}

but not geting the current date so if any one knw about the same then
plz let me knw ASAP

Thanks and Regards
Tushar
 
I

Ian Wilson

H2O said:
Hi can any one help me out to convert the current date to julian date
using java api???
i have tryied hard for the solution using GregorianCalender and
SimpleDateFormat but still not able to conver the date to the julian
date

i have used the follwing method to do so ...
GregorianCalendar calendar = new GregorianCalendar();

I think you are missing
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy.MM.dd G 'at' hh:mm:ss z");
Date date = new Date(Long.MAX_VALUE);
System.out.println("Formated date " + sdf.format(date));
calendar.setGregorianChange(date);
Date date1 = calendar.getGregorianChange();

You are getting the Julian-Gregorian transition date which you just set.
What you probably wanted is
Date date1 = calendar.getTime();
But Date will give you a Gregorian Date.
System.out.println("Formated date1 " + sdf.format(date1));

Try something like
System.out.println("Julian " + calendar.get(Calendar.YEAR)
+" " + calendar.get(Calendar.MONTH)
+" " + calendar.get(Calendar.DATE));

}

but not geting the current date so if any one knw about the same then
plz let me knw ASAP

I find newsgroups are good if you can wait a day or so for solutions.
 
H

H2O

thanks lan,
i have tryied your suggesion but it was fail to my exceptions
o/p is Julian 2006 10 2

what i expected was
Current Calendar date : 2006 11 1
Julian date for it : 2454041.0 /// as expected result from julian date
converter

for more information about julian date
http://www.aavso.org/observing/aids/jdcalendar.shtml

cureent julian year is 245 and julian day is 4041
the date i m geting using the getGregorianChange is no where same as
julian day or julian year...i was strugling to get it using pure java
api...there are many third party api which i can use but client requied
and trust on jdk only....and as mention on jdk doc the
GregorianCalender need to act as pure julian calender if it for it
using newDate(Long.MaxValue)

even i tryied using simple date format to format the date i get from
getGregorianChange
and date.getTime() but fail to foamt as per the julian astronomy foamat

hope will find the solution using jdk but was doubt abt it..because i
found the bug reported in jdk bud database about it...but nowhere found
the solution


Api Desc :
setGregorianChange
public void setGregorianChange(Date date)Sets the GregorianCalendar
change date. This is the point when the switch from Julian dates to
Gregorian dates occurred. Default is October 15, 1582. Previous to
this, dates will be in the Julian calendar.
To obtain a pure Julian calendar, set the change date to
Date(Long.MAX_VALUE). To obtain a pure Gregorian calendar, set the
change date to Date(Long.MIN_VALUE).


Parameters:
date - the given Gregorian cutover date.
 
H

H2O

I have found some details about the Julian calendar as follows:
The Julian date for 2006: JAN: 01:12:01:59 is 2453737.00138
245 represent the year digits for year 2006
3737 represent the date fir 1 Jan
..00138 represents the time for 12:01:59
Julian date change as per every day 12 noon it increase one digit in
it.
As per ref from
http://www.aavso.org/observing/aids/jdcalendar.shtml
Also chk this calendar where Julian date is 20. October 2006 for 02
November 2006
As per ref from
http://www.calendar.sk/julian_calendar-en.php
I have tried the pure "GregorianCalendar" class from jdk1.4 API and
its setGregorianChange method but not getting as per the expected
Julian date format. Using the "setGregorianChange()" i have setting
the cutover date to Long.MAX_VALUE it means GregorianCalendar now have
to act as per the Julian calendar ...so after setting the cutover date
it return me changed date using "getGregorianChange()" but that was
not the Julian date of the current date...as expected or as per above
both scenario. Even though the last two digits are nowhere equal to the
actual Julian date.

Program
GregorianCalendar cal = new GregorianCalendar();
cal.setGregorianChange(new Date(Long.MAX_VALUE)); // setting the
calendar to act as a pure Julian calendar.

// cal.set(Calendar.DATE, new Date().getDate()); // seting the current
date
// Date todayJD = cal.getGregorianChange(); // getting the changed date
after the setGregorianChange
Date todayJD = cal.getTime(); // getting the calculated time of
today's Julian date
SimpleDateFormat sdfJulianDate = new SimpleDateFormat("yyDDD");
SimpleDateFormat sdfJuliandayOfYear = new SimpleDateFormat("DDD");
System.out.println("today Date = " + new Date());
System.out.println("Today as julian date = " +
sdfJulianDate.format(todayJD));
System.out.println("Today as day of year = " +
sdfJuliandayOfYear.format(todayJD));

OUTPUT:
USING : Date todayJD = cal.getGregorianChange();
Today Date = Thu Nov 02 15:17:05 IST 2006
Today as julian date = 94229
Today as day of year = 229

USING : cal.set(Calendar.DATE, new Date().getDate());
Today Date = Thu Nov 02 15:19:22 IST 2006
Today as julian date = 06319
Today as day of year = 319

USING : Date todayJD = cal.getTime();
Today Date = Thu Nov 02 15:17:59 IST 2006
Today as julian date = 06306
Today as day of year = 306

There is one another concept i found to get the Julian day of the year
as per the Julian day chart mention on nasa site
(http://angler.larc.nasa.gov/armsgp/JulianDayChart.html) and i m
getting the moth of the year that is 306 for nov 02 2006 using
getTime() method in above code then the out put is right for Julian
day. But it was not as per the expected Julian date format. So in
conclusion we can only able to retrieve the day of year for the Julian
calendar. hope their will be a solution for this problem in java api
.....else we allways have to depend upon the third party api that was
not accepteble some times.....

Kindly chk chart on the site

http://angler.larc.nasa.gov/armsgp/JulianDayChart.html
http://weather.uwaterloo.ca/julian.html
http://www.fs.fed.us/raws/book/julian.shtml

Thanks,

Tushar
 
I

Ian Wilson

H2O said:
thanks lan,

Please don't top-post!
i have tryied your suggesion but it was fail to my exceptions o/p is
Julian 2006 10 2

what i expected was Current Calendar date : 2006 11 1 Julian date for
it : 2454041.0 /// as expected result from julian date converter

The term "Julian Date" has many meanings,
http://en.wikipedia.org/wiki/Julian_Date#Alternatives

From your use of yyyy.MM.dd to format your answer, it looked like you
wanted a Julian Calendar date.

Your new example is of a Julian date of the type commonly used in
Astronomy. You could have saved me from wasting my time by being a bit
clearer to start with :)

If you read newsgroup posting guidelines you will see that they
generally suggest you provide the values of both the actual output *and*
your expected output.

http://www.yoda.arachsys.com/java/newsgroups.html Item 4 in Java section.
hope will find the solution using jdk but was doubt abt it..because i
found the bug reported in jdk bud database about it...but nowhere
found the solution

Wikipedia says "The Julian day or Julian day number (JDN) is the
(integer) number of days that have elapsed since Monday, January 1, 4713
BC in the proleptic Julian calendar ["

You know how to create a proleptic Julian calendar so create dates for
January 1, 4713 BC and today's date. Subtract the getTime values and
convert from milliseconds to days. Don't forget that Jan 1 was day zero.
 
H

H2O

Thanks Wilson...
i read the guideline that are realy help full for writing mails.....

what i was expected was the JD Julian Date as follows

2454043.0417 for 2006Nov03

also let me knw more about how to get the date for proleptic Julian
calendar
i have tried the example as follws but still not get what
expected....so can u plz brief me about it more....

Calendar JCal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz
yyyy G");
//Set date to January 1, 4713 BC
JCal.set(Calendar.YEAR, 4713);
JCal.set(Calendar.DAY_OF_YEAR, GregorianCalendar.BC);
JCal.set(Calendar.ERA, 1);

Date dtJD = JCal.getTime();
System.out.println(dtJD);
System.out.println(sdf.format(dtJD));
System.out.println(dtJD.getTime());
System.out.println(JCal.get(Calendar.ERA));

Date dtToday= new Date();
System.out.println(dtToday.getTime());
System.out.println(dtToday.getTime()-dtJD.getTime());



Ian said:
H2O said:
thanks lan,

Please don't top-post!
i have tryied your suggesion but it was fail to my exceptions o/p is
Julian 2006 10 2

what i expected was Current Calendar date : 2006 11 1 Julian date for
it : 2454041.0 /// as expected result from julian date converter

The term "Julian Date" has many meanings,
http://en.wikipedia.org/wiki/Julian_Date#Alternatives

From your use of yyyy.MM.dd to format your answer, it looked like you
wanted a Julian Calendar date.

Your new example is of a Julian date of the type commonly used in
Astronomy. You could have saved me from wasting my time by being a bit
clearer to start with :)

If you read newsgroup posting guidelines you will see that they
generally suggest you provide the values of both the actual output *and*
your expected output.

http://www.yoda.arachsys.com/java/newsgroups.html Item 4 in Java section.
hope will find the solution using jdk but was doubt abt it..because i
found the bug reported in jdk bud database about it...but nowhere
found the solution

Wikipedia says "The Julian day or Julian day number (JDN) is the
(integer) number of days that have elapsed since Monday, January 1, 4713
BC in the proleptic Julian calendar ["

You know how to create a proleptic Julian calendar so create dates for
January 1, 4713 BC and today's date. Subtract the getTime values and
convert from milliseconds to days. Don't forget that Jan 1 was day zero.
 
I

Ian Wilson

H2O said:
i read the guideline that are realy help full for writing mails.....

<snip top-posted follow-up>

You top-posted again and you also failed to post the output of your
code, which isn't a complete example that readers of this newsgroup can
cut, paste and compile without further work.

Maybe you don't know what top-posting is and were too shy to ask?

http://www.catb.org/~esr/jargon/html/T/top-post.html
http://en.wikipedia.org/wiki/Top-posting


In answer to your question:

You used Date.getTime() when I meant GregorianCalendar.getTime() and you
ignored my comment about converting from milliseconds to days. You also
took out your code that creates a proleptic Julian calendar. Calendar
dates are very complex things so I'd make the most of
GregorianCalendar's facilities.

I'll probably ignore top-posted replies :)
 
H

H2O

Ian said:
<snip top-posted follow-up>

You top-posted again and you also failed to post the output of your
code, which isn't a complete example that readers of this newsgroup can
cut, paste and compile without further work.

:) ok lan i got the idea....i wlll not top post hense further...i was
not aware about this term top post....thanks a lot

In answer to your question:

You used Date.getTime() when I meant GregorianCalendar.getTime() and you

Date dtJD = JCal.getTime();
is this not the right approch ??? plz correct me

ignored my comment about converting from milliseconds to days. You also

plz tell me how can i get the days out of milisec...do u mean using
SimpleDateFormat.. ???

SimpleDateFormat inputFormat = new SimpleDateFormat("dd");
date = inputFormat.parse(input);
is this not the right approch ??? plz correct me
took out your code that creates a proleptic Julian calendar. Calendar

here do u mean ??
JCal.setGregorianChange(new Date(Long.MAX_VALUE));
if yes i have added this now
dates are very complex things so I'd make the most of
GregorianCalendar's facilities.

I'll probably ignore top-posted replies :)
plz reply me lan, i found your only one replying....:)

Thanks
Tushar

Program :

GregorianCalendar JCal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz
yyyy G");
JCal.setGregorianChange(new Date(Long.MAX_VALUE));
// January 1, 4713 BC
JCal.set(Calendar.YEAR, 4713);
JCal.set(Calendar.DAY_OF_YEAR, 1);
JCal.set(Calendar.ERA, GregorianCalendar.BC);

Date dtJD = JCal.getTime();
System.out.println("dtJD:" + dtJD);
System.out.println("dtJD getTimeInMillis :" +
JCal.getTimeInMillis());
System.out.println(sdf.format(dtJD));
System.out.println(dtJD.getTime());
System.out.println(JCal.get(Calendar.ERA));

Date dtToday = new Date();
System.out.println(dtToday.getTime());
System.out.println(dtToday.getTime() - dtJD.getTime());

Date formatedDate = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("dd");

String input = String.valueOf(dtToday.getTime() -
dtJD.getTime());

formatedDate = inputFormat.parse(input);

SimpleDateFormat julianFormat = new
SimpleDateFormat("yyyyDDD");
String julianString = julianFormat.format(formatedDate);
System.out.println("date: " + formatedDate);
System.out.println("julian date: " + julianString);
o/p

dtJD getTimeInMillis :-210866784717556
Mon Jan 01 10:38:02 IST 4713 BC
-210866784717556
0
1162876082507
212029660800063
date: Sat Feb 22 00:00:00 IST 30166
julian date: 30166053


FYI
i was expecting the out put as per the following format

GC Julian
20061111 :: 2454051

Yr JDate
245+4051

Ref:
http://wwwmacho.mcmaster.ca/JAVA/JD.html
http://www.aavso.org
 
I

Ian Wilson

H2O said:
Date dtJD = JCal.getTime();
is this not the right approch ??? plz correct me

That is correct. JCal is reference to a GregorianCalendar object, not to
a Date object. I would expect getTime() to be overridden in
GregorianCalendar to take into account all the complicated meddling by
various Roman emperors, Popes and others.
plz tell me how can i get the days out of milisec...do u mean using
SimpleDateFormat.. ???

No I don't.
SimpleDateFormat inputFormat = new SimpleDateFormat("dd");
date = inputFormat.parse(input);
is this not the right approch ??? plz correct me

No. Think about it.

How many milliseconds in a second?
How many seconds in an hour?
How many hours in a day?

Form the above you (and most 9 year olds too) should be able to work out
how many milliseconds in a day. From this you can convert a number of
milliseconds into a number of days.
here do u mean ??
JCal.setGregorianChange(new Date(Long.MAX_VALUE));
Yes.


Program :

Its not a program, you omitted the following:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/*
* The Julian day or Julian day number (JDN) is the (integer)
* number of days that have elapsed since Monday, January 1,
* 4713 BC in the proleptic Julian calendar
*/
public class JulianDate {
public static void main(String[] args) throws ParseException {
GregorianCalendar JCal = new GregorianCalendar();

There is a longstanding convention to use initial Capitals to name
Classes and use initial lowercase to name methods and variables. You
should refactor JCal as jCal. Better would be julianCalendar since it
makes the code more readable.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz
yyyy G");
JCal.setGregorianChange(new Date(Long.MAX_VALUE));
// January 1, 4713 BC
JCal.set(Calendar.YEAR, 4713);
JCal.set(Calendar.DAY_OF_YEAR, 1);
JCal.set(Calendar.ERA, GregorianCalendar.BC);

Date dtJD = JCal.getTime();
System.out.println("dtJD:" + dtJD);
System.out.println("dtJD getTimeInMillis :" +
JCal.getTimeInMillis());
System.out.println(sdf.format(dtJD));
System.out.println(dtJD.getTime());
System.out.println(JCal.get(Calendar.ERA));

Date dtToday = new Date();
System.out.println(dtToday.getTime());
System.out.println(dtToday.getTime() - dtJD.getTime());

At this point you have your Julian date in milliseconds. All you need to
do is convert it to hours ...
Date formatedDate = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("dd");

String input = String.valueOf(dtToday.getTime() -
dtJD.getTime());

formatedDate = inputFormat.parse(input);

SimpleDateFormat julianFormat = new
SimpleDateFormat("yyyyDDD");
String julianString = julianFormat.format(formatedDate);
System.out.println("date: " + formatedDate);
System.out.println("julian date: " + julianString);

The above is hopeless as a way of converting milliseconds to days. Try this:

long mSecPerDay = 24 * 60 * 60 * 1000;
System.out.println("There are " + mSecPerDay + " mS in 1 day.");
float days = (mSec / mSecPerDay);
System.out.println("days " + days);


o/p

dtJD getTimeInMillis :-210866784717556
Mon Jan 01 10:38:02 IST 4713 BC
-210866784717556
0
1162876082507
212029660800063
date: Sat Feb 22 00:00:00 IST 30166
julian date: 30166053


FYI
i was expecting the out put as per the following format

GC Julian
20061111 :: 2454051

Thanks for posting actual and expected output.

Here's what I get from my amendements

There are 86400000 mS in 1 day.
days 2454047.0

Yr JDate
245+4051

The first three digits are not a Year! they are 10000's of days.
 
H

H2O

hi lan

thanks , i have tryied the same and now it work with help one of my
senior coulege thanks to him also...have a look at following
program.....

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class JulianDateSimple {

private static final int MILLIS_PER_SEC = 1000;

private static final int SECS_PER_MIN = 60;

private static final int MINS_PER_HOUR = 60;

private static final int HOURS_PER_DAY = 24;

private static final int MILLIS_PER_DAY = MILLIS_PER_SEC *
SECS_PER_MIN * MINS_PER_HOUR
* HOURS_PER_DAY;


public static void main(String[] args) {
GregorianCalendar julianbaseCal = new GregorianCalendar();
julianbaseCal.clear();
julianbaseCal.set(4713, Calendar.JANUARY, 1, 12, 0, 0);
julianbaseCal.set(Calendar.ERA, GregorianCalendar.BC);
System.out.println("Base = " + julianbaseCal.getTime());

System.out.println("Base Date = " + julianbaseCal.getTime()
+ (julianbaseCal.get(Calendar.ERA) ==
GregorianCalendar.BC ? "BC" : "AD") + " ["
+ julianbaseCal.getTimeInMillis() + " millis]");

GregorianCalendar testCal = new GregorianCalendar();
testCal.clear();
testCal.setGregorianChange(new Date(Long.MAX_VALUE));
testCal.setTime(new Date());
System.out.println("Test = " + testCal.getTime());

System.out.println("Test Date = " + testCal.getTime()
+ (testCal.get(Calendar.ERA) == GregorianCalendar.BC ?
"BC" : "AD") + " ["
+ testCal.getTimeInMillis() + " millis]");

test(julianbaseCal, testCal);

}


private static void test(Calendar julianBaseDate, Calendar
testDate) {

long juliandateMillis = testDate.getTimeInMillis() -
julianBaseDate.getTimeInMillis();
double juliandateDay = juliandateMillis / (double)
(MILLIS_PER_DAY);

System.out.println("Julian Date is " + juliandateDay);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Date wierdness 1
Date/Calendar confusion 0
Date/Calendar confusion 8
How to display in HH:MM:SS... ? 2
Date problem 5
Converters used in the JGoodies Framework 1
Need Java Calendar help 3
TimeZone problem 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top