Java Date to String dateToString(String date, String format)

Here you can find the source of dateToString(String date, String format)

Description

date To String

License

Apache License

Declaration

public static Date dateToString(String date, String format) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .  j av  a 2 s.c  o m
 * Copyright (C) 2015 The CloudKit Open Source Project
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        }
        return dateToString(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date dateToString(String date) {
        if (date == null) {
            return null;
        }
        return dateToString(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date dateToString(String date, String format) {
        if (notEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return new Date();
        } else {
            return null;
        }
    }

    public static String dateToString(Date date, String format) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    public static boolean notEmpty(String s) {
        return s != null && !"".equals(s) && !"null".equals(s);
    }

    public static boolean notEmpty(Object[] arr) {
        return arr != null && arr.length > 0;
    }
}

Related

  1. dateToString(Date pdttValue, String pstrDateFormat)
  2. DateToString(Date thisdate, Locale locale)
  3. dateToString(final Date date)
  4. dateToString(final Date value, final boolean utc)
  5. dateToString(java.util.Date currdate, String strFormat)
  6. dateToString(String str, DateTimeFormatter dateTimeFormatter)
  7. dateToString1(Date date, String formatIn)
  8. dateToString_yyyyMMddHHmmssSSS(Date date)
  9. DateToStringByformat2(Date date)