Java Date Format convertDateToString(Date date, String pattern)

Here you can find the source of convertDateToString(Date date, String pattern)

Description

convert Date To String

License

Open Source License

Declaration

public static String convertDateToString(Date date, String pattern) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . java 2  s.co m*/
 * Copyright (C) 2010 dungnv. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     *
     * @param date to convert
     * @return String
     * @throws Exception if error
     */
    public static String convertDateToString(Date date) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (date == null) {
            return "";
        }
        try {
            return dateFormat.format(date);
        } catch (Exception e) {
            throw e;
        }
    }

    public static String convertDateToString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        if (date == null) {
            return "";
        }
        try {
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}

Related

  1. convertDateToString(Date date, boolean millis)
  2. convertDateToString(Date date, int addHours)
  3. convertDateToString(Date date, String dateFormat)
  4. convertDateToString(Date date, String format)
  5. convertDateToString(Date date, String pattern)
  6. convertDateToString(Date date, String patternString, Locale locale)
  7. convertDateToString(Date dateObject, String dateFormat)
  8. convertDateToStringByFormat(Date date)
  9. convertDateToStringT(final Date date)