Format Date to string in yyyy-MM-dd HH:mm:ss format, catch exception - Android java.util

Android examples for java.util:Date Format

Description

Format Date to string in yyyy-MM-dd HH:mm:ss format, catch exception

Demo Code

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{

    @SuppressLint("SimpleDateFormat")
    public static Date getDate(Long dateLong) {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String d = format.format(dateLong);
        Date date;//from  w w w. j  a va 2  s.  c  om
        try {
            date = format.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }

}

Related Tutorials