Android Date to String Convert dateToString(String format, String date)

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

Description

date To String

Declaration

public static String dateToString(String format, String date) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;

public class Main {
    public static Calendar calendar = Calendar.getInstance();
    public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String dateToString(Date date) {
        return dateToString(DEFAULT_DATE_FORMAT, date);
    }/*w w w .  java  2  s .c o  m*/

    public static String dateToString(String format, String date) {
        long time = getTimeInMillis(date);
        calendar.clear();
        calendar.setTimeInMillis(time);

        return dateToString(format, calendar.getTime());
    }

    public static String dateToString(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    @SuppressLint("SimpleDateFormat")
    public static long getTimeInMillis(String date) {
        Date currentDate;
        try {
            String format = "HH:mm";
            if (date.contains("-")) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat dateForamt = new SimpleDateFormat(format);
            currentDate = dateForamt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();

            currentDate = new Date();
        }
        calendar.setTime(currentDate);
        return calendar.getTimeInMillis();
    }
}

Related

  1. javaDateToTimeSpan(final Date date)
  2. toGMTDate(Date localDate)
  3. toString(Date date)
  4. getStrFromDate(Date date)
  5. dateToString(Date date)
  6. dateToString(String format, Date date)
  7. dateToString(Date time)
  8. getFriendlyLocalDateTime(Date date)
  9. dateTimeToString(final Date date)