Format date in long to MM-dd HH:mm format - Android java.util

Android examples for java.util:Date Time

Description

Format date in long to MM-dd HH:mm format

Demo Code

import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String getStringForDate(long date) {

        SimpleDateFormat f = new SimpleDateFormat("MM-dd HH:mm",
                Locale.getDefault());
        Date d = new Date(date);
        String dateString = f.format(d);
        return dateString;

    }//from  ww w  . j a va 2s .  c o  m
    public static String getStringForDate(String date) {

        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm", Locale.getDefault());
        SimpleDateFormat f = new SimpleDateFormat("MM-dd HH:mm",
                Locale.getDefault());
        Date d = new Date();
        try {
            d = formatter.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String dateString = f.format(d);
        return dateString;

    }

}

Related Tutorials