Java tutorial
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; /** * Formats a Date to the format YYYY-MM-DD. * * @param date the Date to parse. * @return A formatted date string. Null if argument is null. */ public static String getMediumDateString(Date date) { final SimpleDateFormat format = new SimpleDateFormat(); format.applyPattern(DEFAULT_DATE_FORMAT); return date != null ? format.format(date) : null; } /** * Formats a Date to the format YYYY-MM-DD. * * @param date the Date to parse. * @param defaultValue the return value if the date argument is null. * @return A formatted date string. The defaultValue argument if date * argument is null. */ public static String getMediumDateString(Date date, String defaultValue) { return date != null ? getMediumDateString(date) : defaultValue; } /** * Formats the current Date to the format YYYY-MM-DD. * * @return A formatted date string. */ public static String getMediumDateString() { return getMediumDateString(Calendar.getInstance().getTime()); } }