Android Time Format dateTimeFormat(String dateTime)

Here you can find the source of dateTimeFormat(String dateTime)

Description

date Time Format

Declaration

public static String dateTimeFormat(String dateTime) 

Method Source Code

//package com.java2s;

public class Main {

    public static String dateTimeFormat(String dateTime) {
        StringBuilder sb = new StringBuilder();
        try {//from w w w  .  j  ava2s . c o  m
            if (isEmpty(dateTime)) {
                return null;
            }
            String[] dateAndTime = dateTime.split(" ");
            if (dateAndTime.length > 0) {
                for (String str : dateAndTime) {
                    if (str.indexOf("-") != -1) {
                        String[] date = str.split("-");
                        for (int i = 0; i < date.length; i++) {
                            String str1 = date[i];
                            sb.append(strFormat2(str1));
                            if (i < date.length - 1) {
                                sb.append("-");
                            }
                        }
                    } else if (str.indexOf(":") != -1) {
                        sb.append(" ");
                        String[] date = str.split(":");
                        for (int i = 0; i < date.length; i++) {
                            String str1 = date[i];
                            sb.append(strFormat2(str1));
                            if (i < date.length - 1) {
                                sb.append(":");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return sb.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static String strFormat2(String str) {
        try {
            if (str.length() <= 1) {
                str = "0" + str;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}

Related

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