Android Time Format formatDateTime(String date, String format)

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

Description

format Date Time

Declaration

public static String formatDateTime(String date, String format) 

Method Source Code

//package com.java2s;

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

import android.annotation.SuppressLint;

public class Main {
    private static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd HH:mm:ss";

    public static String formatDateTime(String date, String format) {
        return convertDateTime(convertDateTime(date, DEFAULT_FORMAT_DATE),
                format);/*from  www .java2  s .  c  om*/
    }

    @SuppressLint("SimpleDateFormat")
    public static long convertDateTime(String date) {
        return convertDateTime(date, null);
    }

    @SuppressLint("SimpleDateFormat")
    public static long convertDateTime(String date, String format) {
        try {
            if (format == null) {
                format = DEFAULT_FORMAT_DATE;
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

            return simpleDateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @SuppressLint("SimpleDateFormat")
    public static String convertDateTime(long date) {
        return convertDateTime(date, null);
    }

    @SuppressLint("SimpleDateFormat")
    public static String convertDateTime(long date, String format) {
        try {
            if (format == null) {
                format = DEFAULT_FORMAT_DATE;
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

            return simpleDateFormat.format(new Date(date));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. formatDisplay(int second)
  2. formatDateAndTime(int timestamp, String pattern)
  3. formatDate(String mounthStr)
  4. dateTimeFormat(String dateTime)
  5. dateTimeFormat(String dateTime)
  6. formatTime(long time)
  7. convertDateTime(String date, String format)
  8. convertDateTime(long date, String format)
  9. getDateFromFormattedTimeStamp( String formattedTimeStamp)