Android Date Format FormatToString(String date, String format)

Here you can find the source of FormatToString(String date, String format)

Description

Format To String

Declaration

public static String FormatToString(String date, String format) 

Method Source Code

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

public class Main{
    public static String FormatToString(Date date, String format) {
        try {/*from  www  .j a  v  a 2s  .  c  o  m*/
            java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String FormatToString(String date, String format) {
        try {
            Date dt = DateTimeHelper.ParseStringToDate(date);
            java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(dt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date ParseStringToDate(String date) {
        Date result = null;

        if (date != null && !date.equals("")) {
            String parse = date;
            parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
            parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1MM$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
            parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1mm$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1ss$2");

            SimpleDateFormat format = new SimpleDateFormat(parse);
            try {
                result = format.parse(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}

Related

  1. FormatToString(Date date, String format)
  2. ParseDateToString(Date date)
  3. ParseDateToString(Date date, String format)
  4. date(Date date)
  5. format(Date date)