Format Calendar to String in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Calendar

Description

Format Calendar to String in yyyy-MM-dd HH:mm:ss format

Demo Code

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

public class Main{

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
        return date2Str(c, null);
    }//from  w  w  w .jav a2 s .  co  m
    public static String date2Str(Calendar c, String format) {
        if (c == null) {
            return null;
        }
        return date2Str(c.getTime(), format);
    }
    public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
        return date2Str(d, null);
    }
    public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
        if (d == null) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(d);
        return s;
    }

}

Related Tutorials