Format date value To String in yyyy-MM-dd format - Android java.util

Android examples for java.util:Date Format

Description

Format date value To String in yyyy-MM-dd format

Demo Code

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{

    /**//from ww  w .  j  a v a  2 s . c om
     * Convert date to string.
     * 
     * @param date Date to convert
     * @param format Date format
     * 
     * @return String of date
     */
    public static String dateToString(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);

        return df.format(date);
    }
    /**
     * Convert date to string with default format yyyy-MM-dd
     * 
     * @param date Date to convert
     * 
     * @return String of date.
     */
    public static String dateToString(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return df.format(date);
    }

}

Related Tutorials