Java Parse Date Pattern YYYY parse(String input)

Here you can find the source of parse(String input)

Description

Try and evaluate the string and return a date

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static Date parse(String input) 

Method Source Code

//package com.java2s;
/*/*from w  ww .  j a v  a2s  .c o m*/
 * Author Stephen Booysen
 *
 * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * Try and evaluate the string and return a date
     * 
     * @param input
     * @return
     */
    public static Date parse(String input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy/MM/dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dd MMM yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(input);
        } catch (Exception e) {

        }
        try {
            return new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(input);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(input);
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input, String format) {

        try {
            return new SimpleDateFormat(format).format(input);

        } catch (Exception e) {
        }

        return null;
    }
}

Related

  1. parse(String dateString)
  2. parse(String dateString, String dateFormat)
  3. parse(String dateText)
  4. parse(String ds)
  5. parse(String format, String val)
  6. parse(String original)
  7. parse(String param)
  8. parse(String param)
  9. parse(String s)