Java SQL Date Parse parse(String date)

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

Description

Parse the string of data into java.sql.Date object

License

Open Source License

Parameter

Parameter Description
date the input data string

Exception

Parameter Description
Exception if error occurs

Return

java.sql.Date object

Declaration

public static java.sql.Date parse(String date) throws Exception 

Method Source Code

//package com.java2s;
/**//from w ww. j  a v a2s.co m
 * Copyright (C) 2016 LibRec
 * <p>
 * This file is part of LibRec.
 * LibRec is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * LibRec 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 General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with LibRec. If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.SimpleDateFormat;

public class Main {
    /** pattern */
    public final static String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";

    /**
     * Parse the string of data into {@code java.sql.Date} object
     *
     * @param date the input data string
     * @return {@code java.sql.Date} object
     * @throws Exception if error occurs
     */
    public static java.sql.Date parse(String date) throws Exception {
        return parse(date, PATTERN_yyyy_MM_dd);
    }

    /**
     * Parse the string of data into {@code java.sql.Date} object
     * with specified pattern
     *
     * @param date     the input data string
     * @param pattern  the given pattern
     * @return {@code java.sql.Date} object
     * @throws Exception if error occurs
     */
    public static java.sql.Date parse(String date, String pattern) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return new java.sql.Date(sdf.parse(date).getTime());
    }

    /**
     * Convert time in milliseconds to human-readable format.
     *
     * @param msType The time in milliseconds
     * @return a human-readable string version of the time
     */
    public static String parse(long msType) {
        long original = msType;
        int ms = (int) (msType % 1000);

        original = original / 1000;
        int sec = (int) (original % 60);

        original = original / 60;
        int min = (int) (original % 60);

        original = original / 60;
        int hr = (int) (original % 24);

        original = original / 24;
        int day = (int) original;

        if (day > 1) {
            return String.format("%d days, %02d:%02d:%02d.%03d", day, hr, min, sec, ms);
        } else if (day > 0) {
            return String.format("%d day, %02d:%02d:%02d.%03d", day, hr, min, sec, ms);
        } else if (hr > 0) {
            return String.format("%02d:%02d:%02d", hr, min, sec);
        } else {
            return String.format("%02d:%02d", min, sec);
        }
    }
}

Related

  1. getSqlDate(String str)
  2. getSqlDateByShortStr(String dateStr)
  3. getSqlDateFromStr(DateFormat format, String s, boolean need_default)
  4. getSQLDateFromStr(String s)
  5. getSqlDateFromString(String dateAsString, String format)
  6. parseDate(final String dateString)
  7. parseDate(String date)
  8. parseDate(String dateStr, String formatStr)
  9. parseDate(String exifDate)