Java SQL Date Get getDate(String value)

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

Description

Helper to create a date given a string of the form yyyy-mm-dd.

License

Open Source License

Parameter

Parameter Description
value the value. May be null

Return

the corresponding date, or null if value is null

Declaration

public static Date getDate(String value) 

Method Source Code

//package com.java2s;
/*//w  w  w  . j  ava2  s . c  o m
 * Version: 1.0
 *
 * The contents of this file are subject to the OpenVPMS License Version
 * 1.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.openvpms.org/license/
 *
 * Software distributed under the License is distributed on an 'AS IS' basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Copyright 2015 (C) OpenVPMS Ltd. All Rights Reserved.
 */

import java.sql.Timestamp;

import java.util.Date;

public class Main {
    /**
     * Helper to create a date given a string of the form <em>yyyy-mm-dd</em>.
     *
     * @param value the value. May be {@code null}
     * @return the corresponding date, or {@code null} if {@code value} is null
     */
    public static Date getDate(String value) {
        return value != null ? getDatetime(value + " 0:0:0") : null;
    }

    /**
     * Helper to create a date-time given a string of the form
     * <em>yyyy-mm-dd hh:mm:ss</em>.
     *
     * @param value the value. May be {@code null}
     * @return the corresponding date-time or {@code null} if {@code value} is null
     */
    public static Date getDatetime(String value) {
        return value != null ? new Date(Timestamp.valueOf(value).getTime()) : null; // use Date, for easy comparison
    }
}

Related

  1. getDate(String date)
  2. getDate(String format)
  3. getDate(String pattern)
  4. getDate(String strDate)
  5. getDate(String theDateStr, int days)
  6. getDate(String year, String month, String day)
  7. getDate(Timestamp establishdate)
  8. getDate(Timestamp timestamp)
  9. getDate(Timestamp timestamp)