Java Date ISO Parse toDate(String isoDate)

Here you can find the source of toDate(String isoDate)

Description

Convert ISO-8601 UTC Date/Time to Date.

License

Apache License

Parameter

Parameter Description
isoDate YYYY-MM-DD'T'hh:mm:ss.SSS'Z'

Declaration

public static Date toDate(String isoDate) 

Method Source Code


//package com.java2s;
/*   Copyright (c) 2015 Magnet Systems, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.//  ww  w . j av  a2 s. co m
 */

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EmptyStackException;
import java.util.Stack;
import java.util.TimeZone;

public class Main {
    private static Stack<SimpleDateFormat> sPool;

    /**
     * Convert ISO-8601 UTC Date/Time to Date.
     * @param isoDate YYYY-MM-DD'T'hh:mm:ss.SSS'Z'
     * @return
     */
    public static Date toDate(String isoDate) {
        SimpleDateFormat fmtr;
        try {
            fmtr = sPool.pop();
        } catch (EmptyStackException e) {
            fmtr = newInstance();
        }
        try {
            return fmtr.parse(isoDate);
        } catch (Throwable e) {
            //    System.err.println("TimeUtil.toDate() failed: "+isoDate);
            //    e.printStackTrace();
            // Invalid ISO date format.
            return null;
        } finally {
            sPool.push(fmtr);
        }
    }

    private static SimpleDateFormat newInstance() {
        SimpleDateFormat fmtr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        fmtr.setTimeZone(TimeZone.getTimeZone("UTC"));
        return fmtr;
    }
}

Related

  1. parseTimestampIso8601(String timestamp)
  2. strongCheckIso8601Date(String date)
  3. time2StringISO(Date date)
  4. timestampAsIsoString(long timestamp)
  5. toCalendar(final String iso8601string)
  6. toIsoString(Calendar value)
  7. toIsoString(Date date)
  8. toStringForComparison(Date date)
  9. tryParse(String isoDateTime)