getTodayDateTime / getTodayDateTimeStd / getDateFormat

getTodayDateTime / getTodayDateTimeStd / getDateFormat

/**

* 현재날짜를 “yyyyMMddHHmmss” 형식의 String 형태로 리턴한다.

*/

public static String getTodayDateTime() {
  Calendar cal = Calendar.getInstance();
  StringBuffer today = new StringBuffer();
  today.append(String.format(“%04d”, cal.get(Calendar.YEAR)));
  today.append(String.format(“%02d”, cal.get(Calendar.MONTH) + 1));
  today.append(String.format(“%02d”, cal.get(Calendar.DAY_OF_MONTH)));
  today.append(String.format(“%02d”, cal.get(Calendar.HOUR_OF_DAY)));
  today.append(String.format(“%02d”, cal.get(Calendar.MINUTE)));
  today.append(String.format(“%02d”, cal.get(Calendar.SECOND)));
  return today.toString();
 }
 

/**

* 현재날짜를 “yyyy/MM/dd HH:mm:ss” 형식의 String 형태로 리턴한다.

*/
 public static String getTodayDateTimeStd() {
  Calendar cal = Calendar.getInstance();
  StringBuffer today = new StringBuffer();
  today.append(String.format(“%04d”, cal.get(Calendar.YEAR)));
  today.append(“/”);
  today.append(String.format(“%02d”, cal.get(Calendar.MONTH) + 1));
  today.append(“/”);
  today.append(String.format(“%02d”, cal.get(Calendar.DAY_OF_MONTH)));
  today.append(” “);
  today.append(String.format(“%02d”, cal.get(Calendar.HOUR_OF_DAY)));
  today.append(“:”);
  today.append(String.format(“%02d”, cal.get(Calendar.MINUTE)));
  today.append(“:”);
  today.append(String.format(“%02d”, cal.get(Calendar.SECOND)));
  return today.toString();
 }
 
 /**
  * Date 객체를 형식에 맞는 String 형태로 리턴한다.
  * format 예 : “yyyy/MM/dd”
  *
  * @param date
  * @param format
  * @return
  */
 public static String getDateFormat(Date date, String format) {
  return new java.text.SimpleDateFormat(format).format(date);
 }
 
 /**
  * Calendar 객체를 형식에 맞는 String 형태로 리턴한다.
  * format 예 : “yyyy/MM/dd”
  *
  * @param cal
  * @param format
  * @return
  */
 public static String getDateFormat(Calendar cal, String format) {
  return new java.text.SimpleDateFormat(format).format(castCalendarToDate((cal)));
 }
 
 /**
  * Calendar 객체를 Date 객체로 만들어 리턴한다.
  *
  * @param cal
  * @return
  */
 public static Date castCalendarToDate(Calendar cal) {
  return new java.sql.Date(cal.getTimeInMillis());
 }