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

Open Source 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;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    /**//from  ww  w.j a va  2 s .c om
     * 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 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) {
        if (date == null)
            return "";
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        String monthStr;
        String dayStr;
        String yearStr;

        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = "" + month;
        }
        if (day < 10) {
            dayStr = "0" + day;
        } else {
            dayStr = "" + day;
        }
        yearStr = "" + year;
        return monthStr + "/" + dayStr + "/" + yearStr;
    }

    public static String toDateString(java.util.Date d, String pattern) {

        String rs = "";

        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            rs = formatter.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isFirstStartDate(rs) ? "" : rs;

    }

    /**
     * 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;
    }

    private static boolean isFirstStartDate(String strDate) {
        if (strDate.compareTo("1970-01-01") == 0 || strDate.compareTo("1899-12-30") == 0
                || strDate.compareTo("1969-12-31") == 0) {
            return true;
        }
        return false;
    }
}

Related

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