< prev index next >

src/java.base/share/classes/java/text/SimpleDateFormat.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 888         calendar.add( Calendar.YEAR, -80 );
 889         parseAmbiguousDatesAsAfter(calendar.getTime());
 890     }
 891 
 892     /* Define one-century window into which to disambiguate dates using
 893      * two-digit years.
 894      */
 895     private void parseAmbiguousDatesAsAfter(Date startDate) {
 896         defaultCenturyStart = startDate;
 897         calendar.setTime(startDate);
 898         defaultCenturyStartYear = calendar.get(Calendar.YEAR);
 899     }
 900 
 901     /**
 902      * Sets the 100-year period 2-digit years will be interpreted as being in
 903      * to begin on the date the user specifies.
 904      *
 905      * @param startDate During parsing, two digit years will be placed in the range
 906      * <code>startDate</code> to <code>startDate + 100 years</code>.
 907      * @see #get2DigitYearStart

 908      * @since 1.2
 909      */
 910     public void set2DigitYearStart(Date startDate) {
 911         parseAmbiguousDatesAsAfter(new Date(startDate.getTime()));
 912     }
 913 
 914     /**
 915      * Returns the beginning date of the 100-year period 2-digit years are interpreted
 916      * as being within.
 917      *
 918      * @return the start of the 100-year period into which two digit years are
 919      * parsed
 920      * @see #set2DigitYearStart
 921      * @since 1.2
 922      */
 923     public Date get2DigitYearStart() {
 924         return (Date) defaultCenturyStart.clone();
 925     }
 926 
 927     /**
 928      * Formats the given <code>Date</code> into a date/time string and appends
 929      * the result to the given <code>StringBuffer</code>.
 930      *
 931      * @param date the date-time value to be formatted into a date-time string.
 932      * @param toAppendTo where the new date-time text is to be appended.
 933      * @param pos the formatting position. On input: an alignment field,
 934      * if desired. On output: the offsets of the alignment field.
 935      * @return the formatted date-time string.
 936      * @exception NullPointerException if the given {@code date} is {@code null}.
 937      */
 938     @Override
 939     public StringBuffer format(Date date, StringBuffer toAppendTo,
 940                                FieldPosition pos)
 941     {
 942         pos.beginIndex = pos.endIndex = 0;
 943         return format(date, toAppendTo, pos.getFieldDelegate());
 944     }
 945 
 946     // Called from Format after creating a FieldDelegate
 947     private StringBuffer format(Date date, StringBuffer toAppendTo,
 948                                 FieldDelegate delegate) {
 949         // Convert input date to time field list
 950         calendar.setTime(date);
 951 
 952         boolean useDateFormatSymbols = useDateFormatSymbols();
 953 
 954         for (int i = 0; i < compiledPattern.length; ) {
 955             int tag = compiledPattern[i] >>> 8;
 956             int count = compiledPattern[i++] & 0xff;


   1 /*
   2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 888         calendar.add( Calendar.YEAR, -80 );
 889         parseAmbiguousDatesAsAfter(calendar.getTime());
 890     }
 891 
 892     /* Define one-century window into which to disambiguate dates using
 893      * two-digit years.
 894      */
 895     private void parseAmbiguousDatesAsAfter(Date startDate) {
 896         defaultCenturyStart = startDate;
 897         calendar.setTime(startDate);
 898         defaultCenturyStartYear = calendar.get(Calendar.YEAR);
 899     }
 900 
 901     /**
 902      * Sets the 100-year period 2-digit years will be interpreted as being in
 903      * to begin on the date the user specifies.
 904      *
 905      * @param startDate During parsing, two digit years will be placed in the range
 906      * <code>startDate</code> to <code>startDate + 100 years</code>.
 907      * @see #get2DigitYearStart
 908      * @throws NullPointerException if {@code startDate} is {@code null}.
 909      * @since 1.2
 910      */
 911     public void set2DigitYearStart(Date startDate) {
 912         parseAmbiguousDatesAsAfter(new Date(startDate.getTime()));
 913     }
 914 
 915     /**
 916      * Returns the beginning date of the 100-year period 2-digit years are interpreted
 917      * as being within.
 918      *
 919      * @return the start of the 100-year period into which two digit years are
 920      * parsed
 921      * @see #set2DigitYearStart
 922      * @since 1.2
 923      */
 924     public Date get2DigitYearStart() {
 925         return (Date) defaultCenturyStart.clone();
 926     }
 927 
 928     /**
 929      * Formats the given <code>Date</code> into a date/time string and appends
 930      * the result to the given <code>StringBuffer</code>.
 931      *
 932      * @param date the date-time value to be formatted into a date-time string.
 933      * @param toAppendTo where the new date-time text is to be appended.
 934      * @param pos the formatting position. On input: an alignment field,
 935      * if desired. On output: the offsets of the alignment field.
 936      * @return the formatted date-time string.
 937      * @exception NullPointerException if any of the parameters is {@code null}.
 938      */
 939     @Override
 940     public StringBuffer format(Date date, StringBuffer toAppendTo,
 941                                FieldPosition pos)
 942     {
 943         pos.beginIndex = pos.endIndex = 0;
 944         return format(date, toAppendTo, pos.getFieldDelegate());
 945     }
 946 
 947     // Called from Format after creating a FieldDelegate
 948     private StringBuffer format(Date date, StringBuffer toAppendTo,
 949                                 FieldDelegate delegate) {
 950         // Convert input date to time field list
 951         calendar.setTime(date);
 952 
 953         boolean useDateFormatSymbols = useDateFormatSymbols();
 954 
 955         for (int i = 0; i < compiledPattern.length; ) {
 956             int tag = compiledPattern[i] >>> 8;
 957             int count = compiledPattern[i++] & 0xff;


< prev index next >