Java String to Date toDateTimeString(java.util.Date date)

Here you can find the source of toDateTimeString(java.util.Date date)

Description

Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date.

License

Apache License

Parameter

Parameter Description
date The Date

Return

A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.

Declaration

public static String toDateTimeString(java.util.Date date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class Main {
    /**/*from  w ww  .  j  ava2s.  com*/
     * Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS"
     * from a Date. If the seconds are 0 they are left off.
     *
     * @param date
     *            The Date
     * @return A combined data and time string in the format
     *         "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are
     *         0.
     */
    public static String toDateTimeString(java.util.Date date) {
        if (date == null)
            return "";
        String dateString = toDateString(date);
        String timeString = toTimeString(date);

        if (dateString != null && timeString != null) {
            return dateString + " " + timeString;
        } else {
            return "";
        }
    }

    /**
     * Makes a date String in the given from a Date
     *
     * @param date
     *            The Date
     * @return A date String in the given format
     */
    public static String toDateString(java.util.Date date, String format) {
        if (date == null)
            return "";
        SimpleDateFormat dateFormat = null;
        if (format != null) {
            dateFormat = new SimpleDateFormat(format);
        } else {
            dateFormat = new SimpleDateFormat();
        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        return dateFormat.format(date);
    }

    /**
     * Makes a date String in the format MM/DD/YYYY from a Date
     *
     * @param date
     *            The Date
     * @return A date String in the format MM/DD/YYYY
     */
    public static String toDateString(java.util.Date date) {
        return toDateString(date, "MM/dd/yyyy");
    }

    /**
     * Makes a time String in the format HH:MM:SS from a Date. If the seconds
     * are 0, then the output is in HH:MM.
     *
     * @param date
     *            The Date
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(java.util.Date date) {
        if (date == null)
            return "";
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND)));
    }

    /**
     * Makes a time String in the format HH:MM:SS from a separate ints for hour,
     * minute, and second. If the seconds are 0, then the output is in HH:MM.
     *
     * @param hour
     *            The hour int
     * @param minute
     *            The minute int
     * @param second
     *            The second int
     * @return A time String in the format HH:MM:SS or HH:MM
     */
    public static String toTimeString(int hour, int minute, int second) {
        String hourStr;
        String minuteStr;
        String secondStr;

        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = "" + hour;
        }
        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = "" + minute;
        }
        if (second < 10) {
            secondStr = "0" + second;
        } else {
            secondStr = "" + second;
        }
        if (second == 0) {
            return hourStr + ":" + minuteStr;
        } else {
            return hourStr + ":" + minuteStr + ":" + secondStr;
        }
    }
}

Related

  1. toDateString(String fmt, Date date)
  2. toDateStyleBoth(String dateStr)
  3. toDatetime(String sDate)
  4. toDateTimeFormat(String input, String inputfmt, String outputfmt)
  5. toDateTimeSSSString(long datetime)
  6. toDateTimeString(java.util.Date date)
  7. toDateWithFormatString(String date, String format)
  8. toDateWithTimezone(String dateTime, String pattern)
  9. transformStringToDate(String dateString)