Here you can find the source of toDateTimeString(java.util.Date date)
Parameter | Description |
---|---|
date | The Date |
public static String toDateTimeString(java.util.Date date)
//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; } }