1 16 package org.joda.time.format; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.util.Locale ; 21 22 import org.joda.time.Chronology; 23 import org.joda.time.DateTime; 24 import org.joda.time.DateTimeUtils; 25 import org.joda.time.DateTimeZone; 26 import org.joda.time.MutableDateTime; 27 import org.joda.time.ReadWritableInstant; 28 import org.joda.time.ReadableInstant; 29 import org.joda.time.ReadablePartial; 30 31 76 public class DateTimeFormatter { 77 78 79 private final DateTimePrinter iPrinter; 80 81 private final DateTimeParser iParser; 82 83 private final Locale iLocale; 84 85 private final boolean iOffsetParsed; 86 87 private final Chronology iChrono; 88 89 private final DateTimeZone iZone; 90 91 private final Integer iPivotYear; 92 93 100 public DateTimeFormatter( 101 DateTimePrinter printer, DateTimeParser parser) { 102 super(); 103 iPrinter = printer; 104 iParser = parser; 105 iLocale = null; 106 iOffsetParsed = false; 107 iChrono = null; 108 iZone = null; 109 iPivotYear = null; 110 } 111 112 115 private DateTimeFormatter( 116 DateTimePrinter printer, DateTimeParser parser, 117 Locale locale, boolean offsetParsed, 118 Chronology chrono, DateTimeZone zone, 119 Integer pivotYear) { 120 super(); 121 iPrinter = printer; 122 iParser = parser; 123 iLocale = locale; 124 iOffsetParsed = offsetParsed; 125 iChrono = chrono; 126 iZone = zone; 127 iPivotYear = pivotYear; 128 } 129 130 136 public boolean isPrinter() { 137 return (iPrinter != null); 138 } 139 140 145 public DateTimePrinter getPrinter() { 146 return iPrinter; 147 } 148 149 154 public boolean isParser() { 155 return (iParser != null); 156 } 157 158 163 public DateTimeParser getParser() { 164 return iParser; 165 } 166 167 178 public DateTimeFormatter withLocale(Locale locale) { 179 if (locale == getLocale() || (locale != null && locale.equals(getLocale()))) { 180 return this; 181 } 182 return new DateTimeFormatter(iPrinter, iParser, locale, 183 iOffsetParsed, iChrono, iZone, iPivotYear); 184 } 185 186 191 public Locale getLocale() { 192 return iLocale; 193 } 194 195 210 public DateTimeFormatter withOffsetParsed() { 211 if (iOffsetParsed == true) { 212 return this; 213 } 214 return new DateTimeFormatter(iPrinter, iParser, iLocale, 215 true, iChrono, null, iPivotYear); 216 } 217 218 224 public boolean isOffsetParsed() { 225 return iOffsetParsed; 226 } 227 228 245 public DateTimeFormatter withChronology(Chronology chrono) { 246 if (iChrono == chrono) { 247 return this; 248 } 249 return new DateTimeFormatter(iPrinter, iParser, iLocale, 250 iOffsetParsed, chrono, iZone, iPivotYear); 251 } 252 253 258 public Chronology getChronolgy() { 259 return iChrono; 260 } 261 262 279 public DateTimeFormatter withZone(DateTimeZone zone) { 280 if (iZone == zone) { 281 return this; 282 } 283 return new DateTimeFormatter(iPrinter, iParser, iLocale, 284 false, iChrono, zone, iPivotYear); 285 } 286 287 292 public DateTimeZone getZone() { 293 return iZone; 294 } 295 296 326 public DateTimeFormatter withPivotYear(Integer pivotYear) { 327 if (iPivotYear == pivotYear || (iPivotYear != null && iPivotYear.equals(pivotYear))) { 328 return this; 329 } 330 return new DateTimeFormatter(iPrinter, iParser, iLocale, 331 iOffsetParsed, iChrono, iZone, pivotYear); 332 } 333 334 363 public DateTimeFormatter withPivotYear(int pivotYear) { 364 return withPivotYear(new Integer (pivotYear)); 365 } 366 367 373 public Integer getPivotYear() { 374 return iPivotYear; 375 } 376 377 384 public void printTo(StringBuffer buf, ReadableInstant instant) { 385 checkPrinter(); 386 387 long millis = DateTimeUtils.getInstantMillis(instant); 388 Chronology chrono = DateTimeUtils.getInstantChronology(instant); 389 printTo(buf, millis, chrono); 390 } 391 392 398 public void printTo(Writer out, ReadableInstant instant) throws IOException { 399 checkPrinter(); 400 401 long millis = DateTimeUtils.getInstantMillis(instant); 402 Chronology chrono = DateTimeUtils.getInstantChronology(instant); 403 printTo(out, millis, chrono); 404 } 405 406 414 public void printTo(StringBuffer buf, long instant) { 415 checkPrinter(); 416 417 printTo(buf, instant, null); 418 } 419 420 427 public void printTo(Writer out, long instant) throws IOException { 428 checkPrinter(); 429 430 printTo(out, instant, null); 431 } 432 433 443 public void printTo(StringBuffer buf, ReadablePartial partial) { 444 checkPrinter(); 445 if (partial == null) { 446 throw new IllegalArgumentException ("The partial must not be null"); 447 } 448 449 iPrinter.printTo(buf, partial, iLocale); 450 } 451 452 461 public void printTo(Writer out, ReadablePartial partial) throws IOException { 462 checkPrinter(); 463 if (partial == null) { 464 throw new IllegalArgumentException ("The partial must not be null"); 465 } 466 467 iPrinter.printTo(out, partial, iLocale); 468 } 469 470 480 public String print(ReadableInstant instant) { 481 checkPrinter(); 482 483 StringBuffer buf = new StringBuffer (iPrinter.estimatePrintedLength()); 484 printTo(buf, instant); 485 return buf.toString(); 486 } 487 488 497 public String print(long instant) { 498 checkPrinter(); 499 500 StringBuffer buf = new StringBuffer (iPrinter.estimatePrintedLength()); 501 printTo(buf, instant); 502 return buf.toString(); 503 } 504 505 514 public String print(ReadablePartial partial) { 515 checkPrinter(); 516 517 StringBuffer buf = new StringBuffer (iPrinter.estimatePrintedLength()); 518 printTo(buf, partial); 519 return buf.toString(); 520 } 521 522 private void printTo(StringBuffer buf, long instant, Chronology chrono) { 523 chrono = selectChronology(chrono); 524 DateTimeZone zone = chrono.getZone(); 527 int offset = zone.getOffset(instant); 528 long adjustedInstant = instant + offset; 529 if ((instant ^ adjustedInstant) < 0 && (instant ^ offset) >= 0) { 530 zone = DateTimeZone.UTC; 532 offset = 0; 533 adjustedInstant = instant; 534 } 535 iPrinter.printTo(buf, adjustedInstant, chrono.withUTC(), offset, zone, iLocale); 536 } 537 538 private void printTo(Writer buf, long instant, Chronology chrono) throws IOException { 539 chrono = selectChronology(chrono); 540 DateTimeZone zone = chrono.getZone(); 543 int offset = zone.getOffset(instant); 544 long adjustedInstant = instant + offset; 545 if ((instant ^ adjustedInstant) < 0 && (instant ^ offset) >= 0) { 546 zone = DateTimeZone.UTC; 548 offset = 0; 549 adjustedInstant = instant; 550 } 551 iPrinter.printTo(buf, adjustedInstant, chrono.withUTC(), offset, zone, iLocale); 552 } 553 554 559 private void checkPrinter() { 560 if (iPrinter == null) { 561 throw new UnsupportedOperationException ("Printing not supported"); 562 } 563 } 564 565 595 public int parseInto(ReadWritableInstant instant, String text, int position) { 596 checkParser(); 597 if (instant == null) { 598 throw new IllegalArgumentException ("Instant must not be null"); 599 } 600 601 long instantMillis = instant.getMillis(); 602 Chronology chrono = instant.getChronology(); 603 long instantLocal = instantMillis + chrono.getZone().getOffset(instantMillis); 604 chrono = selectChronology(chrono); 605 606 DateTimeParserBucket bucket = new DateTimeParserBucket 607 (instantLocal, chrono, iLocale, iPivotYear); 608 int newPos = iParser.parseInto(bucket, text, position); 609 instant.setMillis(bucket.computeMillis(false, text)); 610 if (iOffsetParsed && bucket.getZone() == null) { 611 int parsedOffset = bucket.getOffset(); 612 DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); 613 chrono = chrono.withZone(parsedZone); 614 } 615 instant.setChronology(chrono); 616 return newPos; 617 } 618 619 631 public long parseMillis(String text) { 632 checkParser(); 633 634 Chronology chrono = selectChronology(iChrono); 635 DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear); 636 int newPos = iParser.parseInto(bucket, text, 0); 637 if (newPos >= 0) { 638 if (newPos >= text.length()) { 639 return bucket.computeMillis(true, text); 640 } 641 } else { 642 newPos = ~newPos; 643 } 644 throw new IllegalArgumentException (FormatUtils.createErrorMessage(text, newPos)); 645 } 646 647 664 public DateTime parseDateTime(String text) { 665 checkParser(); 666 667 Chronology chrono = selectChronology(null); 668 DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear); 669 int newPos = iParser.parseInto(bucket, text, 0); 670 if (newPos >= 0) { 671 if (newPos >= text.length()) { 672 long millis = bucket.computeMillis(true, text); 673 if (iOffsetParsed && bucket.getZone() == null) { 674 int parsedOffset = bucket.getOffset(); 675 DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); 676 chrono = chrono.withZone(parsedZone); 677 } 678 return new DateTime(millis, chrono); 679 } 680 } else { 681 newPos = ~newPos; 682 } 683 throw new IllegalArgumentException (FormatUtils.createErrorMessage(text, newPos)); 684 } 685 686 703 public MutableDateTime parseMutableDateTime(String text) { 704 checkParser(); 705 706 Chronology chrono = selectChronology(null); 707 DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear); 708 int newPos = iParser.parseInto(bucket, text, 0); 709 if (newPos >= 0) { 710 if (newPos >= text.length()) { 711 long millis = bucket.computeMillis(true, text); 712 if (iOffsetParsed && bucket.getZone() == null) { 713 int parsedOffset = bucket.getOffset(); 714 DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); 715 chrono = chrono.withZone(parsedZone); 716 } 717 return new MutableDateTime(millis, chrono); 718 } 719 } else { 720 newPos = ~newPos; 721 } 722 throw new IllegalArgumentException (FormatUtils.createErrorMessage(text, newPos)); 723 } 724 725 730 private void checkParser() { 731 if (iParser == null) { 732 throw new UnsupportedOperationException ("Parsing not supported"); 733 } 734 } 735 736 743 private Chronology selectChronology(Chronology chrono) { 744 chrono = DateTimeUtils.getChronology(chrono); 745 if (iChrono != null) { 746 chrono = iChrono; 747 } 748 if (iZone != null) { 749 chrono = chrono.withZone(iZone); 750 } 751 return chrono; 752 } 753 754 } 755 | Popular Tags |