Java Date to String dateToStringWithTime(Date date)

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

Description

date To String With Time

License

Open Source License

Declaration

public static String dateToStringWithTime(Date date) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from  ww w.ja v  a2 s .c o  m
     * Expanded ISO 8601 Date format yyyy-MM-dd i.e., 2002-12-25 for the 25th day of December in the year 2002
     */
    public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * yyyy-MM-dd hh:mm:ss
     */
    public static String DATETIME_PATTERN = "yyyy-MM-dd hh:mm:ss";

    public static String dateToStringWithTime(Date date) {

        return dateToString(date, DATETIME_PATTERN);
    }

    public static String dateToString(Date date, String pattern) {

        if (date == null) {

            return null;
        }

        try {

            SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
            sfDate.setLenient(false);

            return sfDate.format(date);
        } catch (Exception e) {

            return null;
        }
    }

    public static String dateToString(Date date) {
        return dateToString(date, ISO_EXPANDED_DATE_FORMAT);
    }
}

Related

  1. dateToStringTemporalCoverage(Date date)
  2. dateToStringVerbose(Date date)
  3. dateToStringWithFormat(Date dateToString, String dateFormat)
  4. dateToStringWithPattern(Date date, String pattern)
  5. dateToStringWithPattern(Date date, String pattern)
  6. dateToTimeString(Date date)
  7. dateToUTCDate(Date d)
  8. dateToUtcString(Date date)
  9. dateToW3c(Date d)