Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/util/DateConverter.java
40951 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* The MIT License32*33* Copyright (c) 2004-2015 Paul R. Holser, Jr.34*35* Permission is hereby granted, free of charge, to any person obtaining36* a copy of this software and associated documentation files (the37* "Software"), to deal in the Software without restriction, including38* without limitation the rights to use, copy, modify, merge, publish,39* distribute, sublicense, and/or sell copies of the Software, and to40* permit persons to whom the Software is furnished to do so, subject to41* the following conditions:42*43* The above copyright notice and this permission notice shall be44* included in all copies or substantial portions of the Software.45*46* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,47* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF48* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND49* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE50* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION51* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION52* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.53*/5455package jdk.internal.joptsimple.util;5657import java.text.DateFormat;58import java.text.ParsePosition;59import java.text.SimpleDateFormat;60import java.util.Date;61import java.util.Locale;6263import jdk.internal.joptsimple.ValueConversionException;64import jdk.internal.joptsimple.ValueConverter;65import jdk.internal.joptsimple.internal.Messages;6667/**68* Converts values to {@link Date}s using a {@link DateFormat} object.69*70* @author <a href="mailto:[email protected]">Paul Holser</a>71*/72public class DateConverter implements ValueConverter<Date> {73private final DateFormat formatter;7475/**76* Creates a converter that uses the given date formatter/parser.77*78* @param formatter the formatter/parser to use79* @throws NullPointerException if {@code formatter} is {@code null}80*/81public DateConverter( DateFormat formatter ) {82if ( formatter == null )83throw new NullPointerException( "illegal null formatter" );8485this.formatter = formatter;86}8788/**89* Creates a converter that uses a {@link SimpleDateFormat} with the given date/time pattern. The date formatter90* created is not {@link SimpleDateFormat#setLenient(boolean) lenient}.91*92* @param pattern expected date/time pattern93* @return the new converter94* @throws NullPointerException if {@code pattern} is {@code null}95* @throws IllegalArgumentException if {@code pattern} is invalid96*/97public static DateConverter datePattern( String pattern ) {98SimpleDateFormat formatter = new SimpleDateFormat( pattern );99formatter.setLenient( false );100101return new DateConverter( formatter );102}103104public Date convert( String value ) {105ParsePosition position = new ParsePosition( 0 );106107Date date = formatter.parse( value, position );108if ( position.getIndex() != value.length() )109throw new ValueConversionException( message( value ) );110111return date;112}113114public Class<Date> valueType() {115return Date.class;116}117118public String valuePattern() {119return formatter instanceof SimpleDateFormat120? ( (SimpleDateFormat) formatter ).toPattern()121: "";122}123124private String message( String value ) {125String key;126Object[] arguments;127128if ( formatter instanceof SimpleDateFormat ) {129key = "with.pattern.message";130arguments = new Object[] { value, ( (SimpleDateFormat) formatter ).toPattern() };131} else {132key = "without.pattern.message";133arguments = new Object[] { value };134}135136return Messages.message(137Locale.getDefault(),138"jdk.internal.joptsimple.ExceptionMessages",139DateConverter.class,140key,141arguments );142}143}144145146