package jp.esoro.common.utils;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
* 日付処理クラス
* */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
/**
* 日付表示パターン(YYYY/MM/DD hh:mm)
*/
public static final String FORMAT_YYYY_MM_DD_HH_MM = "yyyy/MM/dd HH:mm";
public static final String FORMAT_YYYY_MM_DD = "yyyy/MM/dd";
public static final String FORMAT_YYYYMMDD = "yyyyMMdd";
public static final String FORMAT_YYMMDD = "yyMMdd";
public static final String FORMAT_JPN_YYYY_MM_DD = "yyyy年MM月dd日";
public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";
public static final String FORMAT_MYSQL_DATE = "yyyy-MM-dd";
public static final String FORMAT_MYSQL_DATETIME = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String FORMAT_YYYYMMDDHHMMSSmi = "yyyyMMddHHmmssSSS";
public static final String FORMAT_YYMMDDHHMMSS = "yyMMddHHmmss";
public static final String FORMAT_YYMMDDHHMMSSmi = "yyMMddHHmmssSSS";
/**
* 日付(Calendar)を文字列に編集する
* @param date
* @param pattern
* @return
*/
public static String format(Calendar date, String pattern) {
if (date == null) {
return null;
}
return DateFormatUtils.format(date, pattern);
}
/**
* 日付(Date)を文字列に編集する
* @param date
* @param pattern
* @return
*/
public static String format(Date date, String pattern) {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return format(cal, pattern);
}
/**
* システム日付を取得する
* @return Date
*/
public static Date getDateTime() {
return Calendar.getInstance().getTime();
}
/**
* 文字列をDate型へ変換する
* @param value(yyyy_mm_dd)
* @return
*/
public static Date stringToDate(String value) {
return stringToDate(value , FORMAT_YYYY_MM_DD);
}
/**
* 文字列をDate型へ変換する
* @param value
* @param pattern
* @return
*/
public static Date stringToDate(String value ,String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (value == null) {
return null;
}
try {
return sdf.parse(value);
} catch (ParseException e) {
return null;
}
}
//ここまで以前から、以降を追加
/**
* 日時文字列をDate変換する
* @param str Date変換したい文字列
* @return Date 取得出来ない場合はnull
* @see あらかじめ文字列パターンが判明している場合は、stringToDateを使用してください)
*/
public static Date extractDate(String str) {
final String[] regexs = {"([0-9]{2,4})(/|-|¥¥.|年)([01]?[0-9])(/|-|¥¥.|月)([0123]?[0-9])(日|日¥¥s+|¥¥s|_)([0-9]{1,2})(:|-|¥¥.|時)([0-9]{1,2})(:|-|¥¥.|分)([0-9]{1,2})",
"([0-9]{2,4})(/|-|¥¥.|年)([01]?[0-9])(/|-|¥¥.|月)([0123]?[0-9])(日|日¥¥s+|¥¥s|_)([0-9]{1,2})(:|-|¥¥.|時)([0-9]{1,2})",
"([0-9]{2,4})(/|-|¥¥.|年)([01]?[0-9])(/|-|¥¥.|月)([0123]?[0-9])(日|日¥¥s+|¥¥s|_)([0-9]{1,2})(:|-|¥¥.|時)",
"([0-9]{2,4})(/|-|¥¥.|年)([01]?[0-9])(/|-|¥¥.|月)([0123]?[0-9])",
};
Date ret = null;
String normStr = Normalizer.normalize(str, Form.NFKC);
try{
String chkStr = StringUtils.removePattern(normStr, "am|AM|Am|pm|PM|Pm|午前|午後");
for(String regex : regexs){
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL|Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(chkStr);
if(matcher.find()){
if(matcher.groupCount() >= 5){
boolean isPm = false;
if(normStr.indexOf(matcher.group()) < 0 && matcher.groupCount() >= 7){
if( normStr.matches(".*"+matcher.group(1)+matcher.group(2)+matcher.group(3)+matcher.group(4)+matcher.group(5)+matcher.group(6)+"(pm|PM|pm|午後).*")){
isPm = true;
}
}
if(matcher.group(1).length() == 2){
ret = DateUtils.setYears(getDateTime(), Integer.valueOf("20" + matcher.group(1)));
}
else{
ret = DateUtils.setYears(getDateTime(), Integer.valueOf(matcher.group(1)));
}
ret = DateUtils.setDays(ret, 1);
//Month is 0 -11
ret = DateUtils.setMonths(ret, Integer.valueOf(matcher.group(3))-1);
ret = DateUtils.setDays(ret, Integer.valueOf(matcher.group(5)));
if(matcher.groupCount() >= 7){
int hour = Integer.valueOf(matcher.group(7));
ret = DateUtils.setHours(ret, hour + (hour<12 && isPm?12:0));
}
else{
ret = DateUtils.setHours(ret, 0);
}
if(matcher.groupCount() >= 9){
ret = DateUtils.setMinutes(ret, Integer.valueOf(matcher.group(9)));
}
else{
ret = DateUtils.setMinutes(ret, 0);
}
if(matcher.groupCount() == 11){
ret = DateUtils.setSeconds(ret, Integer.valueOf(matcher.group(11)));
}
else{
ret = DateUtils.setSeconds(ret, 0);
}
}
return ret;
}
}
if(ret == null){
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
ret = sdf1.parse(chkStr);
}
}catch(Exception e){
return null;
}
return ret;
}
}