View Javadoc
1 /*** 2 * Copyright (c) 2002, Raben Systems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * Neither the name of the Raben Systems, Inc. nor the names of its contributors 14 * may be used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 * THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package com.raben.util; 29 30 import java.util.*; 31 import java.text.*; 32 33 /*** 34 * Routines for calculating and setting Julian day number 35 * based on algorithms from Jean Meeus, 36 * "Astronomical Algorithms", 2nd Edition, Willmann-Bell, Inc., 37 * 1998. 38 * 39 * @author Vern Raben (mailto:vern@raben.com) 40 * @version $Revision: 1.19 $ $Date: 2002/12/12 20:46:29 $ 41 */ 42 public final class JulianDay implements java.io.Serializable, Cloneable { 43 public final static int JD=100; 44 public final static int MJD=101; 45 public final static int YEAR=Calendar.YEAR; 46 public final static int MONTH=Calendar.MONTH; 47 public final static int DATE=Calendar.DATE; 48 public final static int HOUR=Calendar.HOUR; 49 public final static int HOUR_OF_DAY=Calendar.HOUR_OF_DAY; 50 public final static int MINUTE=Calendar.MINUTE; 51 public final static int SECOND=Calendar.SECOND; 52 public final static int DAY_OF_YEAR=Calendar.DAY_OF_YEAR; 53 public final static int DAY_OF_WEEK=Calendar.DAY_OF_WEEK; 54 public final static int DAY_OF_MONTH=Calendar.DAY_OF_MONTH; 55 public final static int JANUARY=Calendar.JANUARY; 56 public final static int FEBRUARY=Calendar.FEBRUARY; 57 public final static int MARCH=Calendar.MARCH; 58 public final static int APRIL=Calendar.APRIL; 59 public final static int MAY=Calendar.MAY; 60 public final static int JUNE=Calendar.JUNE; 61 public final static int JULY=Calendar.JULY; 62 public final static int AUGUST=Calendar.AUGUST; 63 public final static int SEPTEMBER=Calendar.SEPTEMBER; 64 public final static int OCTOBER=Calendar.OCTOBER; 65 public final static int NOVEMBER=Calendar.NOVEMBER; 66 public final static int DECEMBER=Calendar.DECEMBER; 67 public final static String[] MONTHS={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}; 68 public final static String[] TIME_UNIT={"unk","yr","mo","unk","unk","day","unk","unk","unk","unk","unk","hr","min","sec"}; 69 public final static double EPOCH_1970=2440587.5; 70 public final static double EPOCH_0=1721057.5; 71 public final static String SQL_DATE_FORMAT="yyyy-MM-dd HH:mm:ss"; 72 private DateFormat dateFormat=new SimpleDateFormat(SQL_DATE_FORMAT); 73 private Integer year=new Integer(0); 74 private Integer month=new Integer(0); 75 private Integer date=new Integer(0); 76 private Integer hour=new Integer(0); 77 private Integer minute=new Integer(0); 78 private Integer second=new Integer(0); 79 private Double jd; 80 private Double mjd; 81 private Integer dayOfWeek; 82 private Integer dayOfYear; 83 private final static DecimalFormat fmt4Dig = new DecimalFormat("0000"); 84 private final static DecimalFormat fmt2Dig = new DecimalFormat("00"); 85 private final static TimeZone tz=TimeZone.getTimeZone("UTC"); 86 87 88 /*** 89 * JulianCalendar constructor - sets JD for current time 90 */ 91 public JulianDay() { 92 Calendar cal=new GregorianCalendar(tz); 93 setTime(cal.getTime()); 94 } 95 96 /*** 97 * JulianCalendar constructor - sets JD passed as double 98 * @param jd double The Julian date 99 */ 100 public JulianDay(double jd) { 101 set(JulianDay.JD,jd); 102 calcCalDate(); 103 } 104 105 /*** 106 * Constructor to create Julian day given year, month, and decimal day 107 * @param yr int 108 * @param mo int 109 * @param da double 110 */ 111 public JulianDay(int yr, int mo, double da) { 112 int day=(int)da; 113 int hr=0; 114 int min=0; 115 int sec=0; 116 double dhr=(da-day)*24.0; 117 hr=(int)dhr; 118 double dmin=(dhr-hr)*60.0; 119 min=(int)(dmin); 120 sec=(int)((dmin-min)*60.0); 121 set(yr,mo,day,hr,min,sec); 122 calcJD(); 123 } 124 125 /*** 126 * Construct JulianDate given year, month, and date 127 * @param yr int 128 * @param mo int 129 * @param da int 130 */ 131 public JulianDay(int yr, int mo, int da) { 132 int hr=0; 133 int min=0; 134 int sec=0; 135 136 if (da < 1) { 137 da = 1; 138 } 139 140 if (mo < 0) { 141 mo = 0; 142 } 143 144 if (hr < 0) { 145 hr = 0; 146 } 147 148 if (min < 0) { 149 min = 0; 150 } 151 152 if (sec < 0) { 153 sec = 0; 154 } 155 156 set(yr, mo, da, hr, min, sec); 157 calcJD(); 158 } 159 160 /*** 161 * Construct JulianDate given year, month, date, hour and minute 162 * @param yr int 163 * @param mo int 164 * @param da int 165 */ 166 public JulianDay(int yr, int mo, int da,int hr,int min) { 167 168 int sec=0; 169 170 if (da < 1) { 171 da = 1; 172 } 173 174 if (mo < 0) { 175 mo = 0; 176 } 177 178 if (hr < 0) { 179 hr = 0; 180 } 181 182 if (min < 0) { 183 min = 0; 184 } 185 186 if (sec < 0) { 187 sec = 0; 188 } 189 190 set(yr, mo, da, hr, min, sec); 191 calcJD(); 192 } 193 194 /*** 195 * Construct JulianDate given year, month, day, hour, minute, and second 196 * @param yr int 197 * @param mo int 198 * @param da int 199 * @param hr int 200 * @param min int 201 * @param sec int 202 */ 203 public JulianDay(int yr, int mo, int da, int hr, int min, int sec) { 204 205 if (da < 1) { 206 da = 1; 207 } 208 209 if (mo < 0) { 210 mo = 0; 211 } 212 213 if (hr < 0) { 214 hr = 0; 215 } 216 217 if (min < 0) { 218 min = 0; 219 } 220 221 if (sec < 0) { 222 sec = 0; 223 } 224 225 set(yr, mo, da, hr, min, sec); 226 calcJD(); 227 } 228 229 /*** 230 * Construct JulianDay from system time in milli-seconds since Jan 1, 1970 231 * @param systemTimeInMilliSeconds long 232 */ 233 public JulianDay(long timeInMilliSec) { 234 setDateTime("1970-01-01 0:00"); 235 add(JulianDay.DATE,((double)timeInMilliSec/86400000.0)); 236 } 237 238 /*** 239 * Copy constructor for JulianDate 240 * @param cal com.raben.util.JulianDate 241 */ 242 public JulianDay(JulianDay cal) { 243 if (cal!=null) { 244 set(Calendar.YEAR,cal.get(Calendar.YEAR)); 245 set(Calendar.MONTH,cal.get(Calendar.MONTH)); 246 set(Calendar.DATE,cal.get(Calendar.DATE)); 247 set(Calendar.HOUR_OF_DAY,cal.get(Calendar.HOUR_OF_DAY)); 248 set(Calendar.MINUTE,cal.get(Calendar.MINUTE)); 249 set(Calendar.SECOND,cal.get(Calendar.SECOND)); 250 calcJD(); 251 } 252 else { 253 Calendar calendar=new GregorianCalendar(tz); 254 setTime(calendar.getTime()); 255 } 256 } 257 258 /*** 259 * Set JulianDay from sql database compatible date/time string (yyyy-mm-dd hh:mm:ss) 260 * @param dateStr java.lang.String 261 */ 262 public JulianDay(String str) { 263 setDateTime(str); 264 calcJD(); 265 } 266 267 /*** 268 * Construct JulianDate given Calendar as a parameter 269 * @param cal java.util.Calendar 270 */ 271 public JulianDay(Calendar cal) { 272 set(YEAR,cal.get(YEAR)); 273 set(MONTH,cal.get(MONTH)); 274 set(DATE,cal.get(DATE)); 275 set(HOUR_OF_DAY,cal.get(HOUR_OF_DAY)); 276 set(MINUTE,cal.get(MINUTE)); 277 set(SECOND,cal.get(SECOND)); 278 calcJD(); 279 calcCalDate(); 280 } 281 282 /*** 283 * Add specified value in specified time unit to current Julian Date 284 * increments next higher field 285 * ISSUE - meaning of incrementing YEAR and MONTH by fractional value is not clear since 286 * period of a month and year varies, that is ignored. Year is assumed to be 365 days and 287 * month is assumed to be 30 days for computing the fractional increment. 288 * ISSUE - not thoroughly tested, typically 1-2 second errors may occur 289 * due to round-off. Will be refactored 290 * "real soon now" :) to utilize BigDecimal internal representation 291 * of Julian Day. 292 * @param unit int Time unit 293 * @param val int Time increment 294 */ 295 public void add(int unit, double val) { 296 double da; 297 298 switch(unit) { 299 case YEAR: 300 // issue - what this means if its not whole year 301 int yr=year.intValue()+(int)val; 302 set(YEAR,yr); 303 da=(val-(int)val)*365.0; 304 set(DATE,da); 305 break; 306 case MONTH: 307 int mo=month.intValue()+(int)val; 308 set(MONTH,mo); 309 da=(val-(int)val)*30.0; 310 set(DATE,da); 311 break; 312 313 case DATE: 314 set(JD,getJDN()+val); 315 break; 316 case HOUR: 317 case HOUR_OF_DAY: 318 set(JD,getJDN()+(double)val/24.0); 319 break; 320 case MINUTE: 321 double min=minute.doubleValue()+val; 322 set(JD,getJDN()+(double)val/1440.0); 323 break; 324 case SECOND: 325 double sec=second.doubleValue()+val; 326 set(JD,getJDN()+(double)val/86400.0); 327 break; 328 default: 329 System.out.println("Error: JulianDate.add: The 'unit' parameter is not recognized="+unit); 330 set(JD,getJDN()+val); 331 break; 332 } 333 334 calcJD(); 335 336 } 337 338 /*** 339 * Add specified value in specified time unit to current Julian Date 340 * increments next higher field 341 * 342 * ISSUE - meaning of incrementing YEAR and MONTH by fractional value is not clear since 343 * period of a month and year varies, that is ignored. Year is assumed to be 365 days and 344 * month is assumed to be 30 days for computing the fractional increment. 345 * ISSUE - not thoroughly tested, typically 1-2 second errors may occur 346 * due to round-off. Will be refactored 347 * "real soon now" :) to utilize BigDecimal internal representation 348 * of Julian Day. 349 * @param unit int Time unit 350 * @param val int Time increment 351 */ 352 public void add(int unit, int val) { 353 int yr; 354 int mo; 355 switch(unit) { 356 case YEAR: 357 yr=year.intValue()+val; 358 set(YEAR,yr); 359 break; 360 case MONTH: 361 mo=month.intValue()+val; 362 363 while (mo>=12) { 364 mo-=12; 365 yr=year.intValue()+1; 366 set(YEAR,yr); 367 } 368 369 while (mo<0) { 370 mo+=12; 371 yr=year.intValue()-1; 372 set(YEAR,yr); 373 } 374 375 set(MONTH,mo); 376 break; 377 378 case DATE: 379 set(JD,getJDN()+val); 380 break; 381 case HOUR: 382 case HOUR_OF_DAY: 383 set(JD,getJDN()+val*0.041667); 384 break; 385 386 case MINUTE: 387 set(JD,getJDN()+(double)val/1440.0); 388 break; 389 390 case SECOND: 391 set(JD,getJDN()+(double)val/86400.0); 392 break; 393 default: 394 System.out.println("Error: JulianDate.add: The 'unit' parameter is not recognized="+unit); 395 set(JD,getJDN()+val); // default to adding days 396 break; 397 } 398 399 calcJD(); 400 401 } 402 403 /*** 404 * Calculate calendar date for Julian date field this.jd 405 */ 406 private void calcCalDate() { 407 408 Double jd2=new Double(jd.doubleValue()+0.5); 409 long I=jd2.longValue(); 410 double F=jd2.doubleValue()-(double)I; 411 long A=0; 412 long B=0; 413 414 if (I>2299160) { 415 Double a1=new Double(((double)I-1867216.25)/36524.25); 416 A=a1.longValue(); 417 Double a3=new Double((double)A/4.0); 418 B=I+1+A-a3.longValue(); 419 } 420 else { 421 B=I; 422 } 423 424 double C=(double)B+1524; 425 Double d1=new Double((C-122.1)/365.25); 426 long D=d1.longValue(); 427 Double e1=new Double(365.25*(double)D); 428 long E=e1.longValue(); 429 Double g1=new Double((double)(C-E)/30.6001); 430 long G=g1.longValue(); 431 Double h=new Double((double)G*30.6001); 432 long da=(long)C-E-h.longValue(); 433 date=new Integer((int)da); 434 435 if (G<14L) { 436 month=new Integer((int)(G-2L)); 437 } 438 else { 439 month=new Integer((int)(G-14L)); 440 } 441 442 if (month.intValue()>1) { 443 year=new Integer((int)(D-4716L)); 444 } 445 else { 446 year=new Integer((int)(D-4715L)); 447 } 448 449 // Calculate fractional part as hours, minutes, and seconds 450 Double dhr=new Double(24.0*F); 451 hour=new Integer(dhr.intValue()); 452 Double dmin=new Double((dhr.doubleValue()-(double)dhr.longValue())*60.0); 453 minute=new Integer(dmin.intValue()); 454 Double dsec=new Double((dmin.doubleValue()-(double)dmin.longValue())*60.0); 455 second=new Integer(dsec.intValue()); 456 457 } 458 459 /*** 460 * Calculate day of week class attribute for class attribute jd 461 */ 462 private void calcDayOfWeek() { 463 JulianDay nJd=new JulianDay(getJDN()); 464 nJd.setStartOfDay(); 465 double nJdn=nJd.getJDN()+1.5; 466 int dow=(int)(nJdn%7); 467 dayOfWeek=new Integer(dow); 468 } 469 /*** 470 * Calculate day of year for jd (jd is a class attribute) 471 */ 472 private void calcDayOfYear() { 473 JulianDay julCal=new JulianDay(); 474 julCal.set(year.intValue(),0,1); 475 double doy=jd.doubleValue()-julCal.getJDN(); 476 int idoy=(int)doy; 477 dayOfYear=new Integer(idoy); 478 } 479 /*** 480 * Calculate Julian Date class attribute for class attributes year, month, 481 * date, hour, minute, and second 482 */ 483 private void calcJD() { 484 int mo=month.intValue()+1; 485 int da=date.intValue(); 486 487 int yr=year.intValue(); 488 int A=0; 489 int B=0; 490 int C=0; 491 int D=0; 492 493 if (mo<=2) { 494 yr--; 495 mo+=12; 496 } 497 else { 498 mo=month.intValue()+1; 499 } 500 501 if ((year.intValue()>1582)||((year.intValue()==1582)&&(month.intValue()>=10)&&(date.intValue()>=15))) { 502 Double a1=new Double((double)yr/100.0); 503 A=a1.intValue(); 504 Double b1=new Double((double)A/4.0); 505 B=2-A+b1.intValue(); 506 } 507 else { 508 B=0; 509 } 510 511 Double c1=new Double(365.25*(double)yr); 512 if (yr<0) { 513 c1=new Double(365.25*(double)yr-0.75); 514 } 515 516 C=c1.intValue(); 517 Double d1=new Double(30.6001*(mo+1)); 518 D=d1.intValue(); 519 520 double jdd=B+C+D+da + (hour.doubleValue()/24.0)+ 521 (minute.doubleValue()/1440.0)+(second.doubleValue()/86400.0)+ 522 1720994.5; 523 jd=new Double(jdd); 524 //System.out.println("JulianDay B="+B+" C="+C+" D="+D+" da="+(da+(hour.doubleValue()/24.0)+(minute.doubleValue()/1440.0)+(second.doubleValue()/86400.0))+" jdd="+jdd); 525 } 526 /*** 527 * Returns time difference in days between date specified and the JulianDay of this object 528 * (parameter date-this date) 529 * @return double 530 * @param date com.raben.util.JulianDate 531 */ 532 public double diff(JulianDay date) { 533 return date!=null ? date.getJDN()-getJDN() : Double.NaN; 534 } 535 /*** 536 * Returns true if Julian day number is within 0.001 of parameter jd 537 * @return boolean 538 * @param jd double 539 */ 540 public boolean equals(double jd) { 541 return Math.abs(jd-getJDN())<0.001 ? true : false; 542 } 543 /*** 544 * Return true if JulianDates are equal, false otherwise 545 * @return boolean 546 * @param date com.raben.util.JulianDate 547 */ 548 public boolean equals(JulianDay date) { 549 boolean retVal=false; 550 551 if (date!=null) { 552 retVal=equals(date.getJDN()); 553 } 554 555 return retVal; 556 557 } 558 /*** 559 * Returns the specified field 560 * @param field int The specified field 561 * @return int The field value 562 */ 563 public final int get(int field) { 564 565 switch(field) { 566 case YEAR: 567 return year.intValue(); 568 case MONTH: 569 return month.intValue(); 570 case DAY_OF_MONTH: 571 return date.intValue(); 572 case HOUR: 573 int hr=hour.intValue(); 574 hr=hr > 12 ? hr-=12:hr; 575 return hr; 576 case HOUR_OF_DAY: 577 return hour.intValue(); 578 case MINUTE: 579 return minute.intValue(); 580 case SECOND: 581 return second.intValue(); 582 case DAY_OF_WEEK: 583 calcDayOfWeek(); 584 return dayOfWeek.intValue(); 585 case DAY_OF_YEAR: 586 calcDayOfYear(); 587 return dayOfYear.intValue(); 588 default: 589 return -1; // ISSUE - should throw exception? - what does Calendar do? 590 } 591 592 } 593 /* 594 * Get the UTC date/time string using the current dateFormat. @see setDateFormat 595 * By default the dateFormat is "yyyy-mm-dd hh:mm:ss" 596 * Dates earlier than 0 AD will use be formatted as "yyyy-mm-dd hh:mm" regardless 597 * of dateFormat setting. 598 * @return java.lang.String 599 */ 600 public String getDateTimeStr() { 601 602 String retStr=""; 603 if (getJDN()>EPOCH_0) { 604 dateFormat.setTimeZone(tz); 605 retStr=dateFormat.format(getTime()); 606 } 607 else { 608 StringBuffer strBuf = new StringBuffer(fmt4Dig.format(get(JulianDay.YEAR))); 609 strBuf.append("-"); 610 strBuf.append(fmt2Dig.format(get(JulianDay.MONTH)+1)); 611 strBuf.append("-"); 612 strBuf.append(fmt2Dig.format(get(JulianDay.DATE))); 613 strBuf.append(" "); 614 strBuf.append(fmt2Dig.format(get(JulianDay.HOUR_OF_DAY))); 615 strBuf.append(":"); 616 strBuf.append(fmt2Dig.format(get(JulianDay.MINUTE))); 617 retStr=strBuf.toString(); 618 } 619 return retStr; 620 } 621 /*** 622 * Returns the Julian Date Number as a double 623 * @return double 624 * @param field int 625 */ 626 public final double getJDN() { 627 if (jd==null) { 628 calcJD(); 629 } 630 631 calcJD(); 632 633 return jd.doubleValue(); 634 } 635 /*** 636 * Returns milli-seconds since Jan 1, 1970 637 * @return long 638 */ 639 public long getMilliSeconds() { 640 //JulianDay jd1970=new JulianDay("1970-01-01 0:00"); 641 //double diff=getJDN()-jd1970.getJDN(); 642 double diff=getJDN()-EPOCH_1970; 643 return (long)(diff*86400000.0); 644 } 645 /*** 646 * Return the modified Julian date 647 * @return double 648 * @param field int 649 */ 650 public final double getMJD() { 651 652 return (getJDN() - 2400000.5); 653 } 654 /*** 655 * Return date as YYYYMMDDHHSS string with the least unit to be returned specified 656 * For example to to return YYYYMMDD specify least unit as JulianDay.DATE 657 * @param leastUnit int least unit to be returned 658 */ 659 public String getYMD(int leastUnit) { 660 661 StringBuffer retBuf = new StringBuffer(); 662 int yr = get(JulianDay.YEAR); 663 int mo = get(JulianDay.MONTH) + 1; 664 int da = get(JulianDay.DATE); 665 int hr = get(JulianDay.HOUR_OF_DAY); 666 int min = get(JulianDay.MINUTE); 667 int sec = get(JulianDay.SECOND); 668 669 String yrStr = fmt4Dig.format(yr); 670 671 String moStr = fmt2Dig.format(mo); 672 String daStr = fmt2Dig.format(da); 673 String hrStr = fmt2Dig.format(hr); 674 String minStr = fmt2Dig.format(min); 675 String secStr = fmt2Dig.format(sec); 676 677 switch (leastUnit) { 678 case JulianDay.YEAR : 679 retBuf.append(yrStr); 680 break; 681 682 case JulianDay.MONTH : 683 retBuf.append(yrStr); 684 retBuf.append(moStr); 685 break; 686 687 case JulianDay.DATE : 688 retBuf.append(yrStr); 689 retBuf.append(moStr); 690 retBuf.append(daStr); 691 break; 692 693 case JulianDay.HOUR_OF_DAY : 694 case JulianDay.HOUR : 695 retBuf.append(yrStr); 696 retBuf.append(moStr); 697 retBuf.append(daStr); 698 retBuf.append(hrStr); 699 break; 700 701 case JulianDay.MINUTE : 702 retBuf.append(yrStr); 703 retBuf.append(moStr); 704 retBuf.append(daStr); 705 retBuf.append(hrStr); 706 retBuf.append(minStr); 707 break; 708 709 case JulianDay.SECOND : 710 retBuf.append(yrStr); 711 retBuf.append(moStr); 712 retBuf.append(daStr); 713 retBuf.append(hrStr); 714 retBuf.append(minStr); 715 retBuf.append(secStr); 716 break; 717 } 718 719 return retBuf.toString(); 720 721 } 722 /*** 723 * This method sets Julian day or modified Julian day 724 * @param field int Field to be changed 725 * @param value double The value the field is set to 726 * ISSUE - double values are truncated when setting 727 * YEAR, MONTH<DATE, HOUR,MINUTE, and SECOND - this is not 728 * what should happen. (Should be able to set date to 1.5 to be 729 * the 1st day of month plus 12 hours). 730 */ 731 public void set(int field,double value) { 732 int ivalue=(int)value; 733 734 switch(field) { 735 736 case JD: 737 jd=new Double(value); 738 calcCalDate(); 739 break; 740 741 case MJD: 742 jd=new Double(value+2400000.5); 743 calcCalDate(); 744 break; 745 746 case YEAR: 747 year=new Integer(ivalue); 748 calcJD(); 749 break; 750 751 case MONTH: 752 if (ivalue>11) { 753 int yr=year.intValue()+1; 754 set(YEAR,ivalue); 755 ivalue-=11; 756 } 757 month=new Integer(ivalue); 758 calcJD(); 759 break; 760 761 case DATE: 762 date=new Integer(ivalue); 763 calcJD(); 764 break; 765 766 case HOUR_OF_DAY: 767 case HOUR: 768 hour=new Integer(ivalue); 769 while (hour.intValue()>=24) { 770 add(DATE,1); 771 hour=new Integer(hour.intValue()-24); 772 } 773 calcJD(); 774 break; 775 776 case MINUTE: 777 minute=new Integer(ivalue); 778 while (minute.intValue()>=60) { 779 add(HOUR,1); 780 minute=new Integer(minute.intValue()-60); 781 } 782 calcJD(); 783 break; 784 785 case SECOND: 786 second=new Integer(ivalue); 787 while (second.intValue()>=60) { 788 add(MINUTE,1); 789 second=new Integer(second.intValue()-60); 790 } 791 calcJD(); 792 break; 793 794 } 795 796 } 797 /*** 798 * Set various JulianCalendar fields 799 * Example: 800 * JulianDay jd=new JulianDay(); 801 * jd.set(Calendar.YEAR,1999); 802 * @param field int The field to be set 803 * @param value int The field value 804 */ 805 public final void set(int field,int value) { 806 807 switch (field) { 808 case YEAR: 809 year=new Integer(value); 810 break; 811 812 case MONTH: 813 month=new Integer(value); 814 break; 815 816 case DATE: 817 date=new Integer(value); 818 break; 819 820 case HOUR_OF_DAY: 821 case HOUR: 822 hour=new Integer(value); 823 break; 824 825 case MINUTE: 826 minute=new Integer(value); 827 break; 828 829 case SECOND: 830 second=new Integer(value); 831 break; 832 } 833 calcJD(); 834 835 } 836 /*** 837 * Set year, month, and day 838 * @param year int 839 * @param month int Note - January is 0, December is 11 840 * @param date int 841 */ 842 public final void set(int year,int month,int date) { 843 this.year=new Integer(year); 844 this.month=new Integer(month); 845 this.date=new Integer(date); 846 this.hour=new Integer(0); 847 this.minute=new Integer(0); 848 this.second=new Integer(0); 849 calcJD(); 850 } 851 /*** 852 * Set year, month,day, hour and minute 853 * @param year int 854 * @param month int January is 0, Dec is 11 855 * @param date int 856 * @param hour int 857 * @param minute int 858 */ 859 public final void set(int year,int month,int date,int hour,int minute) { 860 this.year=new Integer(year); 861 this.month=new Integer(month); 862 this.date=new Integer(date); 863 this.hour=new Integer(hour); 864 this.minute=new Integer(minute); 865 this.second=new Integer(0); 866 calcJD(); 867 } 868 /*** 869 * Set year month, day, hour, minute and second 870 * @param year int 871 * @param month int January is 0, December is 11 872 * @param date int 873 * @param hour int 874 * @param minute int 875 * @param second int 876 */ 877 public final void set(int year,int month,int date,int hour,int minute, int second) { 878 this.year=new Integer(year); 879 this.month=new Integer(month); 880 this.date=new Integer(date); 881 this.hour=new Integer(hour); 882 this.minute=new Integer(minute); 883 this.second=new Integer(second); 884 calcJD(); 885 } 886 887 public final void set(JulianDay jd) { 888 set(jd.get(JulianDay.YEAR),jd.get(JulianDay.MONTH),jd.get(JulianDay.DATE), 889 jd.get(JulianDay.HOUR_OF_DAY),jd.get(JulianDay.MINUTE), 890 jd.get(JulianDay.SECOND)); 891 calcJD(); 892 } 893 894 /*** 895 * Set date/time from string 896 * @param str java.lang.String 897 */ 898 public void setDateTime(String str) { 899 try { 900 int vals[]={0,0,0,0,0,0}; 901 str=str.replace('T',' '); 902 StringTokenizer tok=new StringTokenizer(str,"/:- "); 903 904 if (tok.countTokens()>0) { 905 906 // Check if its not a database time format yyyy-mm-dd 907 int j=str.indexOf("-"); 908 909 if ((j==-1)&&(tok.countTokens()==1)) { 910 setYMD(str); 911 } 912 else { 913 int i=0; 914 915 while(tok.hasMoreTokens()) { 916 vals[i++]=Integer.parseInt(tok.nextToken()); 917 } 918 919 set(vals[0],vals[1]-1,vals[2],vals[3],vals[4],vals[5]); 920 921 } 922 923 } 924 925 } catch (NumberFormatException e) { 926 throw new Error(e.toString()); 927 } 928 929 calcJD(); 930 931 932 } 933 /*** 934 * set hour to 23, minute and second to 59 935 */ 936 public void setEndOfDay() { 937 int yr=get(YEAR); 938 int mo=get(MONTH); 939 int da=get(DATE); 940 set(yr,mo,da,23,59,59); 941 } 942 /*** 943 * Set hour,minute, and second to 0 944 */ 945 public void setStartOfDay() { 946 int yr=get(YEAR); 947 int mo=get(MONTH); 948 int da=get(DATE); 949 set(yr,mo,da,0,0,0); 950 } 951 /*** 952 * Set date from Java Date 953 * @param date java.util.Date 954 */ 955 public final void setTime(Date dat) { 956 Calendar cal=new GregorianCalendar(tz); 957 cal.setTime(dat); 958 year=new Integer(cal.get(Calendar.YEAR)); 959 month=new Integer(cal.get(Calendar.MONTH)); 960 date=new Integer(cal.get(Calendar.DATE)); 961 hour=new Integer(cal.get(Calendar.HOUR_OF_DAY)); 962 minute=new Integer(cal.get(Calendar.MINUTE)); 963 second=new Integer(cal.get(Calendar.SECOND)); 964 //System.out.println("JulianCalendar.setTime: year="+year+" month="+month+" date="+date+" hour="+hour+" minute="+minute+" second="+second); 965 calcJD(); 966 //System.out.println("jd="+jd); 967 } 968 /*** 969 * Set date from sting in the form YYYYMMDDhhmmss (YYYY=year MM=month DD=day hh=hr mm=min ss=sec) 970 * @param str java.lang.String 971 */ 972 public void setYMD(String str) { 973 974 int vals[]={0,0,0,0,0,0}; 975 976 if (str.length() >= 4) { 977 vals[0] = Integer.parseInt(str.substring(0, 4)); 978 } 979 if (str.length() >= 6) { 980 vals[1] = Integer.parseInt(str.substring(4, 6)); 981 } 982 983 if (str.length() >= 8) { 984 vals[2] = Integer.parseInt(str.substring(6, 8)); 985 } 986 987 if (str.length() >= 10) { 988 vals[3] = Integer.parseInt(str.substring(8, 10)); 989 } 990 if (str.length() >= 12) { 991 vals[4] = Integer.parseInt(str.substring(10, 12)); 992 } 993 994 if (str.length() >=14) { 995 vals[5] = Integer.parseInt(str.substring(12,14)); 996 } 997 998 set(YEAR,vals[0]); 999 set(MONTH,vals[1]-1); 1000 set(DATE,vals[2]); 1001 set(HOUR_OF_DAY,vals[3]); 1002 set(MINUTE,vals[4]); 1003 set(SECOND,vals[5]); 1004 } 1005 public final String toString() { 1006 1007 StringBuffer buf=new StringBuffer("JulianDay[jdn="); 1008 buf.append(getJDN()); 1009 buf.append(",yr="); 1010 buf.append(get(Calendar.YEAR)); 1011 buf.append(",mo="); 1012 buf.append(get(Calendar.MONTH)); 1013 buf.append(",da="); 1014 buf.append(get(Calendar.DATE)); 1015 buf.append(",hr="); 1016 buf.append(get(Calendar.HOUR_OF_DAY)); 1017 buf.append(",min="); 1018 buf.append(get(Calendar.MINUTE)); 1019 buf.append(",sec="); 1020 buf.append(get(Calendar.SECOND)); 1021 buf.append(",dayOfWeek="); 1022 buf.append(get(DAY_OF_WEEK)); 1023 buf.append(",dayOfYear="); 1024 buf.append(get(DAY_OF_YEAR)); 1025 buf.append("]"); 1026 1027 return buf.toString(); 1028 } 1029 1030 /*** 1031 * Return clone of JulianDay object 1032 * @return Object; 1033 */ 1034 public Object clone() { 1035 JulianDay clone=null; 1036 try { 1037 clone = (JulianDay) super.clone(); 1038 } 1039 catch (CloneNotSupportedException e) { 1040 e.printStackTrace(); 1041 } 1042 return clone; 1043 } 1044 1045 /*** 1046 * Set SimpleDateFormat string 1047 * ISSUE - only valid after Jan 1, 1970 1048 */ 1049 public void setDateFormat(java.lang.String formatStr) { 1050 if ((formatStr!=null)&&(formatStr.length()>0)) { 1051 dateFormat=new SimpleDateFormat(formatStr); 1052 } 1053 } 1054 1055 /*** 1056 * Set SimpleDateFormat for displaying date/time string 1057 * @param dateFormat SimpleDateFormat 1058 */ 1059 public void setDateFormat(SimpleDateFormat dateFormat) { 1060 this.dateFormat=dateFormat; 1061 } 1062 1063 /*** 1064 * Return Java Date 1065 * @return Date 1066 */ 1067 public Date getTime() { 1068 return new Date(getMilliSeconds()); 1069 } 1070 1071 /*** 1072 * Update JulianDay to current time 1073 */ 1074 public void update() { 1075 Calendar cal=new GregorianCalendar(tz); 1076 setTime(cal.getTime()); 1077 } 1078 1079 /*** 1080 * Get increment in days given time unit and increment 1081 * @param unit Time unit (DATE,HOUR,HOUR_OF_DAY,MINUTE, or SECOND 1082 * @param incr Time increment in unit specified 1083 * @return double Increment in days 1084 * @exception If unit is not Julian.DATE, HOUR, HOUR_OF_DAY, MINUTE or SECOND 1085 */ 1086 public static double getIncrement(int unit,int incr) { 1087 double retVal=0.0; 1088 1089 switch(unit) { 1090 case DATE: 1091 retVal=incr; 1092 break; 1093 case HOUR: 1094 case HOUR_OF_DAY: 1095 retVal=incr/24.0; 1096 break; 1097 case MINUTE: 1098 retVal=incr/1440.0; 1099 break; 1100 case SECOND: 1101 retVal=incr/86400.0; 1102 break; 1103 default: 1104 StringBuffer errMsg=new StringBuffer("JulianDay.getIncrement unit="); 1105 errMsg.append(unit); 1106 1107 if ((unit>0)&&(unit<TIME_UNIT.length)) { 1108 errMsg.append(" ("); 1109 errMsg.append(TIME_UNIT[unit]); 1110 errMsg.append(" )"); 1111 } 1112 1113 throw new IllegalArgumentException(errMsg.toString()); 1114 1115 } 1116 1117 return retVal; 1118 } 1119 1120 /*** 1121 * Get java Calendar equivalent of Julian Day 1122 * @return Calendar 1123 */ 1124 public java.util.Calendar getCalendar() { 1125 Calendar cal=GregorianCalendar.getInstance(tz); 1126 1127 cal.set(get(YEAR),get(MONTH),get(DATE),get(HOUR_OF_DAY), 1128 get(MINUTE),get(SECOND)); 1129 //cal.setTimeZone(tz); 1130 return cal; 1131 } 1132 1133 1134 }

This page was automatically generated by Maven