convert Date To String - Android java.util

Android examples for java.util:Date Convert

Description

convert Date To String

Demo Code

import android.content.Context;
import android.provider.Settings;
import android.telephony.PhoneNumberUtils;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/*from   ww  w. jav a  2  s .  c  o  m*/
     * Convert date to string
     * 
     * @param date
     * @param format
     * @return
     */
    public static String convertDateToString(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String strDate;
        try {
            strDate = dateFormat.format(date);
            return strDate;
        } catch (IllegalArgumentException e) {
            return dateFormat.format(new Date(System.currentTimeMillis()));
        }
    }
    public static String convertDateToString(Calendar calendar,
            String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String strDate;
        try {
            strDate = dateFormat.format(calendar.getTime());
            return strDate;
        } catch (IllegalArgumentException e) {
            return dateFormat.format(new Date(System.currentTimeMillis()));
        }
    }

}

Related Tutorials