Java SQL Time Format formatCharDateYMD(String str)

Here you can find the source of formatCharDateYMD(String str)

Description

format Char Date YMD

License

Open Source License

Declaration

public static Date formatCharDateYMD(String str) 

Method Source Code


//package com.java2s;
import java.sql.Date;

import java.text.*;

public class Main {
    public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd";

    public static Date formatCharDateYMD(String str) {
        return formatCharDateYMD(str, DEFAULT_DATE_YMD_FORMAT);
    }// ww  w  .j av  a 2  s . c o  m

    public static Date formatCharDateYMD(String str, String format) {

        if (str == null || str.trim().length() == 0) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        ParsePosition pos = new ParsePosition(0);

        java.util.Date date = sdf.parse(str, pos);

        if (date == null) {
            return null;
        }
        return new Date(date.getTime());
    }

    public static Date formatCharDateYMD(String yy, String mm, String dd) {

        if (yy == null || mm == null || dd == null || yy.trim().length() == 0 || mm.trim().length() == 0
                || dd.trim().length() == 0) {
            return null;
        }
        return formatCharDateYMD(yy + "-" + (mm != null && mm.length() == 1 ? "0" + mm : mm) + "-"
                + (dd != null && dd.length() == 1 ? "0" + dd : dd));
    }
}

Related

  1. formatCharDateYMDHMS(String str)
  2. formatDate(String Format)
  3. formatDateWithSlashes(java.sql.Date date)
  4. formatDateYM(Date date, String datePtn)