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

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

Description

convert a date to a format string default format:yyyy-MM-dd HH:mm:ss

License

Open Source License

Parameter

Parameter Description
date a parameter
format a parameter

Declaration

public static String date2Str(Date date, String format) 

Method Source Code

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

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**// w w  w .  j a v  a  2 s . c  o  m
     * convert a date to a format string default format:yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format) {
        if (date != null)
            return long2Datestr(date.getTime(), format);
        else
            return long2Datestr(new Date().getTime(), format);
    }

    public static String long2Datestr(long time, String format) {
        if (format == null || format.trim().equals(""))
            format = "yyyy-MM-dd HH:mm:ss";

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(time);
    }
}

Related

  1. date2Str(Date aDate)
  2. Date2Str(Date date)
  3. date2Str(Date date)
  4. date2Str(Date date)
  5. date2Str(Date date, String dateFormat)
  6. date2Str(Date date, String format)
  7. date2Str(Date date, String partten)
  8. date2Str(Date date, String pattern)
  9. date2Str(Date value)