Android Date String Parse parseDateStr(String dateStr, String srcPattern, String destPattern)

Here you can find the source of parseDateStr(String dateStr, String srcPattern, String destPattern)

Description

parse Date Str

Declaration

public static String parseDateStr(String dateStr, String srcPattern,
            String destPattern) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static String parseDateStr(String dateStr, String srcPattern,
            String destPattern) {
        if (isEmpty(dateStr)) {
            return null;
        }//from  w  ww  . j  av a 2 s .c  o m
        if (isEmpty(srcPattern)) {
            srcPattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (isEmpty(destPattern)) {
            destPattern = "yyyy-MM-dd";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat(srcPattern,
                Locale.getDefault());
        Date date = null;
        try {
            date = parseFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(destPattern,
                Locale.getDefault());
        return dateFormat.format(date);
    }

    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }
}

Related

  1. parse(String pattern, String date)
  2. parse3339(String time)
  3. parseDate(String date)
  4. parseDate(String date)
  5. parseDate(String rawDate)
  6. parseDateString(String dateStr, String pattern)
  7. parseDateString(String dateStr, String pattern, Locale locale)
  8. parseSimpleDate(String dateString)
  9. parseUTCFormattedDateString(String dateString)