Here you can find the source of getDateTimeStringFromCalendar(Calendar calendar)
private static String getDateTimeStringFromCalendar(Calendar calendar)
//package com.java2s; /******************************************************************************* * Copyright Duke Comprehensive Cancer Center and SemanticBits * //from ww w. ja va 2 s . c o m * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/c3pr/LICENSE.txt for details. ******************************************************************************/ import java.sql.Time; import java.util.Calendar; import java.util.Date; public class Main { public static final String DISPLAY_DATE_FORMAT = "MM/dd/yyyy"; public static final String DISPLAY_DATE_TIME_FORMAT = "Day mm/dd/yyyy HH:MM:SS EDT"; public static final String JDBC_DATE_ESCAPE_FORMAT = "yyyy-MM-dd"; private static final String DISPLAY_DATE_DELIMITER = "/"; private static final String ZONE = "EDT"; private static String[] nameOfDay = { "0 no day", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; private static String getDateTimeStringFromCalendar(Calendar calendar) { String dateString = nameOfDay[calendar.get(Calendar.DAY_OF_WEEK)] + " "; dateString += getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE), calendar.get(Calendar.YEAR)); dateString += (" " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(calendar.SECOND) + " " + ZONE); return dateString; } private static String getDateAsString(int month, int date, int year) { StringBuffer dateAsString = new StringBuffer(); if (month > 9) { dateAsString.append(month); } else { dateAsString.append("0" + month); } dateAsString.append(DISPLAY_DATE_DELIMITER); if (date > 9) { dateAsString.append(date); } else { dateAsString.append("0" + date); } dateAsString.append(DISPLAY_DATE_DELIMITER).append(year); return dateAsString.toString(); } /** * converts a java.sql.Date string with appropriate format * * @param sqlDate * java.sql.Date * @param format * specifying the output format for the return string * @return formatted string */ public static String toString(java.sql.Date sqlDate, String format) { // convert Date to String of the form "mm/dd/yyyy" if (DISPLAY_DATE_FORMAT.equalsIgnoreCase(format)) { Calendar calendar = Calendar.getInstance(); calendar.setTime(sqlDate); return getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE), calendar.get(Calendar.YEAR)); } // convert Date to String of the form "mm/dd/yyyy HH:MM" else if (DISPLAY_DATE_TIME_FORMAT.equals(format)) { Calendar calendar = Calendar.getInstance(); calendar.setTime(sqlDate); return getDateTimeStringFromCalendar(calendar); } // convert Date to String of the form "yyyy-mm-dd" else if (JDBC_DATE_ESCAPE_FORMAT.equalsIgnoreCase(format)) { return sqlDate.toString(); } // default util.date to string convertion else { java.util.Date defaultDate = new java.util.Date(sqlDate.getTime()); return defaultDate.toString(); } } /** * converts a java.util.Date string with appropriate format * * @param utilDate * java.util.Date * @param format * specifying the output format for the return string * @return formatted string if utilDate is not null */ public static String toString(java.util.Date utilDate, String format) { if (utilDate == null) { return ""; } // convert Date to String of the form "mm/dd/yyyy" if (DISPLAY_DATE_FORMAT.equalsIgnoreCase(format)) { Calendar calendar = Calendar.getInstance(); calendar.setTime(utilDate); return getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE), calendar.get(Calendar.YEAR)); } // convert Date to String of the form "mm/dd/yyyy HH:MM" else if (DISPLAY_DATE_TIME_FORMAT.equals(format)) { Calendar calendar = Calendar.getInstance(); calendar.setTime(utilDate); return getDateTimeStringFromCalendar(calendar); } // convert Date to String of the form "yyyy-mm-dd" else if (JDBC_DATE_ESCAPE_FORMAT.equalsIgnoreCase(format)) { java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); return sqlDate.toString(); } // default util.date to string convertion else { return utilDate.toString(); } } public static Time getTime(Date date) { return new Time(date.getTime()); } }