Java Forum / General / May 2006
How to convert a Date to int
Terry Jolly - 04 May 2006 16:52 GMT I'm new to Java, have been using vb6.
How do I do this in java?
VB6:
Dim lDate as Long
lDate = CLng(Date)
Java:
int lDate;
then what?
Thanks In Advance.
Jan Thomä - 04 May 2006 17:21 GMT > I'm new to Java, have been using vb6. > [quoted text clipped - 13 lines] > > Thanks In Advance. You have to use a long as well. I guess the long holds the number of milliseconds in the current era in vb, so the equivalent in java would be:
long l = System.currentTimeMillis();
Greetings, Jan
Bjorn Abelli - 04 May 2006 18:29 GMT >> I'm new to Java, have been using vb6. >> [quoted text clipped - 19 lines] > > long l = System.currentTimeMillis(); Though correct from a Java perspective, it doesn't correspond to the VB6 Date, which internally is represented by a *double*, where the fraction is the time. The "integral" part is hence the number of *days* since its epoch: 1 January 1900
The question should rather be:
From where are you getting that int, and how are you going to use the date?
// Bjorn A
Terry Jolly - 04 May 2006 19:24 GMT The database i'm writing too stores dates as int32 --05/04/2006 would equal 38841. Just like MS Excel would display the a date if formated as a number with zero decimal places.
>>> I'm new to Java, have been using vb6. >>> [quoted text clipped - 34 lines] > Inviato da X-Privat.Org - Registrazione gratuita > http://www.x-privat.org/join.php Terry Jolly - 04 May 2006 18:58 GMT Jan,
Thanks for the info. However it date number needs to be for example:
05/04/2006 would equal 38841. Which, what is stored in the database. Just like MS Excel would display the a date if formated as a number with zero decimal places.
> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 [quoted text clipped - 33 lines] > =bbgk > -----END PGP SIGNATURE----- Lasse Reichstein Nielsen - 04 May 2006 19:22 GMT (please don't top post)
> 05/04/2006 would equal 38841. I assume that date means the 4th of May.
Based on this, it appears the number is based such that 1 corresponds to the 1st of January 1900.
Anyway, a simple way to get the 38841'th day after 1st of January 1900 is: var theDate = new Date(1900,0,38841);
> Which, what is stored in the database. Just > like MS Excel would display the a date if formated as a number with zero > decimal places. The results given by Excel and Javascript differ, since they disagree on whether there was a 29th of February 1900. Excel says yes, Javascript no. Javascript appears to be right by the Gregorian calendar :) <URL:http://www.tondering.dk/claus/cal/node3.html#SECTION00321000000000000000>
To compensate, you might want to subtract one to numbers >=61 before passing them to Javascript (assuming all dates are in the range 1900..2099, otherwise you'll have to figure out what other discrepancies there might be)
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Lasse Reichstein Nielsen - 04 May 2006 19:41 GMT > Anyway, a simple way to get the 38841'th day after 1st of January 1900 > is: > var theDate = new Date(1900,0,38841); Whoops, forgot what newsgroup I was in. The method still holds, the format is just a little different:
java.util.Calendar cal = new java.util.GregorianCalendar(); cal.setLenient(true); cal.set(1900,0,38841); java.util.Date theDate = cal.getTime();
The result is still the 5th of May, not the 4th as in Excel.
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Terry Jolly - 04 May 2006 19:56 GMT This also poses a problem. The database has roughly 30 tables, with close to 1m records and almost each has at least one field using an int to store a Date number. The date number matches what Excel would produce exactly. Since this is an accounting system I have to be consistent with dates currently being utilized.
>> Anyway, a simple way to get the 38841'th day after 1st of January 1900 >> is: [quoted text clipped - 11 lines] > > /L Terry Jolly - 04 May 2006 19:50 GMT Thanks Lasse,
Yes, I meant today May, 4, 2006.
In your example you're using 33841 (var theDate = new Date(1900,0,38841);) which is what I'm trying to get from the Date. Maybe I'm mis-understanding you.
I need to convert today, tomorrow, a pass date, etc., into a number like 38841 (May 4, 2006). Also the database I'm working with is storing the dates in an int field as a number for example May 4, 2006 is stored as 38841. The Database has lots of tables and all kind of dates (that are being store as a number in a int field). I need to be able now only to convert the number into a date, but also takea date and store it as an int number.
> (please don't top post) > [quoted text clipped - 24 lines] > > /L Steve Wampler - 04 May 2006 20:13 GMT > Thanks Lasse, > [quoted text clipped - 10 lines] > number in a int field). I need to be able now only to convert the number > into a date, but also takea date and store it as an int number. Can't you just use simple arithmetic now that you know the date 'count' starts on 01/01/1990? You can use the GregorianCalendar class to get the time in milliseconds for both the date you're interested in and for 01/01/1990, subtract and then divide by the number of ms in a day. You may need to add one if you think of 01/01/1990 as day 1 instead of day 0 and *maybe* add a leap-year adjustment if Excel really doesn't get that right.
 Signature Steve Wampler -- swampler@noao.edu The gods that smiled on your birth are now laughing out loud.
Lasse Reichstein Nielsen - 05 May 2006 00:54 GMT > In your example you're using 33841 (var theDate = new Date(1900,0,38841);) > which is what I'm trying to get from the Date. Maybe I'm mis-understanding > you. No, it was me who was misunderstanding the direction of the problem.
The other direction is a little harder since you have to convert a difference in milliseconds to one in days.
Date theDate = new GregorianCalendar(2006, Calendar.MAY, 4).getTime(); Date epoch = new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime(); int days = (int) Math.round((theDate.getTime() - epoch.getTime())/864E5) + 1; Again it gives the correct number of days, so in the range 1900..2099, you should add one to the number if 60 or above:
if (days >= 60) { days++; } // match Excel, which believes in 1900-02-29
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Terry Jolly - 05 May 2006 17:36 GMT Many Thanks!
>> In your example you're using 33841 (var theDate = new >> Date(1900,0,38841);) [quoted text clipped - 18 lines] > > /L Roedy Green - 04 May 2006 21:46 GMT >How do I do this in java? read up http://mindprod.com/jgloss/calendar.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Terry Jolly - 04 May 2006 22:04 GMT Thanks that answers my question -- I'll use C# and .net instead.
>>How do I do this in java? > > read up http://mindprod.com/jgloss/calendar.html Roedy Green - 04 May 2006 23:54 GMT >Thanks that answers my question -- I'll use C# and .net instead. Did you follow the link before coming to that conclusion? The BigDate alternative is pretty easy.
>>>How do I do this in java? >> >> read up http://mindprod.com/jgloss/calendar.html  Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli - 05 May 2006 00:23 GMT "Roedy Green" wrote...
>>Thanks that answers my question -- I'll use C# and .net instead. > > Did you follow the link before coming to that conclusion? > The BigDate alternative is pretty easy. I don't think that would suffice for the OP.
In the C# group he was practically served the complete code. Here we asked him to think out the algorithm for himself... ;-)
But for the OP's problem, BigDate would have made it possible to use exactly the same solution as he was given in the C# group, to instantiate a BigDate at the epoch of a VB Date, and then to simply use the method addDays(int).
// Bjorn A
Oliver Wong - 05 May 2006 16:35 GMT > "Roedy Green" wrote... >> [quoted text clipped - 7 lines] > In the C# group he was practically served the complete code. Here we asked > him to think out the algorithm for himself... ;-) Also, nothing works better with Microsoft products than more Microsoft products. That's one of the reasons they keep getting antitrust trials.
- Oliver
Terry Jolly - 05 May 2006 18:50 GMT I intend to take a look at BigDate and have no idea what you mean refering to me as OP. Yes, the C# group gave me the answer to which I am grateful.
First, I spent 5 years develop/maintaining an PMA system I wrote for the web (using VB6 + asp) and I have about 60 companies using it.
Second, I have a full time job which itself is 50+ hours a week (and it's not in IT). There's just not a lot of extra time left over. I spent the last month reading books on Java and C# trying to determine the best approach for moving my development.
Third, I would perfer to leave Microsoft behind - that's why I'm on this newsgroup to begin with, however, I am not going multiple my efforts 10 fold over simplest items (getting a darn date) just because I don't like Microsoft.
> "Roedy Green" wrote... >> [quoted text clipped - 17 lines] > Inviato da X-Privat.Org - Registrazione gratuita > http://www.x-privat.org/join.php Bjorn Abelli - 05 May 2006 19:05 GMT "Terry Jolly" wrote...
> I intend to take a look at BigDate and have no idea what you > mean refering to me as OP. Yes, the C# group gave me the > answer to which I am grateful. Hi Terry,
"OP" is a common acronym in Usenet for "Original Poster", i.e. the one starting a thread.
Glad to hear you have found a Java solution as well. :-)
// Bjorn A
Roedy Green - 05 May 2006 22:31 GMT >I intend to take a look at BigDate and have no idea what you mean refering >to me as OP. see http://mindprod.com/jgloss/netspeak.html
There are about a dozen acronyms you will see commonly used in this newsgroup we call netspeak. This is distinct from textspeak which we frown on.
see http://mindprod.com/jgloss/textspeak.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 05 May 2006 22:38 GMT >Third, I would perfer to leave Microsoft behind - that's why I'm on this >newsgroup to begin with, however, I am not going multiple my efforts 10 fold >over simplest items (getting a darn date) just because I don't like >Microsoft. Unfortunately Date itself is the lemon of Java (one of Sun's engineer's terms for it). I wrote BigDate in self defence. Then Sun came out with Calendar (GregorianCalender SimpleDateFormat) which I find ugly as Rush Limbaugh's behind, but at least they work.
I have written lots of sample code to show you how to do the common things with Calendar.
If I am working with timestamps -- instants in time, I use Calendar. If am talking about pure dates, e.g. Christmas falls on Dec 25, then I use BigDate. I also use Calendar for pure dates where there are no calculations just to avoid packing an extra class.
Check out the following:
http://mindprod.com/jgloss/date.html http://mindprod.com/jgloss/bigdate.html http://mindprod.com/jgloss/calendar.html http://mindprod.com/jgloss/gotchas.html#DATE http://mindprod.com/jgloss/time.html http://mindprod.com/jgloss/timezone.html
I give you an introduction to the classes which is helpful before you get buried in the detail of the Sun Javadoc.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli - 05 May 2006 23:23 GMT "Roedy Green" wrote...
> Unfortunately Date itself is the lemon of Java (one of Sun's > engineer's terms for it). I wrote BigDate in self defence. Then Sun > came out with Calendar (GregorianCalender SimpleDateFormat) which I > find ugly as Rush Limbaugh's behind, but at least they work. How do you know Rush Limbaugh's behind works? ;-)
> I have written lots of sample code to show you how to > do the common things with Calendar. > > If I am working with timestamps -- instants in time, I use > Calendar. If am talking about pure dates, e.g. Christmas > falls on Dec 25, then I use BigDate. I haven't looked into detail in your BigDate class, but does that mean that it doesn't rely on the java.util.Date or Calendar in any way?
A BIG difference between the platforms/languages is that a java.util.Date internally uses UTC, while Dates in VB and .NET does not! What about BigDate? If it *doesn't* use UTC it would be even more suitable for the OP.
I made some Date-conversion utilities myself between different platforms and languages, and was surprised to find that the .NET DateTime doesn't use an internal UTC, in our days and ages, where communication across the borders are increasingly common...
// Bjorn A
Roedy Green - 06 May 2006 03:27 GMT On 6 May 2006 00:23:48 +0200, "Bjorn Abelli" <bjorn_abelli@DoNotSpam.hotmail.com> wrote, quoted or indirectly quoted someone who said :
>I haven't looked into detail in your BigDate class, but does that mean that >it doesn't rely on the java.util.Date or Calendar in any way? Correct. BigDate uses a 32-bit days since 1970 Jan 1 internally rather than milliseconds. It simplifies calculations that don't involve time or timezones.
You can convert Dates to BigDates and vice versa.
It was primarily written to fix the bugs and shortcomings of Date. Date is now almost entirely deprecated.
Calendar has a clumsy UI where you feed it magic numbers to describe what you want it to do rather than calling methods for each function. It is an exercise in how to destroy type safety.
Here are some examples of BigDate use and the sort of problem it tackles easily:
sep(); { // what is today's date in American format System.out .println( "Today American style is " + BigDate.localToday() .toDowMMDDYY() ); } sep(); { // what is today's date in iso format System.out .println( "Today ISO style is " + BigDate.localToday() .toString() ); } sep(); { // What date is it in Greenwich? BigDate d = BigDate.UTCToday(); System.out .println( "Today in Greenwich England = " + d.toString() + "." ); /* 0=Sunday to 6=Saturday */ int dayOfWeek = d.getDayOfWeek(); String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String dayName = daysOfTheWeek[ dayOfWeek ]; System.out.println( "In Greenwich it is " + dayName + "." ); } sep(); { // What day of the week is today? // display ISO 8601:1988 international standard format: yyyy-mm-dd. BigDate d = BigDate.localToday(); System.out.println( "Today = " + d.toString() + "." ); /* 0=Sunday to 6=Saturday */ int dayOfWeek = d.getDayOfWeek(); String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String dayName = daysOfTheWeek[ dayOfWeek ]; System.out.println( "Today is " + dayName + "." ); System.out .println( "Today is dayOfWeek number " + d.getDayOfWeek() + " if Sunday=0." ); System.out .println( "Today is dayOfWeek number " + d.getCalendarDayOfWeek() + " if Sunday=1." ); } pause(); { // what season is it? String[] seasons = new String[] {"spring", "summer", "fall", "winter"}; System.out .println( "It is " + seasons[ BigDate.localToday() .getSeason() ] ); System.out.println( "Season for months are: " ); for ( int i = 1; i <= 12; i++ ) { BigDate b = new BigDate( 2004, i, 1 ); System.out.println( i + " " + seasons[ b.getSeason() ] ); } } pause(); { // What Date is Cobol yyddd date 99360? BigDate d = new BigDate( 99 + 1900, 1, 360, BigDate.NORMALISE ); System.out .println( "COBOL-style yyddd date 99360 = " + d.toString() + "." ); } sep(); { // How do you convert Java timestamps into Windows timestamps. // Java timestamps use 64-bit milliseconds since 1970 GMT. // Windows timestamps use 64-bit value representing the number // of 100-nanosecond intervals since January 1, 1601. int windowsBase = BigDate.toOrdinal( 1601, 1, 1 ); int javaBase = BigDate.toOrdinal( 1970, 1, 1 ); int daysDifference = javaBase - windowsBase; // but Microsoft forgot that // 86,400,000 = 1000 * 60 * 60 * 24 = milliseconds per day long millisDifference = daysDifference * 86400000L; System.out .println( "To convert Java Timestamps to Windows Timestamps:" ); System.out .println( "windows = ( java + " + millisDifference + "L ) * 10000" ); } sep(); { // Display a BigDate with SimpleDateFormat using local time BigDate bigDate = new BigDate( 1999, 12, 31 ); Date date = bigDate.getLocalDate(); SimpleDateFormat sdf = new SimpleDateFormat( "EEEE yyyy/MM/dd G hh:mm:ss aa zz : zzzzzz" ); sdf.setTimeZone( TimeZone.getDefault() ); // local time String dateString = sdf.format( date ); System.out.println( "Local SimpleDateFormat: " + dateString ); } sep(); { // Display a BigDate with SimpleDateFormat using UTC (Greenwich GMT) // time BigDate bigDate = new BigDate( 1999, 12, 31 ); Date date = bigDate.getUTCDate(); SimpleDateFormat sdf = new SimpleDateFormat( "EEEE yyyy/MM/dd G hh:mm:ss aa zz : zzzzzz" ); sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // GMT time String dateString = sdf.format( date ); System.out.println( "UTC/GMT SimpleDateFormat: " + dateString ); } sep(); { // Display a BigDate with default-locale DateFormat using UTC // (Greenwich GMT) time BigDate bigDate = new BigDate( 1999, 12, 31 ); Date date = bigDate.getUTCDate(); DateFormat df = DateFormat.getDateInstance(); df.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // GMT time String dateString = df.format( date ); System.out.println( "UTC/GMT locale DateFormat: " + dateString ); } sep(); { // What are the earliest and latest dates BigDate can handle BigDate d = new BigDate( BigDate.MIN_ORDINAL ); System.out.println( "Earliest BigDate possible is " + d.toString() ); d.setOrdinal( BigDate.MAX_ORDINAL ); System.out.println( "Latest BigDate possible is " + d.toString() ); } sep(); { // What is the last day of February in 2000, preferred method BigDate d = new BigDate( 2000, 3, 1 ); /* 2000, March 1 */ d.addDays( -1 ); /* last day of Feb */ System.out.println( "Last day in Feb 2000 = " + d.toString() + "." ); } sep(); { // What is the last day of February in 2004, alternate less safe, // no-check method BigDate d = new BigDate( 2004, 2 + 1, 1 - 1, BigDate.NORMALIZE ); /* * 2004, * March * 0 */ System.out.println( "Last day in Feb 2004 = " + d.toString() + "." ); } sep(); { // What is the last day of February in 2006, generic method. BigDate d = new BigDate( 2006, 2, 1 ); /* 2004, Feb 1 */ d.addDays( BigDate.daysInMonth( 2, 2006 ) - 1 ); System.out.println( "Last day in Feb 2006 = " + d.toString() + "." ); } sep(); { // What Date was it yesterday? BigDate d = BigDate.localToday(); d.addDays( -1 ); System.out.println( "Yesterday = " + d.toString() + "." ); } sep(); { // What Date will it be tomorrow? BigDate d = BigDate.localToday(); d.addDays( 1 ); System.out.println( "Tomorrow = " + d.toString() + "." ); } sep(); { // What Date will it be 1000 days from now? BigDate d = BigDate.localToday(); d.addDays( 1000 ); System.out.println( "Today+1000 days = " + d.toString() + "." ); } sep(); { // What date will it be 50 months after 1998-01-03 // You can think of this as how to turn a possibly invalid date into // the equivalent valid one. BigDate d = new BigDate( 1998, 1 + 50, 3, BigDate.NORMALISE ); System.out.println( "1998-01-03+50 months = " + d.toString() + "." ); } sep(); { // What date will it be 60 months from today BigDate today = BigDate.localToday(); BigDate d = new BigDate( today.getYYYY(), today.getMM() + 60, today .getDD(), BigDate.NORMALISE ); System.out .println( "60 months from today will be " + d.toString() + "." ); }
sep(); { // Was 1830-02-29 a valid date? boolean ok = BigDate.isValid( 1830, 2, 29 ); System.out .println( "True or false: 1830/02/29 was a valid date: " + ok + "." ); } sep(); { // how many seconds between Jan 1, 1900 0:00 and Jan 1, 1970 0:00 // i.e. difference between SNTP and Java timestamp bases. // No leap seconds to worry about. long diffInDays = BigDate.toOrdinal( 1970, 1, 1 ) - BigDate.toOrdinal( 1900, 1, 1 ); long diffInSecs = diffInDays * ( 24 * 60 * 60L ); System.out .println( diffInDays + " days or " + diffInSecs + " seconds between 1900 Jan 1 and 1970 Jan 1" ); } sep(); { // How Many days since 1970 is today ? BigDate d = BigDate.localToday(); System.out.println( "Today is " + d.getOrdinal() + " days since 1970" ); } sep(); { // How Many days since 1970 is 2000/2/29?
BigDate d = new BigDate( 2000, 2, 29 );
System.out .println( "1999-12-29 is " + d.getOrdinal() + " days since 1970" ); } sep(); { // How Many milliseconds since 1970 is 1999/11/7 BigDate d = new BigDate( 1999, 11, 7 ); System.out .println( "1999-11-7 is " + d.getLocalTimeStamp() + " milliseconds since 1970" ); } sep(); { // Is the year 2000 a leap year? System.out .println( "True or false: 2000 is a leap year: " + BigDate.isLeap( 2000 ) + "." ); } sep();
{ // What is the last day of this week, e.g. this Saturday? // display yyyy/mm/dd format. BigDate d = BigDate.localToday(); /* 0=Sunday to 6=Saturday */ d.addDays( 6 - d.getDayOfWeek() ); System.out.println( "This Saturday is " + d.toString() + "." ); } sep(); { // In what week number did 1999/08/20 fall?
BigDate d = new BigDate( 1999, 8, 20 ); System.out .println( "1999/08/20 fell in ISO week number " + d.getISOWeekNumber() + " on ISO day of week number " + d.getISODayOfWeek() + "." );
System.out .println( " According to BigDate.getWeekNumber that is week number " + d.getWeekNumber() );
GregorianCalendar g = new GregorianCalendar( 1999, 8 - 1, 20 ); System.out .println( " According to java.util.Calendar that is week number " + g.get( Calendar.WEEK_OF_YEAR ) + "." ); } sep(); { // what is the next business day? BigDate d = BigDate.localToday(); int interval; switch ( d.getDayOfWeek() ) { case 5 /* Friday -> Monday */: interval = 3; break;
case 6 /* Saturday -> Monday */: interval = 2; break;
case 0 /* Sunday -> Monday */: case 1 /* Monday -> Tuesday */: case 2 /* Tuesday -> Wednesday */: case 3 /* Wednesday -> Thursday */: case 4 /* Thursday -> Friday */: default : interval = 1; break; } // end switch d.addDays( interval ); System.out.println( "Next business day = " + d.toString() + "." ); } sep(); { // What Day of the Month Is The Second Thursday Of This Month? // This is when the Vancouver PC User Society (VPCUS) meets. // MacMillan planetarium. BigDate today = BigDate.localToday(); int dayOfMonthOfSecondThursday = BigDate.nthXXXDay( 2 /* second */, 4 /* thursday */, today.getYYYY(), today .getMM() ); System.out .println( "VPCUS meets the second Thursday of the month. This month's meeting is on the " + dayOfMonthOfSecondThursday + "th." ); } sep(); { // What Day of the Month Is The third Thursday Of This Month? // This is when the Vancouver Apple User Society meets. // Scottish Cultural Center, 8886 Hudson, near the Oak St Bridge BigDate today = BigDate.localToday(); int dayOfMonthOfThirdThursday = BigDate.nthXXXDay( 3 /* third */, 4 /* thursday */, today.getYYYY(), today.getMM() );
System.out .println( "Apples BC meets the third Thursday of the month. This month's meeting is on the " + dayOfMonthOfThirdThursday + "th." ); } sep(); { // When is the next issue of Xtra West magazine due out // It came out 2000 July 27, and comes out every two weeks. BigDate today = BigDate.localToday(); BigDate first = new BigDate( 2000, 7, 27 ); int days = today.getOrdinal() - first.getOrdinal(); int fortnights = ( days + 13 ) / 14; // round up BigDate next = new BigDate( first.getOrdinal() + fortnights * 14 ); System.out .println( "Next issue of Xtra West magazine is due out " + next.toString() + "." ); } sep(); { // When is J. McRee (Mac) Elrod's next potluck? // http://www.islandnet.com/~jelrod/gpl.html // the fourth Saturday of the month, jan, apr, jul, oct, every 3 // months. BigDate today = BigDate.localToday(); int year = today.getYYYY(); // 1>1 2>4 3>4 4>4 5>7 6>7 7>7 8>10 9>10 10>10 11>1 12>1 int month = ( ( ( today.getMM() + ( 3 - 1 - 1 ) ) / 3 ) * 3 ) % 12 + 1; BigDate when = new BigDate( BigDate.ordinalOfnthXXXDay( 4 /* fourth */, 6 /* saturday */, year, month ) ); if ( when.getOrdinal() < today.getOrdinal() ) { // we missed this month's potlock, get next one. month += 3; if ( month > 12 ) { year++; month -= 12; when = new BigDate( BigDate.ordinalOfnthXXXDay( 4 /* fourth */, 6 /* saturday */, year, month ) ); } } System.out .println( "Mac's next potluck in on the fourth Saturday of the month, every 3 months,\nNext is on " + when ); } sep(); { // What Day of the Month Is The Last Friday Of This Month? BigDate today = BigDate.localToday(); int dayOfMonthOfLastFriday = BigDate.nthXXXDay( 5 /* last */, 5 /* friday */, today.getYYYY(), today.getMM() );
System.out .println( "The last Friday of the month is on the " + dayOfMonthOfLastFriday + "th." ); } sep();
{ // Precisely how old is Roedy Green, in years, months and days // Roedy was born on February 4, 1948. BigDate birthDate = new BigDate( 1948, 2, 4 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( birthDate, today ); System.out .println( "Today Roedy is " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old." ); System.out .println( "or " + ( today.getOrdinal() - birthDate.getOrdinal() ) + " days." ); } sep();
{ // How old would John Lennon be, in years, months and days // Lennon was born on October 9, 1940. BigDate birthDate = new BigDate( 1940, 10, 9 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( birthDate, today ); System.out .println( "Today John Lennon would be " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old." ); } sep(); { // How long since John Lennon died, in years, months and days // Lennon was born on December 8, 1980. BigDate deathDate = new BigDate( 1980, 12, 8 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( deathDate, today ); System.out .println( "It has been " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days since John Lennon was murdered." ); } sep(); { // Precisely how old is are the Bush twins in years, months and days // There were born on 1981-11-25. BigDate birthDate = new BigDate( 1981, 11, 25 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( birthDate, today ); System.out .println( "Today fraternal twins Barbara and Jenna Bush are " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old." ); System.out .println( "or " + ( today.getOrdinal() - birthDate.getOrdinal() ) + " days." ); } sep();
{ // Dr. Paul Norman was killed in plane crash // on the Sunday before Friday 2004-07-02. What date was that? BigDate deathDate = new BigDate( 2004, 7, 2 ); deathDate .setOrdinal( deathDate.getOrdinal() - 5 /* Friday */ + 0 /* Sunday */ ); System.out .println( "Dr. Paul Norman was killed in a plane crash on " + deathDate + "." ); } sep();
{ // How much time elapsed after Hitler died before George W. Bush was // born.
BigDate hitler = new BigDate( 1945, 5, 1 ); BigDate bush = new BigDate( 1946, 7, 6 ); int[] age = BigDate.age( hitler, bush ); System.out .println( "George W. Bush was born " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days after Hitler died on 1945-05-01" ); System.out .println( "or " + ( bush.getOrdinal() - hitler.getOrdinal() ) + " days." ); } sep();
{ // Precisely how old was Hillary Clinton (born 1947-10-26) // when Sir Edmund Hillary climbed // mount everent in 1953-05-29 in years, months and days BigDate birthDate = new BigDate( 1947, 10, 26 ); BigDate climb = new BigDate( 1953, 5, 29 ); int[] age = BigDate.age( birthDate, climb ); System.out .println( "Hillary Clinton was " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old when Sir Edmund Hillary climbed Mount Everest on 1953-05-29" ); System.out .println( "or " + ( climb.getOrdinal() - birthDate.getOrdinal() ) + " days." ); } sep();
{ // What day of the week was Jesus born on? // assume Jesus was born on December 25, 0001. // Scholars assure us he was NOT actually born then. int dayOfWeek = BigDate.dayOfWeek( BigDate.toOrdinal( 1, 12, 25 ) ); String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String dayName = daysOfTheWeek[ dayOfWeek ]; System.out.println( "Jesus was born on a " + dayName + "." ); } sep(); { // How much time elapsed between December 25 4 BC and April 30, 28 // AD BigDate fromDate = new BigDate( -4, 12, 25 ); BigDate toDate = new BigDate( 28, 4, 30 ); System.out.println( "How long did Jesus live?" ); System.out .println( toDate.getOrdinal() - fromDate.getOrdinal() + " days elapsed between 0004/12/25 BC and 0028/04/30 AD," ); int[] age = BigDate.age( fromDate, toDate ); System.out .println( "or put another way, " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days." ); } sep();
{ // Precisely how old is George Bush Jr., in years, months and days // George Bush Jr. was born on July 6. 1946. BigDate birthDate = new BigDate( 1946, 7, 6 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( birthDate, today ); System.out .println( "Today George Bush Jr. is " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old." ); } sep(); { // Precisely how old is Saddam Hussein, in years, months and days // Saddam Hussein was born on April 28, 1937. BigDate birthDate = new BigDate( 1937, 4, 28 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( birthDate, today ); System.out .println( "Today Saddam Hussein. is " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old." ); } sep();
{ // How Many days after bin Laden's last documented meeting with the // CIA // was the World Trade attack? BigDate fromDate = new BigDate( 2001, 7, 1 ); // 2001-07-01 BigDate toDate = new BigDate( 2001, 9, 11 ); // 2001-09-11 System.out .println( "The World Trade attack occurred " + ( toDate.getOrdinal() - fromDate.getOrdinal() ) + " days after Osmama bin Laden's last meeting with the CIA." ); } sep(); { // How long ago was the World Trade Center destroyed?
// The attack was 2001-09-11. BigDate hitDate = new BigDate( 2001, 9, 11 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( hitDate, today ); System.out .println( "The World Trade center was destroyed " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days ago." ); } sep(); { // How long did it take Bush to give up trying to catch bin Laden. // The attack was 2001-09-11. // On 2002-03-13 Bush announced: // I don't know where he is. I have no idea and I really don't care. // It's not that important. It's not our priority. BigDate hitDate = new BigDate( 2001, 9, 11 ); BigDate giveUpDate = new BigDate( 2002, 3, 13 ); int[] patience = BigDate.age( hitDate, giveUpDate ); System.out .println( "Bush officially gave up chasing bin Laden after " + patience[ 0 ] + " years, " + patience[ 1 ] + " months and " + patience[ 2 ] + " days." ); } sep(); { // What was the date and day of week 72 days prior to Lincoln's // assassination? BigDate assassinDate = new BigDate( 1865, 4, 18 ); // 1865-04-18 BigDate priorDate = new BigDate( assassinDate.getOrdinal() - 72 ); int dayOfWeek = priorDate.getDayOfWeek(); final String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; System.out .println( "72 days prior to Lincoln's assassination was " + priorDate.toString() + ", a " + daysOfTheWeek[ dayOfWeek ] ); } sep(); { // how long has it been since since John F. Kennedy was // assassinated? // John F. Kennedy was assassinated on November 22, 1963 System.out .println( "John F. Kennedy was assassinated " + ( BigDate.localToday().getOrdinal() - BigDate.toOrdinal( 1963, 11, 22 ) ) + " days ago." ); } sep(); { // How long after Martin Luther King's assassination // was Bobby Kennedy assassinated? // MLK was assassinated April 4, 1968. // RK was assassinated June 4, 1968. System.out .println( "Robert Kennedy was assassinated " + ( BigDate.toOrdinal( 1968, 6, 4 ) - BigDate.toOrdinal( 1968, 4, 4 ) ) + " days after Martin Luther King." ); } sep(); { // How old would Martin Luther King be if he were still alive today? // MLK was born 1929-01-15, celebrated 3rd monday in January BigDate mlk = new BigDate( 1929, 1, 15 ); BigDate today = BigDate.localToday(); int[] age = BigDate.age( mlk, today ); System.out .println( "Martin Luther King would be " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days old today." ); }
sep(); { // Mysterious missing days in the calendar. // Pope Gregory: 1582 Oct 4 Thursday, was followed immediately // by 1582 Oct 15 Friday dropping 10 days. // // British: 1752 Sep 2 Wednesday was followed immediately // by 1752 Sep 14 Thursday dropping 12 days. BigDate priorDate; BigDate afterDate; if ( BigDate.isBritish ) { System.out .println( "According to the British/American/Canadian calendar, 12 days were dropped." ); priorDate = new BigDate( 1752, 9, 2 ); afterDate = new BigDate( 1752, 9, 14 ); } else { System.out .println( "According to Pope Gregory's calendar, 10 days were dropped." ); priorDate = new BigDate( 1582, 10, 4 ); afterDate = new BigDate( 1582, 10, 15 ); } final String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int dayOfWeek = priorDate.getDayOfWeek(); System.out .println( priorDate.toString() + " was a " + daysOfTheWeek[ dayOfWeek ] + "." ); dayOfWeek = afterDate.getDayOfWeek(); System.out .println( afterDate.toString() + " was a " + daysOfTheWeek[ dayOfWeek ] + "." ); } pause(); { // What is the Julian Day number of various dates 1970/1/1 // Cross check these with the US Naval Observatory at: // http://aa.usno.navy.mil/AA/data/docs/JulianDate.html BigDate d = new BigDate( 2000, 3, 20 ); System.out .println( "Astronomical Julian Propleptic calendar day for 2000/03/20 is " + d.getProplepticJulianDay() ); d = new BigDate( 1970, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for 1970/01/01 is " + d.getProplepticJulianDay() ); d = new BigDate( 1600, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for 1600/01/01 is " + d.getProplepticJulianDay() ); d = new BigDate( 1500, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for 1500/01/01 is " + d.getProplepticJulianDay() ); d = new BigDate( 1, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for 0001/01/01 is " + d.getProplepticJulianDay() ); d = new BigDate( -1, 12, 31 ); System.out .println( "Astronomical Julian Propleptic calendar day for -0001/12/31 is " + d.getProplepticJulianDay() ); d = new BigDate( -6, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for -0006/01/01 is " + d.getProplepticJulianDay() ); d = new BigDate( -4713, 1, 1 ); System.out .println( "Astronomical Julian Propleptic calendar day for -4713/01/01 is " + d.getProplepticJulianDay() ); } pause(); { // How long till Christmas? ( Sun's way.) // this code will give a negative number just after Christmas. System.out .println( "Your child asks, how long is it until Christmas?" ); System.out .println( "Here are the possible answers you might give to an American child" );
GregorianCalendar now = new GregorianCalendar(); int thisYear = now.get( Calendar.YEAR );
// You may open presents at 7 AM December 25, local time System.out .println( "You may open your presents at 7 AM Christmas morning." );
GregorianCalendar christmas = new GregorianCalendar( thisYear, 12 - 1, 25, 7, 0, 0 );
// millis since 1970 Jan 1 long christmasTimeStamp = christmas.getTime().getTime();
long nowTimeStamp = now.getTime().getTime();
final int MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
double dayUnitsDiff = ( christmasTimeStamp - nowTimeStamp ) / (double) MILLISECONDS_PER_DAY;
System.out .println( "1. It is " + dayUnitsDiff + " day units of 24 hours until you may open your presents." );
System.out .println( "2. It is " + Math.ceil( dayUnitsDiff ) + " day units of 24 hours rounded up until you may open your presents." );
System.out .println( "3. It is " + Math.round( dayUnitsDiff ) + " day units of 24 hours rounded until you may open your presents." );
System.out .println( "4. It is " + Math.floor( dayUnitsDiff ) + " day units of 24 hours rounded down until you may open your presents." );
int gmtChristmasOrdinal = (int) ( christmasTimeStamp / MILLISECONDS_PER_DAY ); int gmtNowOrdinal = (int) ( nowTimeStamp / MILLISECONDS_PER_DAY ); int gmtDiffInDays = gmtChristmasOrdinal - gmtNowOrdinal; System.out .println( "5. Children in Greenwich have " + gmtDiffInDays + " sleeps (midnight crossings) to go until\n" + "you may open your presents.\n" + "They may open theirs even sooner." );
// For children living in the USA, the timezone offset will be // negative. // days since 1970 Jan 1 // There is no GregorianCalender.getOffset or // get(ADJUSTED_ZONE_OFFSET); int christmasZoneOffset = christmas.get( Calendar.ZONE_OFFSET ) + now.get( Calendar.DST_OFFSET );
int localChristmasOrdinal = (int) ( ( christmasTimeStamp + christmasZoneOffset ) / MILLISECONDS_PER_DAY ); int nowZoneOffset = now.get( Calendar.ZONE_OFFSET ) + now.get( Calendar.DST_OFFSET ); int localNowOrdinal = (int) ( ( nowTimeStamp + nowZoneOffset ) / MILLISECONDS_PER_DAY ); int localDiffInDays = localChristmasOrdinal - localNowOrdinal; System.out .println( "6. You have " + localDiffInDays + " sleeps (midnight crossings) to go." ); } { // how long till Christmas? ( with BigDate. ) // this code will give a negative number just after Christmas.
BigDate today = BigDate.localToday(); BigDate christmas = new BigDate( today.getYYYY(), 12, 25 ); System.out .println( "7. Uncle Roedy's Bigdate says it is " + ( christmas.getOrdinal() - today.getOrdinal() ) + " sleeps until Christmas." );
int[] age = BigDate.age( today, christmas ); System.out .println( "8. Or put another way " + age[ 0 ] + " years and " + age[ 1 ] + " months and " + age[ 2 ] + " days to go." ); }
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Terry Jolly - 05 May 2006 18:20 GMT You're right the BigDate does seem easy and resovle my problem.
Many Thanks.
>>Thanks that answers my question -- I'll use C# and .net instead. > [quoted text clipped - 4 lines] >>> >>> read up http://mindprod.com/jgloss/calendar.html mayur - 06 May 2006 04:38 GMT hi, In java date is diffrent data type for that u have to import java.sql Or java.util date can be converted in String formate and string canbe Int DATE>>>>STRING>>>>INT
using Substring(); & then ParseInt(); functions.
Try it.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|