DateEnums.class
public class DateEnums {
/**
* 日期风格枚举
*/
public enum DateStyle {
/**
* MM-dd 例如 01-01
*/
MM_DD("MM-dd"),
/**
* yyyy-MM 例如 2021-01
*/
yyyy_MM("yyyy-MM"),
/**
* yyyy-MM-dd 例如 2021-01-01
*/
yyyy_MM_dd("yyyy-MM-dd"),
/**
* MM-dd HH:mm 例如 03-13 15:16
*/
MM_dd_HH_mm("MM-dd HH:mm"),
/**
* MM-dd HH:mm:ss 例如 03-13 15:16:47
*/
MM_dd_HH_mm_ss("MM-dd HH:mm:ss"),
/**
* yyyy-MM-dd HH:mm 例如 2021-01-01 15
*/
yyyy_MM_dd_HH("yyyy-MM-dd HH"),
/**
* yyyy-MM-dd HH:mm 例如 2021-01-01 15:16
*/
yyyy_MM_dd_HH_mm("yyyy-MM-dd HH:mm"),
/**
* yyyy-MM-dd HH:mm:ss 例如 2021-01-01 15:16:47
*/
yyyy_MM_dd_HH_mm_ss("yyyy-MM-dd HH:mm:ss"),
/**
* yyyy-MM-dd E HH:mm:ss 例如 2021-01-01 星期一 15:16:47
*/
yyyy_MM_dd_E_HH_mm_ss("yyyy-MM-dd E HH:mm:ss"),
/**
* yyyy-MM-dd HH:mm:ss:S 例如 2021-01-01 15:16:47:356
*/
yyyy_MM_dd_HH_mm_ss_S("yyyy-MM-dd HH:mm:ss:S"),
/**
* yyyy-MM-dd E HH:mm:ss:S 例如 2021-01-01 星期五 15:16:47:356
*/
yyyy_MM_dd_E_HH_mm_ss_S("yyyy-MM-dd E HH:mm:ss:S"),
/**
* MM/dd 例如 01/01
*/
MM_dd_EN("MM/dd"),
/**
* yyyy/MM 例如 2021/03
*/
yyyy_MM_EN("yyyy/MM"),
/**
* yyyy/MM/dd 例如 2021/01/01
*/
yyyy_MM_dd_EN("yyyy/MM/dd"),
/**
* MM/dd HH:mm 例如 01/01 15:16
*/
MM_dd_HH_mm_EN("MM/dd HH:mm"),
/**
* MM/dd HH:mm:ss 例如 01/01 15:16:47
*/
MM_dd_HH_mm_ss_EN("MM/dd HH:mm:ss"),
/**
* yyyy/MM/dd HH:mm 例如 2021/01/01 15
*/
yyyy_MM_dd_HH_EN("yyyy/MM/dd HH"),
/**
* yyyy/MM/dd HH:mm 例如 2021/01/01 15:16
*/
yyyy_MM_dd_HH_mm_EN("yyyy/MM/dd HH:mm"),
/**
* yyyy/MM/dd HH:mm:ss 例如 2021/01/01 15:16:47
*/
yyyy_MM_dd_HH_mm_ss_EN("yyyy/MM/dd HH:mm:ss"),
/**
* yyyy/MM/dd E HH:mm:ss:S 例如 2021/01/01 星期五 15:16:47
*/
yyyy_MM_dd_E_HH_mm_ss_EN("yyyy/MM/dd E HH:mm:ss"),
/**
* yyyy/MM/dd HH:mm:ss:S 例如 2021/01/01 15:16:47:356
*/
yyyy_MM_dd_HH_mm_ss_S_EN("yyyy/MM/dd HH:mm:ss:S"),
/**
* yyyy/MM/dd E HH:mm:ss:S 例如 2021/01/01 星期五 15:16:47:356
*/
yyyy_MM_dd_E_HH_mm_ss_S_EN("yyyy/MM/dd E HH:mm:ss:S"),
/**
* MM月dd日 例如 01月01日
*/
MM_dd_CN("MM月dd日"),
/**
* yyyy年MM月 例如 2021年01月
*/
yyyy_MM_CN("yyyy年MM月"),
/**
* yyyy年MM月dd日 例如 2021年01月01日
*/
yyyy_MM_dd_CN("yyyy年MM月dd日"),
/**
* MM月dd日 HH:mm 例如 01月01日 13:32
*/
MM_dd_HH_mm_CN("MM月dd日 HH:mm"),
/**
* MM月dd日 HH:mm:ss 例如 01月01日 13:32:53
*/
MM_dd_HH_mm_ss_CN("MM月dd日 HH:mm:ss"),
/**
* yyyy年MM月dd日 HH:mm 例如 2021年01月01日 13时
*/
yyyy_MM_dd_HH_CN("yyyy年MM月dd日 HH时"),
/**
* yyyy年MM月dd日 HH:mm 例如 2021年01月01日 13:32
*/
yyyy_MM_dd_HH_mm_CN("yyyy年MM月dd日 HH:mm"),
/**
* yyyy年MM月dd日 HH:mm:ss:S 例如 2021年01月01日 13:32:53
*/
yyyy_MM_dd_HH_mm_ss_CN("yyyy年MM月dd日 HH:mm:ss"),
/**
* yyyy年MM月dd日 HH:mm:ss:S 例如 2021年01月01日 星期五 13:32:53
*/
yyyy_MM_dd_E_HH_mm_ss_CN("yyyy年MM月dd日 E HH:mm:ss"),
/**
* yyyy年MM月dd日 HH:mm:ss:S 例如 2021年01月01日 13:32:53:356
*/
yyyy_MM_dd_HH_mm_ss_S_CN("yyyy年MM月dd日 HH:mm:ss:S"),
/**
* yyyy年MM月dd日 HH:mm:ss:S 例如 2021年01月01日 星期五 13:32:53:356
*/
yyyy_MM_dd_E_HH_mm_ss_S_CN("yyyy年MM月dd日 E HH:mm:ss:S"),
/**
* yy 例如 01
*/
yy("yy"),
/**
* yyyy 例如 2021
*/
yyyy("yyyy"),
/**
* MM 例如 01
*/
MM("MM"),
/**
* dd 例如 01
*/
dd("dd"),
/**
* yyyyMM 例如 202101
*/
yyyyMM("yyyyMM"),
/**
* yyyyMMdd 例如 20210101
*/
yyyyMMdd("yyyyMMdd"),
/**
* yyyyMMddHH 例如 2021010113
*/
yyyyMMddHH("yyyyMMddHH"),
/**
* yyyyMMddHHmm 例如 202101011332
*/
yyyyMMddHHmm("yyyyMMddHHmm"),
/**
* yyyyMMddHHmmss 例如 20210101133253
*/
yyyyMMddHHmmss("yyyyMMddHHmmss"),
/**
* yyMMddHHmmss 例如 170101133253
*/
yyMMddHHmmss("yyMMddHHmmss"),
/**
* HH:mm 例如 32:45
*/
HH_mm("HH:mm"),
/**
* HH:mm:ss 例如 13:32:45
*/
HH_mm_ss("HH:mm:ss");
private String value;
DateStyle(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
/**
* 星期 枚举
*/
public enum Week {
/**
* 星期一
*/
MONDAY("星期一", "Monday", "Mon.", 1),
/**
* 星期二
*/
TUESDAY("星期二", "Tuesday", "Tues.", 2),
/**
* 星期三
*/
WEDNESDAY("星期三", "Wednesday", "Wed.", 3),
/**
* 星期四
*/
THURSDAY("星期四", "Thursday", "Thur.", 4),
/**
* 星期五
*/
FRIDAY("星期五", "Friday", "Fri.", 5),
/**
* 星期六
*/
SATURDAY("星期六", "Saturday", "Sat.", 6),
/**
* 星期日
*/
SUNDAY("星期日", "Sunday", "Sun.", 7);
String name_cn;
String name_en;
String name_enShort;
int number;
Week(String name_cn, String name_en, String name_enShort, int number) {
this.name_cn = name_cn;
this.name_en = name_en;
this.name_enShort = name_enShort;
this.number = number;
}
public String getChineseName() {
return name_cn;
}
public String getName() {
return name_en;
}
public String getShortName() {
return name_enShort;
}
public int getNumber() {
return number;
}
}
/**
* 日期类型,包括年、月、周、日、时、分
*/
public enum DateType {
/** 年 */
YEAR("YEAR"),
/** 月 */
MONTH("MONTH"),
/** 周 */
WEEK("WEEK"),
/** 日 */
DAY("DAY"),
/** 时 */
HOUR("HOUR"),
/** 分 */
MINUTE("MINUTE");
private String type;
DateType(String type){
this.type = type;
}
public String getType(){
return type;
}
}
}
DateUtil
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateUtil {
private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
/**
* 缺省的日期格式 yyyy-MM-dd
*/
private static final String DAFAULT_DATE_FORMAT = "yyyy-MM-dd";
/**
* 默认日期类型格试.
*
*/
private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
DAFAULT_DATE_FORMAT);
/**
* 缺省的日期时间格式 yyyy-MM-dd HH:mm:ss
*/
private static final String DAFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat(
DAFAULT_DATETIME_FORMAT);
/**
* 缺省的时间格式 HH:mm:ss
*/
private static final String DAFAULT_TIME_FORMAT = "HH:mm:ss";
private static final SimpleDateFormat timeFormat = new SimpleDateFormat(
DAFAULT_TIME_FORMAT);
private static final Map<String, String> chineseMap = new HashMap<String, String>();
private static final String yearReg="[一|二|三|四|五|六|七|八|九|十|〇|○]{4}年";
private static final String monthReg="(([十][一|二])|([一|二|三|四|五|六|七|八|九|十]))月";
private static final String dayReg="(([十][一|二|三|四|五|六|七|八|九])|(一|二|三|四|五|六|七|八|九|十)|([一|二|三][十][一|二|三|四|五|六|七|八|九]))日";
static{
chineseMap.put("一", "1");
chineseMap.put("元", "1");
chineseMap.put("二", "2");
chineseMap.put("三", "3");
chineseMap.put("四", "4");
chineseMap.put("五", "5");
chineseMap.put("六", "6");
chineseMap.put("七", "7");
chineseMap.put("八", "8");
chineseMap.put("九", "9");
chineseMap.put("〇", "0");
chineseMap.put("○", "0");
chineseMap.put("十", "10");
chineseMap.put("百", "100");
}
private DateUtil() {
// 私有类构造方法
}
private static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
private static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 获取某个时间点前N天的时间点。
*
* @param currentDate
* @param days
* @return
*/
public static Date getDateBefore(Date currentDate, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DAY_OF_YEAR, -days);
return cal.getTime();
}
/**
* 获取某个时间点后N天的时间点。
*
* @param currentDate
* @param days
* @return
*/
public static Date getDateAfter(Date currentDate, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DAY_OF_YEAR, days);
return cal.getTime();
}
/**
* 根据生日计算年龄。
*
* @param birthday
* @return int
*/
public static int getAgeByBirthDay(Date birthday) {
Calendar cal = Calendar.getInstance();
if (cal.before(birthday)) {
return 0;
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthday);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH) + 1;
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
// monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
}
} else {
// monthNow>monthBirth
age--;
}
}
return age;
}
/**
* 检查字符串是否是日期
*
* @param dateString
* 字符串
* @return 任意格式[DateStyle] 只要有一个转换成功就返回true,否则返回false
*/
public static boolean check(String dateString) {
if (isEmpty(dateString)) {
return false;
}
for (DateEnums.DateStyle style : DateEnums.DateStyle.values()) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(style.getValue());
try {
date = (Date) sdf.parse(dateString);
if (date != null && dateString.equals(sdf.format(date))) {
return true;
}
} catch (ParseException e) {
}
}
return false;
}
/**
* 返回两个时间之间的差(毫秒数)
*
* @param time1 日期一
* @param time2 日期二
* @return 毫秒数[绝对值]
*/
public static long dateDiffer(Date time1, Date time2) {
if (time1 == null || time2 == null) {
return 0;
}
long differ = Math.abs(time1.getTime() - time2.getTime());
return differ;
}
/**
* 返回两个时间之间的差(毫秒数)
*
* @param time1 日期一
* @param time2 日期二
* @param formatStr 格式字符串
* @return 毫秒数[绝对值]
*/
public static long dateDiffer(String time1, String time2, String formatStr) {
if (isEmpty(time1) || isEmpty(time2)) {
return 0L;
}
Date date1 = parse(time1, formatStr);
Date date2 = parse(time2, formatStr);
return dateDiffer(date1, date2);
}
/**
* 返回两个时间之间的差(毫秒数)
*
* @param time1 日期一
* @param time2 日期二
* @return 毫秒数[绝对值]
*/
public static long dateDiffer(String time1, String time2) {
return dateDiffer(time1, time2, "");
}
/**
* 计算日期差
* @param time1
* 日期一
* @param time2
* 日期二
* @return 日期差[绝对值]
*/
public static int daysBetween(Date time1, Date time2) {
if (time1 == null || time2 == null) {
return 0;
}
Calendar cDate1 = Calendar.getInstance();
Calendar cDate2 = Calendar.getInstance();
cDate1.setTime(time1);
cDate2.setTime(time2);
setTimeToMidnight(cDate1);
setTimeToMidnight(cDate2);
long todayMs = cDate1.getTimeInMillis();
long returnMs = cDate2.getTimeInMillis();
long intervalMs = returnMs - todayMs;
return Math.abs(millisecondsToDays(intervalMs));
}
/**
* 计算日期差
* @param time1
* 日期一
* @param time2
* 日期二
* @param formatStr
* 格式字符串
* @return 日期差[绝对值]
*/
public static int daysBetween(String time1, String time2, String formatStr) {
if (isEmpty(time1) || isEmpty(time2)) {
return 0;
}
Date date1 = parse(time1, formatStr);
Date date2 = parse(time2, formatStr);
return daysBetween(date1, date2);
}
/**
* 计算日期差
* @param time1
* 日期一
* @param time2
* 日期二
* @return 日期差[绝对值]
*/
public static int daysBetween(String time1, String time2) {
return daysBetween(time1, time2, "");
}
private static int millisecondsToDays(long intervalMs) {
return (int) (intervalMs / (1000 * 86400));
}
private static void setTimeToMidnight(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}
/**
* 计算月份差
* @param time1
* 日期一
* @param time2
* 日期二
* @return 月份差[绝对值]
*/
public static int monthsBetween(Date time1, Date time2) {
if (time1 == null || time2 == null) {
return 0;
}
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
bef.setTime(time1);
aft.setTime(time2);
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
return Math.abs(month + result);
}
/**
* 计算月份差
* @param time1
* 日期一
* @param time2
* 日期二
* @param formatStr
* 格式字符串
* @return 月份差[绝对值]
*/
public static int monthsBetween(String time1, String time2, String formatStr) {
if (isEmpty(time1) || isEmpty(time2)) {
return 0;
}
Date date1 = parse(time1, formatStr);
Date date2 = parse(time2, formatStr);
return monthsBetween(date1, date2);
}
/**
* 计算月份差
* @param time1
* 日期一
* @param time2
* 日期二
* @return 月份差[绝对值]
*/
public static int monthsBetween(String time1, String time2) {
return monthsBetween(time1, time2, "");
}
/**
* 时间戳转化为时间格式
*
* @param timeStamp
* 时间戳
* @return yyyy-MM-dd HH:mm:ss
*/
public static String timeStampToDateTime(long timeStamp) {
String date = datetimeFormat.format(timeStamp);
return date;
}
/**
* 时间戳转化为时间格式
*
* @param timeStamp
* 时间戳
* @return yyyy-MM-dd
*/
public static String timeStampToDate(long timeStamp) {
String date = dateFormat.format(timeStamp * 1000);
return date;
}
/**
* 时间戳转化为时间格式
*
* @param timeStamp
* 时间戳
* @return HH:mm:ss
*/
public static String timeStampToTime(long timeStamp) {
String time = null;
String date = timeFormat.format(timeStamp * 1000);
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
}
/**
* 将一个时间戳转换成提示性时间字符串,如刚刚,1秒前
*
* @param timeStamp
* @return
*/
public static String convertTimeToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
if (time < 60 && time >= 0) {
return "刚刚";
} else if (time >= 60 && time < 3600) {
return time / 60 + "分钟前";
} else if (time >= 3600 && time < 3600 * 24) {
return time / 3600 + "小时前";
} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
return time / 3600 / 24 + "天前";
} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 + "个月前";
} else if (time >= 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 / 12 + "年前";
} else {
return "刚刚";
}
}
/**
* 将一个时间戳转换成提示性时间字符串
*
* @param timeStamp
* @return
*/
public static String convertTimeToFormat2(long timeStamp) {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = timeStamp / dd;
Long hour = (timeStamp - day * dd) / hh;
Long minute = (timeStamp - day * dd - hour * hh) / mi;
Long second = (timeStamp - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = timeStamp - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if(day > 0) {
sb.append(day+"天");
}
if(hour > 0) {
sb.append(hour+"小时");
}
if(minute > 0) {
sb.append(minute+"分");
}
if(second > 0) {
sb.append(second+"秒");
}
if(milliSecond > 0) {
sb.append(milliSecond+"毫秒");
}
return sb.toString();
}
/**
* 将一个时间戳转换成提示性时间字符串,(多少分钟)
*
* @param timeStamp
* @return
*/
public static String timeStampToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
return time / 60 + "";
}
/**
* 日期/时间 计算
*
* @param time 待计算时间
* @param addpart 可选 Y M D H F S
* @param num 增加或者减少量(整数)
* @return yyyy-MM-dd HH:mm:ss
* @throws Exception
*/
public static String calculateTime(String time, String addpart, int num) throws ParseException {
return calculateTime(parseTime(time,DAFAULT_DATETIME_FORMAT),addpart,num);
}
/**
* 日期/时间 计算
*
* @param time 待计算时间
* @param addpart 可选 Y M D H F S
* @param num 增加或者减少量(整数)
* @return yyyy-MM-dd HH:mm:ss
* @throws Exception
*/
public static String calculateTime(Date time, String addpart, int num) throws ParseException {
DateFormat yyyyMMddHHmmssDateFormat = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(time);
if (addpart.equalsIgnoreCase("Y")) {
cal.add(Calendar.YEAR, num);
} else if (addpart.equalsIgnoreCase("M")) {
cal.add(Calendar.MONTH, num);
} else if (addpart.equalsIgnoreCase("D")) {
cal.add(Calendar.DATE, num);
} else if (addpart.equalsIgnoreCase("H")) {
cal.add(Calendar.HOUR_OF_DAY, num);
} else if (addpart.equalsIgnoreCase("F")) {
cal.add(Calendar.MINUTE, num);
} else if (addpart.equalsIgnoreCase("S")) {
cal.add(Calendar.SECOND, num);
}
return yyyyMMddHHmmssDateFormat.format(cal.getTime());
}
/**
* 格式化Calendar
*
* @param calendar
* @return
*/
public static String formatCalendar(Calendar calendar) {
if (calendar == null) {
return "";
}
return dateFormat.format(calendar.getTime());
}
/**
* 格式化日期时间,默认日期格式yyyy-MM-dd HH:mm:ss
* @param datetime
* @return
*/
public static String formatDateTime(Date datetime) {
if (datetime == null) {
return "";
}
return datetimeFormat.format(datetime);
}
/**
* 日期格式化,默认日期格式yyyy-MM-dd
* @param date
* @return
*/
public static String formatDate(Date date) {
if (date == null) {
return "";
}
return dateFormat.format(date);
}
/**
* 格式化时间,默认日期格式HH:mm:ss
*
* @param time
* @return
*/
public static String formatTime(Date time) {
if (time == null) {
return "";
}
return timeFormat.format(time);
}
/**
* 格式化整数型日期
*
* @param intDate
* @return
*/
public static String formatIntDate(Integer intDate) {
if (intDate == null) {
return "";
}
Calendar c = newCalendar(intDate);
return formatCalendar(c);
}
/**
* 根据指定格式化来格式日期.
*
* @param date
* 待格式化的日期.
* @param style
* 日期风格
* @return 字符串型日期.
*/
public static String format(Date date, DateEnums.DateStyle style) {
return format(date, style.getValue());
}
/**
* 根据指定格式化来格式日期.
*
* @param date
* 待格式化的日期.
* @param pattern
* 格式化样式或分格,如yyMMddHHmmss
* @return 字符串型日期.
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "";
}
if (isEmpty(pattern)) {
return formatDate(date);
}
SimpleDateFormat simpleDateFormat = null;
try {
simpleDateFormat = new SimpleDateFormat(pattern);
} catch (Exception e) {
logger.error(e.toString(), e);
return formatDate(date);
}
return simpleDateFormat.format(date);
}
/**
* 将日期字符串转化为另一日期字符串。失败返回null。
*
* @param date
* 旧日期字符串
* @param pattern
* 新日期格式
* @return 新日期字符串
*/
public static String changeFormat(String date, String pattern) {
return changeFormat(date, null, pattern);
}
/**
* 将日期字符串转化为另一日期字符串。失败返回null。
*
* @param date
* 旧日期字符串
* @param olddPattern
* 旧日期格式
* @param newPattern
* 新日期格式
* @return 新日期字符串
*/
public static String changeFormat(String date, String olddPattern,
String newPattern) {
String dateString = null;
Date myDate = parse(date, olddPattern);
dateString = format(myDate, newPattern);
return dateString;
}
/**
* 将日期字符串转化为另一日期字符串。失败返回null。
*
* @param date
* 旧日期字符串
* @param olddDteStyle
* 旧日期风格
* @param newDateStyle
* 新日期风格
* @return 新日期字符串
*/
public static String changeFormat(String date, DateEnums.DateStyle olddDteStyle,
DateEnums.DateStyle newDateStyle) {
String dateString = null;
dateString = changeFormat(date, olddDteStyle.getValue(),
newDateStyle.getValue());
return dateString;
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @return yyyyMMdd
*/
public static Date parseDateChar(String dateString) {
return parse(dateString, "yyyyMMdd");
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @return yyyy-MM-dd
*/
public static Date parseDate(String dateString) {
return parse(dateString, "yyyy-MM-dd");
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date parseDateTime(String dateString) {
return parse(dateString, "yyyy-MM-dd HH:mm:ss");
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @return 任意格式[DateStyle] 只要有一个转换成功就返回转换结果,否则返回null
*/
public static Date parse(String dateString) {
for (DateEnums.DateStyle style : DateEnums.DateStyle.values()) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(style.getValue());
try {
date = (Date) sdf.parse(dateString);
if (date != null && dateString.equals(sdf.format(date))) {
return new Date(date.getTime());
}
} catch (ParseException e) {
}
}
return null;
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @param pattern
* 格式
* @return
*/
public static Date parse(String dateString, String pattern) {
if (isEmpty(dateString)) {
return null;
}
if (isEmpty(pattern)) {
return parse(dateString);
}
Date date = null;
SimpleDateFormat sdf = getFormatInstance(pattern);
try {
date = (Date) sdf.parse(dateString);
if (date == null || !dateString.equals(sdf.format(date))) {
logger.error("日期格式错误");
return null;
}
} catch (ParseException e) {
logger.error(e.toString(), e);
}
return new Date(date.getTime());
}
/**
* 把字符串转换为日期
* @param dateString
* @return
*/
public static Date parseCST(String dateString) {
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date = null;
try {
date = (Date) sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 把字符串转换为日期
*
* @param dateString
* 字符串
* @param style
* 格式
* @return
*/
public static Date parse(String dateString, DateEnums.DateStyle style) {
return parse(dateString, style.getValue());
}
/**
* 把字符串转换为时间
*
* @param dateString
* 字符串
* @return yyyy-MM-dd HH:mm:ss
*/
public static Timestamp parseTime(String dateString) {
return parseTime(dateString, DAFAULT_DATETIME_FORMAT);
}
/**
* 把字符串转换为时间
*
* @param dateString
* 字符串
* @param pattern
* 格式
* @return
*/
public static Timestamp parseTime(String dateString, String pattern) {
if ("".equals(dateString)) {
return null;
}
if (pattern == null) {
pattern = DAFAULT_DATETIME_FORMAT;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = (Date) simpleDateFormat.parse(dateString);
if (date == null
|| !dateString.equals(simpleDateFormat.format(date))) {
logger.error("日期时间格式错误");
return null;
}
} catch (ParseException e) {
logger.error(e.toString(), e);
}
return new Timestamp(date.getTime());
}
/**
* 当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。
* @return
*/
public static long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 系统计时器的当前值,以毫微秒为单位。
* @return
*/
public static long getNanoTime() {
return System.nanoTime();
}
/**
* 返回unix时间戳 (1970年至今的秒数)
* @return
*/
public static long getUnixStamp() {
return System.currentTimeMillis() / 1000;
}
/**
* 得到昨天的日期,时间格式yyyy-MM-dd
* @return
*/
public static String getYestoryDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
String yestoday = dateFormat.format(calendar.getTime());
return yestoday;
}
/**
* 得到明天的日期,时间格式yyyy-MM-dd
* @return
*/
public static String getTomorrowDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
String yestoday = dateFormat.format(calendar.getTime());
return yestoday;
}
/**
* 得到当前的时间,时间格式yyyy-MM-dd
* @return
*/
public String getCurrentDate() {
return dateFormat.format(new Date());
}
/**
* 得到当前的时间,自定义时间格式
* @param pattern
* 输出显示的时间格式
* @return
*/
public String getCurrentDate(String pattern) {
SimpleDateFormat sdf = getFormatInstance(pattern);
return sdf.format(new Date());
}
/**
* 取得Date型的当前日期
* @return
*/
public static Date getNow() {
return new Date();
}
/**
* 取得Integer型的当前日期
* @return
*/
public static Integer getIntNow() {
return getIntDate(getNow());
}
/**
* 取得Integer型的当前年份
* @return
*/
public static Integer getIntYearNow() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
return year;
}
/**
* 取得Integer型的当前月份
* @return
*/
public static Integer getIntMonthNow() {
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
return month;
}
/**
* 取得Integer型的指定时间的年份
* @return
*/
public static Integer getIntYearByDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int year = c.get(Calendar.YEAR);
return year;
}
/**
* 取得Integer型的指定时间的月份
* @return
*/
public static Integer getIntMonthByDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH) + 1;
return month;
}
public static String getStringToday() {
return getIntDate(getNow()) + "";
}
/**
* 根据年月日获取整型日期
*
* @param year
* @param month
* @param day
* @return
*/
public static Integer getIntDate(int year, int month, int day) {
return getIntDate(newCalendar(year, month, day));
}
/**
* 获取某年第一天
*
* @param year
* @return
*/
public static Date getFirstDateByYear(Integer year) {
if (year == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.set(year, 0, 1, 0, 0, 0);
return c.getTime();
}
/**
* 获取某年第一天
*
* @param date
* @return
*/
public static Date getFirstDateByYear(Date date) {
return getFirstDateByYear(getYear(date));
}
/**
* 获取今年第一天
*
* @return
*/
public static Date getFirstDateByYear() {
return getFirstDateByYear(getNow());
}
/**
* 获取某年最后一天
*
* @param date
* @return 年份的最后一天
*/
public static Date getLastDateByYear(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.YEAR, now.get(Calendar.YEAR) + 1);
now.set(Calendar.MONTH, 0);
now.set(Calendar.DATE, 0);
now.set(Calendar.HOUR_OF_DAY, 23);
now.set(Calendar.MINUTE, 59);
now.set(Calendar.SECOND, 59);
return now.getTime();
}
/**
* 获取今年最后一天
*
* @return 年份的最后一天
*/
public static Date getLastDateByYear() {
return getLastDateByYear(getNow());
}
/**
* 获取某年最后一天
*
* @param year
* @return 年份的最后一天
*/
public static Date getLastDateByYear(int year) {
return getLastDateByYear(getDate(year));
}
/**
* 某年月的第一天
*
* @param year
* @param month
* @return
*/
public static Integer getFirstDateByMonth(int year, int month) {
return getIntDate(newCalendar(year, month, 1));
}
/**
* 本月第一天
*
* @return
*/
public static Integer getFirstDateByMonth() {
Integer year = getIntYearNow();
Integer month = getIntMonthNow();
return getIntDate(newCalendar(year, month, 1));
}
/**
* 获得所在月份的第一天
*
* @param date 当前月份所在的时间
* @return 月份的第一天
*/
public static Date getFirstDateByMonth(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, 0);
now.set(Calendar.HOUR_OF_DAY, 24);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
return now.getTime();
}
/**
* 获得所在月份的最后一天
*
* @param date 当前月份所在的时间
* @return 月份的最后一天
*/
public static Date getLastDateByMonth(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);
now.set(Calendar.DATE, 1);
now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
now.set(Calendar.HOUR_OF_DAY, 23);
now.set(Calendar.MINUTE, 59);
now.set(Calendar.SECOND, 59);
return now.getTime();
}
/**
* 本月最后一天
*
* @return
*/
public static Integer getLastDateByMonth() {
Integer year = getIntYearNow();
Integer month = getIntMonthNow();
return intDateSub(getIntDate(newCalendar(year, month + 1, 1)), 1);
}
/**
* 某年月的最后一天
*
* @param year
* @param month
* @return
*/
public static Integer getLastDateByMonth(int year, int month) {
return intDateSub(getIntDate(newCalendar(year, month + 1, 1)), 1);
}
/**
* 获得所在星期的第一天,(星期一为一周开始)
*/
public static Date getFirstDateByWeek(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
now.set(now.DATE, first_day_of_week);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
return now.getTime();
}
/**
* 获得所在星期的最后一天,(星期一为一周开始)
*/
public static Date getLastDateByWeek(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
int last_day_of_week = first_day_of_week + 6; // 星期日
now.set(now.DATE, last_day_of_week);
now.set(Calendar.HOUR_OF_DAY, 23);
now.set(Calendar.MINUTE, 59);
now.set(Calendar.SECOND, 59);
return now.getTime();
}
/**
* 获得所在星期的第一天,(星期日为一周开始)
*/
public static Date getFirstDateByWeekBySunDay(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 1 - today; // 星期日
now.set(now.DATE, first_day_of_week);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
return now.getTime();
}
/**
* 获得所在星期的最后一天,(星期日为一周开始)
*/
public static Date getLastDateByWeekBySunDay(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 1 - today; // 星期日
int last_day_of_week = first_day_of_week + 6; // 星期六
now.set(now.DATE, last_day_of_week);
now.set(Calendar.HOUR_OF_DAY, 23);
now.set(Calendar.MINUTE, 59);
now.set(Calendar.SECOND, 59);
return now.getTime();
}
/**
* 获取上个月
* @return
*/
public static String getLastMonth(){
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
String time = format.format(c.getTime());
return time;
}
/**
* 返回一天的开始时间
*
* @param day
* @return
*/
public static Date getDayFrom(Date day) {
Calendar cal = Calendar.getInstance();
cal.setTime(day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}
/**
* 返回一天的结束时间
*
* @param day
* @return
*/
public static Date getDayEnd(Date day) {
Calendar cal = Calendar.getInstance();
cal.setTime(day);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
/**
* 根据Calendar获取整型年份
*
* @param c
* @return
*/
public static Integer getIntYear(Calendar c) {
int year = c.get(Calendar.YEAR);
return year;
}
/**
* 根据Calendar获取整型日期
*
* @param c
* @return
*/
public static Integer getIntDate(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
return year * 10000 + month * 100 + day;
}
/**
* 根据Date获取整型年份
*
* @param d
* @return
*/
public static Integer getIntYear(Date d) {
if (d == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return getIntYear(c);
}
/**
* 根据Date获取整型日期
*
* @param d
* @return
*/
public static Integer getIntDate(Date d) {
if (d == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return getIntDate(c);
}
/**
* 根据Integer获取Date日期
*
* @param n
* @return
*/
public static Date getDate(Integer n) {
if (n == null) {
return null;
}
return parse(String.valueOf(n));
}
/**
* 根据年月日生成Calendar
*
* @param year
* @param month
* @param day
* @return
*/
public static Calendar newCalendar(int year, int month, int day) {
Calendar ret = Calendar.getInstance();
if (year < 100) {
year = 2000 + year;
}
ret.set(year, month - 1, day);
return ret;
}
/**
* 根据整型日期生成Calendar
*
* @param date
* @return
*/
public static Calendar newCalendar(int date) {
int year = date / 10000;
int month = (date % 10000) / 100;
int day = date % 100;
Calendar ret = Calendar.getInstance();
ret.set(year, month - 1, day);
return ret;
}
/**
* 整数型日期的加法
*
* @param date
* @param days
* @return
*/
public static Integer intDateAdd(int date, int days) {
int year = date / 10000;
int month = (date % 10000) / 100;
int day = date % 100;
day += days;
return getIntDate(year, month, day);
}
/**
* 整数型日期的减法
*
* @param date
* @param days
* @return
*/
public static Integer intDateSub(int date, int days) {
return intDateAdd(date, -days);
}
/**
* 获取SimpleDateFormat实例.
*
* @param pattern
* 日期格式 如果为空使用DAFAULT_DATE_FORMAT
* @return
*/
public static SimpleDateFormat getFormatInstance(String pattern) {
if (isEmpty(pattern)) {
pattern = DAFAULT_DATE_FORMAT;
}
return new SimpleDateFormat(pattern);
}
/**
* 获取日期字符串的日期风格。失敗返回null。
*
* @param date
* 日期字符串
* @return 日期风格
*/
public static DateEnums.DateStyle getDateStyle(String date) {
if (isEmpty(date)) {
return null;
}
for (DateEnums.DateStyle style : DateEnums.DateStyle.values()) {
Date dateTmp = null;
SimpleDateFormat sdf = new SimpleDateFormat(style.getValue());
try {
dateTmp = (Date) sdf.parse(date);
if (dateTmp != null && date.equals(sdf.format(dateTmp))) {
return style;
}
} catch (ParseException e) {
}
}
return null;
}
/**
* 获取日期中的某数值。如获取月份
*
* @param date
* 日期
* @param dateType
* 日期格式
* @return 数值
*/
private static int getInteger(Date date, int dateType) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(dateType);
}
/**
* 增加日期中某类型的某数值。如增加日期
*
* @param date
* 日期字符串
* @param dateType
* 类型
* @param amount
* 数值
* @return 计算后日期字符串
*/
private static String addInteger(String date, int dateType, int amount) {
String dateString = null;
DateEnums.DateStyle dateStyle = getDateStyle(date);
if (dateStyle != null) {
Date myDate = parse(date, dateStyle);
myDate = addInteger(myDate, dateType, amount);
dateString = format(myDate, dateStyle);
}
return dateString;
}
/**
* 增加日期中某类型的某数值。如增加日期
*
* @param date
* 日期
* @param dateType
* 类型
* @param amount
* 数值
* @return 计算后日期
*/
private static Date addInteger(Date date, int dateType, int amount) {
Date myDate = null;
if (date != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dateType, amount);
myDate = calendar.getTime();
}
return myDate;
}
/**
* 判断字符串是否为日期字符串
*
* @param date
* 日期字符串
* @return true or false
*/
public static boolean isDate(String date) {
boolean isDate = false;
if (date != null) {
if (parse(date) != null) {
isDate = true;
}
}
return isDate;
}
/**
* 增加日期的年份。失败返回null。
*
* @param date
* 日期
* @param yearAmount
* 增加数量。可为负数
* @return 增加年份后的日期字符串
*/
public static String addYear(String date, int yearAmount) {
return addInteger(date, Calendar.YEAR, yearAmount);
}
/**
* 增加日期的年份。失败返回null。
*
* @param date
* 日期
* @param yearAmount
* 增加数量。可为负数
* @return 增加年份后的日期
*/
public static Date addYear(Date date, int yearAmount) {
return addInteger(date, Calendar.YEAR, yearAmount);
}
/**
* 增加日期的月份。失败返回null。
*
* @param date
* 日期
* @param yearAmount
* 增加数量。可为负数
* @return 增加月份后的日期字符串
*/
public static String addMonth(String date, int yearAmount) {
return addInteger(date, Calendar.MONTH, yearAmount);
}
/**
* 增加日期的月份。失败返回null。
*
* @param date
* 日期
* @param yearAmount
* 增加数量。可为负数
* @return 增加月份后的日期
*/
public static Date addMonth(Date date, int yearAmount) {
return addInteger(date, Calendar.MONTH, yearAmount);
}
/**
* 增加日期的天数。失败返回null。
*
* @param date
* 日期字符串
* @param dayAmount
* 增加数量。可为负数
* @return 增加天数后的日期字符串
*/
public static String addDay(String date, int dayAmount) {
return addInteger(date, Calendar.DATE, dayAmount);
}
/**
* 增加日期的天数。失败返回null。
*
* @param date
* 日期
* @param dayAmount
* 增加数量。可为负数
* @return 增加天数后的日期
*/
public static Date addDay(Date date, int dayAmount) {
return addInteger(date, Calendar.DATE, dayAmount);
}
/**
* 增加日期的小时。失败返回null。
*
* @param date
* 日期字符串
* @param hourAmount
* 增加数量。可为负数
* @return 增加小时后的日期字符串
*/
public static String addHour(String date, int hourAmount) {
return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
}
/**
* 增加日期的小时。失败返回null。
*
* @param date
* 日期
* @param hourAmount
* 增加数量。可为负数
* @return 增加小时后的日期
*/
public static Date addHour(Date date, int hourAmount) {
return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
}
/**
* 增加日期的分钟。失败返回null。
*
* @param date
* 日期字符串
* @param hourAmount
* 增加数量。可为负数
* @return 增加分钟后的日期字符串
*/
public static String addMinute(String date, int hourAmount) {
return addInteger(date, Calendar.MINUTE, hourAmount);
}
/**
* 增加日期的分钟。失败返回null。
*
* @param date
* 日期
* @param hourAmount
* 增加数量。可为负数
* @return 增加分钟后的日期
*/
public static Date addMinute(Date date, int hourAmount) {
return addInteger(date, Calendar.MINUTE, hourAmount);
}
/**
* 增加日期的秒钟。失败返回null。
*
* @param date
* 日期字符串
* @param hourAmount
* 增加数量。可为负数
* @return 增加秒钟后的日期字符串
*/
public static String addSecond(String date, int hourAmount) {
return addInteger(date, Calendar.SECOND, hourAmount);
}
/**
* 增加日期的秒钟。失败返回null。
*
* @param date
* 日期
* @param hourAmount
* 增加数量。可为负数
* @return 增加秒钟后的日期
*/
public static Date addSecond(Date date, int hourAmount) {
return addInteger(date, Calendar.SECOND, hourAmount);
}
/**
* 获取日期的年份。失败返回0。
*
* @param date
* 日期字符串
* @return 年份
*/
public static int getYear(String date) {
return getYear(parse(date));
}
/**
* 获取日期的年份。失败返回0。
*
* @param date
* 日期
* @return 年份
*/
public static int getYear(Date date) {
return getInteger(date, Calendar.YEAR);
}
/**
* 获取日期的月份。失败返回0。
*
* @param date
* 日期字符串
* @return 月份
*/
public static int getMonth(String date) {
return getMonth(parse(date));
}
/**
* 获取日期的月份。失败返回0。
*
* @param date
* 日期
* @return 月份
*/
public static int getMonth(Date date) {
return getInteger(date, Calendar.MONTH);
}
/**
* 获取日期的天数。失败返回0。
*
* @param date
* 日期字符串
* @return 天
*/
public static int getDay(String date) {
return getDay(parse(date));
}
/**
* 获取日期的天数。失败返回0。
*
* @param date
* 日期
* @return 天
*/
public static int getDay(Date date) {
return getInteger(date, Calendar.DATE);
}
/**
* 获取日期的小时。失败返回0。
*
* @param date
* 日期字符串
* @return 小时
*/
public static int getHour(String date) {
return getHour(parse(date));
}
/**
* 获取日期的小时。失败返回0。
*
* @param date
* 日期
* @return 小时
*/
public static int getHour(Date date) {
return getInteger(date, Calendar.HOUR_OF_DAY);
}
/**
* 获取日期的分钟。失败返回0。
*
* @param date
* 日期字符串
* @return 分钟
*/
public static int getMinute(String date) {
return getMinute(parse(date));
}
/**
* 获取日期的分钟。失败返回0。
*
* @param date
* 日期
* @return 分钟
*/
public static int getMinute(Date date) {
return getInteger(date, Calendar.MINUTE);
}
/**
* 获取日期的秒钟。失败返回0。
*
* @param date
* 日期字符串
* @return 秒钟
*/
public static int getSecond(String date) {
return getSecond(parse(date));
}
/**
* 获取日期的秒钟。失败返回0。
*
* @param date
* 日期
* @return 秒钟
*/
public static int getSecond(Date date) {
return getInteger(date, Calendar.SECOND);
}
/**
* 获取日期。默认yyyy-MM-dd格式。失败返回null。
*
* @param date
* 日期
* @return 日期
*/
public static String getDate(Date date) {
return format(date, DateEnums.DateStyle.yyyy_MM_dd);
}
/**
* 获取日期的时间。默认HH:mm:ss格式。失败返回null。
*
* @param date
* 日期字符串
* @return 时间
*/
public static String getTime(String date) {
return getTime(parse(date));
}
/**
* 获取日期的时间。默认HH:mm:ss格式。失败返回null。
*
* @param date
* 日期
* @return 时间
*/
public static String getTime(Date date) {
return format(date, DateEnums.DateStyle.HH_mm_ss);
}
/**
* 获取日期的星期。失败返回null。
*
* @param date
* 日期字符串
* @return 星期
*/
public static DateEnums.Week getWeek(String date) {
DateEnums.Week week = null;
DateEnums.DateStyle dateStyle = getDateStyle(date);
if (dateStyle != null) {
Date myDate = parse(date, dateStyle);
week = getWeek(myDate);
}
return week;
}
/**
* 获取日期的星期。失败返回null。
*
* @param date
* 日期
* @return 星期
*/
public static DateEnums.Week getWeek(Date date) {
DateEnums.Week week = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
switch (weekNumber) {
case 0:
week = DateEnums.Week.SUNDAY;
break;
case 1:
week = DateEnums.Week.MONDAY;
break;
case 2:
week = DateEnums.Week.TUESDAY;
break;
case 3:
week = DateEnums.Week.WEDNESDAY;
break;
case 4:
week = DateEnums.Week.THURSDAY;
break;
case 5:
week = DateEnums.Week.FRIDAY;
break;
case 6:
week = DateEnums.Week.SATURDAY;
break;
}
return week;
}
public static String regMethod(String dateSign, String value) {
Pattern pattern = null;
if ("year".equals(dateSign)){
pattern = Pattern.compile(yearReg);
}else if ("month".equals(dateSign)){
pattern = Pattern.compile(monthReg);
}else if ("day".equals(dateSign)){
pattern = Pattern.compile(dateSign);
}
Matcher ma = pattern.matcher(value);
if (ma.find()) {
return ma.group();
}
return null;
}
public static int judgeChineseData(String value){
int sumNum=0;
int unitValue=0;
for(int i=0;i<value.length()-1;i++){
char te=value.charAt(i);
int temp=Integer.parseInt(chineseMap.get(String.valueOf(te)));
switch (temp) {
case 100:
if(unitValue==0){
unitValue=1;
}
sumNum+=unitValue*temp;
unitValue=0;
break;
case 10:
if(unitValue==0){
unitValue=1;
}
sumNum+=unitValue*temp;
unitValue=0;
break;
default:
unitValue+=temp;
break;
}
}
sumNum+= unitValue;
return sumNum;
}
}