Returns the given date as a well formatted string yyyy-MM-dd. - Java java.util

Java examples for java.util:Date Format

Description

Returns the given date as a well formatted string yyyy-MM-dd.

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar calendar = Calendar.getInstance();
        System.out.println(format(calendar));
    }/*  w w  w .jav a2s .com*/

    /**
     * Default date format in the form 2013-03-18.
     */
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
            "yyyy-MM-dd");

    /**
     * Returns the given date as a well formatted string. The above defined date
     * format is used.
     * 
     * @param calendar date to be returned as a string
     * @return formatted string
     */
    public static String format(Calendar calendar) {
        if (calendar == null) {
            return null;
        }
        return DATE_FORMAT.format(calendar.getTime());
    }
}

Related Tutorials