Java Hour Format format(Date date, String format)

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

Description

format

License

Open Source License

Declaration

public static String format(Date date, String format) 

Method Source Code


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

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

public class Main {
    public static String format(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }//  ww  w.  j  a  va  2 s  .  c  o  m

    public static String format(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

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

    public static Date format(String date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String formatDate(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    public static Date formatDate(String date) {
        return format(date, "yyyy-MM-dd");
    }
}

Related

  1. format(Date date)
  2. format(Date date)
  3. format(Date date)
  4. format(Date date)
  5. format(Date date, String dateFormat)
  6. format(Date date, String format)
  7. format(Date date, String pattern)
  8. format(Date date, String ptrn)
  9. format(Date dateTime)