Java tutorial
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { /** * Year-Month-Day pattern: 2015-01-28 */ public static final String DATE_YEAR_MONTH_DAY_STRING_FORMAT = "yyyy-MM-dd"; /** * @param date * @return String in the format {@link #DATE_YEAR_MONTH_DAY_STRING_FORMAT} */ public static String dateToString(Date date) { return dateToString(date, DATE_YEAR_MONTH_DAY_STRING_FORMAT); } /** * @param date * @param * @return String in the format passed */ public static String dateToString(Date date, String format) { if (date == null) { return ""; } SimpleDateFormat df = new SimpleDateFormat(format, Locale.US); return df.format(date); } }