//package com.projectsexception.festivales.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class DateFormater {
private static final String feedDatePattern = "dd/MM/yyyy HH:mm";
private static final String showDatePattern = "EEE, dd";
private static String[] patterns = {
"[A-Z][a-z]{2}, [0-9][0-9] [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}",
"[A-Z][a-z]{2}, [0-9] [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}",
"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}",
"[0-9]{4}-[0-9]{2}-[0-9]{2}",
"[0-9]{2}-[0-9]{2}-[0-9]{4}"
};
private static String[] datePatterns = {
"EEE, dd MMM yyyy HH:mm",
"EEE, d MMM yyyy HH:mm",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd",
"dd-MM-yyyy"
};
private static Map mapPatterns;
static {
mapPatterns = new HashMap();
for (int i = 0; i < patterns.length; i++) {
mapPatterns.put(patterns[i], datePatterns[i]);
}
}
public static Date parseDate(String dateString) {
Pattern pattern;
Matcher matcher;
SimpleDateFormat entrada = new SimpleDateFormat("", new Locale("en"));
for (String patternString : mapPatterns.keySet()) {
pattern = Pattern.compile(patternString);
matcher = pattern.matcher(dateString);
if (matcher.find()) {
entrada.applyPattern(mapPatterns.get(patternString));
try {
return entrada.parse(matcher.group());
} catch (ParseException e) {
}
}
}
return null;
}
public static Date parseFeedDate(String dateString) {
return parseDate(dateString, feedDatePattern);
}
public static String feedDate(String dateString) {
Date date = parseDate(dateString);
if (date != null) {
return new SimpleDateFormat(feedDatePattern).format(date);
} else {
return dateString;
}
}
public static String formatShowDate(Date showDate) {
SimpleDateFormat formatter = new SimpleDateFormat(showDatePattern);
return formatter.format(showDate);
}
private static Date parseDate(String dateString, String datePattern) {
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
try {
return formatter.parse(dateString);
} catch (ParseException e) {
//
}
return null;
}
}