Java Date to String date2String(Date date)

Here you can find the source of date2String(Date date)

Description

date String

License

Open Source License

Declaration

public static String date2String(Date date) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static SimpleDateFormat sdfDash = new SimpleDateFormat("MM-dd-yyyy");
    private static SimpleDateFormat sdfSlash = new SimpleDateFormat("MM/dd/yyyy");
    private static SimpleDateFormat sdfColon = new SimpleDateFormat("MM:dd:yyyy");

    public static String date2String(Date date) {
        return date2String(date, "-");
    }/*ww  w. j av a  2  s.c  o  m*/

    public static String date2String(Date date, String delimiter) {
        if (date == null)
            return null;
        if (sdfDash == null)
            sdfDash = new SimpleDateFormat("MM-dd-yyyy");
        if (sdfSlash == null)
            sdfSlash = new SimpleDateFormat("MM/dd/yyyy");
        if (sdfColon == null)
            sdfColon = new SimpleDateFormat("MM:dd:yyyy");
        if (delimiter == null) {
            return sdfDash.format(date);
        } else if (delimiter.trim().equals("-")) {
            return sdfDash.format(date);
        } else if (delimiter.trim().equals("/")) {
            return sdfSlash.format(date);
        } else if (delimiter.trim().equals(":")) {
            return sdfColon.format(date);
        } else {
            return sdfDash.format(date);
        }
    }
}

Related

  1. date2string(Date date)
  2. date2String(Date date)
  3. date2String(Date date)
  4. date2string(Date date)
  5. date2String(Date date)
  6. date2string(Date date)
  7. date2String(Date date)
  8. date2String(Date date)
  9. date2String(Date date, DateFormat dateFormat)