Java Date Parse dateFromString(final String value)

Here you can find the source of dateFromString(final String value)

Description

Parses string into a Date object, first attempting as long(millis) then using the SimpleDateFormat: "MM-dd-yyyy"

License

Open Source License

Parameter

Parameter Description
value string representation of date/or a long (millis)

Return

date object parsed from string

Declaration

public static java.util.Date dateFromString(final String value) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   w w w .  j  a  v  a2s . c o  m*/
 * Copyright (c) 2013 The Johns Hopkins University/Applied Physics Laboratory
 *                             All rights reserved.
 *
 * This material may be used, modified, or reproduced by or for the U.S.
 * Government pursuant to the rights granted under the clauses at
 * DFARS 252.227-7013/7014 or FAR 52.227-14.
 *
 * 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
 *
 * NO WARRANTY.   THIS MATERIAL IS PROVIDED "AS IS."  JHU/APL DISCLAIMS ALL
 * WARRANTIES IN THE MATERIAL, WHETHER EXPRESS OR IMPLIED, INCLUDING (BUT NOT
 * LIMITED TO) ANY AND ALL IMPLIED WARRANTIES OF PERFORMANCE,
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF
 * INTELLECTUAL PROPERTY RIGHTS. ANY USER OF THE MATERIAL ASSUMES THE ENTIRE
 * RISK AND LIABILITY FOR USING THE MATERIAL.  IN NO EVENT SHALL JHU/APL BE
 * LIABLE TO ANY USER OF THE MATERIAL FOR ANY ACTUAL, INDIRECT,
 * CONSEQUENTIAL, SPECIAL OR OTHER DAMAGES ARISING FROM THE USE OF, OR
 * INABILITY TO USE, THE MATERIAL, INCLUDING, BUT NOT LIMITED TO, ANY DAMAGES
 * FOR LOST PROFITS.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    /**
     * Parses string into a Date object, first attempting as long(millis) then using the SimpleDateFormat: "MM-dd-yyyy"
     *
     * @param value string representation of date/or a long (millis)
     * @return date object parsed from string
     */
    public static java.util.Date dateFromString(final String value) throws Exception {
        java.util.Date d = null;
        try {
            d = new java.util.Date();
            d.setTime(Long.parseLong(value));
        } catch (NumberFormatException nfe) {
            try {
                d = new SimpleDateFormat("MM-dd-yyyy").parse(value);
            } catch (ParseException e) {
                throw new Exception(String.format("Failed to parse '%s' as date", value));
            }
        }
        return d;
    }
}

Related

  1. dateFromFilename(File file, Pattern fileNamePattern, DateFormat dateFormat)
  2. dateFromHourAndMinutes(final String pTime)
  3. dateFromISO8601(String iso)
  4. dateFromISO8601(String time)
  5. dateFromISODate(String isoDate)
  6. dateFromString(String date)
  7. dateFromString(String date, String pattern)
  8. dateFromString(String dateString)
  9. dateFromString(String dateString)