Java Parse Date parseDate(String value)

Here you can find the source of parseDate(String value)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String value) throws ParseException 

Method Source Code

//package com.java2s;
/*//from   ww w. j  av a  2  s  .  c  o  m
 * Copyright (C) 2009 Tolven Inc

 * This library is free software; you can redistribute it and/or modify it under the terms of 
 * the GNU Lesser General Public License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;  
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Lesser General Public License for more details.
 *
 * Contact: info@tolvenhealth.com 
 *
 * @author anil
 * @version $Id: HL7DateFormatUtility.java,v 1.1 2009/04/06 00:50:26 jchurin Exp $
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String HL7TSFormat4 = "yyyy";
    private static final String HL7TSFormatL12 = "yyyyMMddHHmm";
    private static final String HL7TSFormatL14 = "yyyyMMddHHmmss";
    private static final String HL7TSFormatL16 = "yyyyMMddHHmmssZZ";
    private static final String HL7TSFormatL8 = "yyyyMMdd";

    public static Date parseDate(String value) throws ParseException {
        if (value == null || value.length() == 0) {
            return null;
        } else if (value.length() == 4) {
            return getSimpleDateFormat(HL7TSFormat4).parse(value);
        } else if (value.length() == 8) {
            return getSimpleDateFormat(HL7TSFormatL8).parse(value);
        } else if (value.length() == 12) {
            return getSimpleDateFormat(HL7TSFormatL12).parse(value);
        } else if (value.length() == 14) {
            return getSimpleDateFormat(HL7TSFormatL14).parse(value);
        } else if (value.length() == 19) {
            return getSimpleDateFormat(HL7TSFormatL16).parse(value);
        } else {
            throw new RuntimeException("Unrecognized format: " + value);
        }

    }

    private static SimpleDateFormat getSimpleDateFormat(String format) {
        return new SimpleDateFormat(format);
    }
}

Related

  1. parseDate(String token)
  2. parseDate(String value)
  3. parseDate(String value)
  4. parseDate(String value)
  5. parseDate(String value)
  6. parseDate(String value)
  7. parseDate(String value, Class targetType, String... formats)
  8. parseDate(String value, String formater)
  9. parseDate(String value, String pattern)