package cn.com.css.misps.graph.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @brief DateUtil.java 日期工具类
* @attention
* @author 涂作权
* @date 2013-10-13
* @note begin modify by null
*/
public class DateUtil {
/**
* \brief 生成类似:201310181533 的时间值
*
* @return
* @attention
* @author 涂作权
* @date 2013-10-18
* @note begin modify by null
*/
public static String getTimeNow() {
// 使用默认时区和语言环境获得一个日历
Calendar calendar = Calendar.getInstance();
// 今年
String year = calendar.get(Calendar.YEAR) + "";
// 当月
String month = ((calendar.get(Calendar.MONTH) + 1) < 10) ? "0"
+ (calendar.get(Calendar.MONTH) + 1) : ""
+ (calendar.get(Calendar.MONTH) + 1);
// 当天
String day = (calendar.get(Calendar.DAY_OF_MONTH) < 10) ? "0"
+ calendar.get(Calendar.DAY_OF_MONTH) : ""
+ calendar.get(Calendar.DAY_OF_MONTH);
// 时,24小时制几点
String hour = (calendar.get(Calendar.HOUR_OF_DAY) < 10) ? "0"
+ calendar.get(Calendar.HOUR_OF_DAY) : ""
+ calendar.get(Calendar.HOUR_OF_DAY);
// 分
String minute = (calendar.get(Calendar.MINUTE) < 10) ? "0"
+ calendar.get(Calendar.MINUTE) : ""
+ calendar.get(Calendar.MINUTE);
// 秒,暂时不用
// int seconds = calendar.get(Calendar.SECOND);
return year + month + day + hour + minute;
}
/**
* \brief 获取到n天后的日期
*
* @param string
* @param string2
* @attention
* @author 涂作权
* @date 2013-11-16
* @note begin modify by null
*/
public static String handleAdd(String str, Integer num) {
// 获得一个默认时区和语言环境获得一个日历
Calendar calendar = Calendar.getInstance();
String newStr = null;
if (str.toLowerCase().startsWith("y")) {
newStr = (calendar.get(Calendar.YEAR) + num) + "";
// 判断字符里面有几个y,如果只有一个,则取得最后两位,如果有4位,则返回4位
return newStr.substring(newStr.length() - str.length());
} else if (str.startsWith("M")) {
// 重新设置月份
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + num);
// 否则直接返回
return ((calendar.get(Calendar.MONTH) + 1) < 10) ? "0"
+ (calendar.get(Calendar.MONTH) + 1) : (calendar
.get(Calendar.MONTH) + 1)
+ "";
} else if (str.startsWith("d")) {
//int monthNow1 = calendar.get(Calendar.MONTH) + 1;// 获取月份
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+ num);
//int monthNow2 = calendar.get(Calendar.MONTH) + 1;// 获取月份
String mouth = ((calendar.get(Calendar.MONTH) + 1) < 10) ? "0"
+ (calendar.get(Calendar.MONTH) + 1) + "月" : (calendar
.get(Calendar.MONTH) + 1)
+ "月";
return (calendar.get(Calendar.DAY_OF_MONTH) < 10) ? mouth + "0"
+ calendar.get(Calendar.DAY_OF_MONTH) : mouth
+ calendar.get(Calendar.DAY_OF_MONTH);
} else if (str.startsWith("h")) {
calendar.set(Calendar.HOUR_OF_DAY, calendar
.get(Calendar.HOUR_OF_DAY)
+ num);
return (calendar.get(Calendar.HOUR_OF_DAY) < 10) ? "0"
+ calendar.get(Calendar.HOUR_OF_DAY) : ""
+ calendar.get(Calendar.HOUR_OF_DAY);
} else if (str.startsWith("m")) {
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + num);
return (calendar.get(Calendar.MINUTE) < 10) ? "0"
+ calendar.get(Calendar.MINUTE) : calendar
.get(Calendar.MINUTE)
+ "";
} else if (str.startsWith("s")) {
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + num);
return (calendar.get(Calendar.SECOND) < 10) ? "0"
+ calendar.get(Calendar.SECOND) : calendar
.get(Calendar.SECOND)
+ "";
} else {
calendar.set(Calendar.MILLISECOND, calendar
.get(Calendar.MILLISECOND)
+ num);
return calendar.get(Calendar.MILLISECOND) + "";
}
}
/**
* \brief 获取到n天后的日期
*
* @param string
* @param string2
* @attention
* @author 涂作权
* @date 2013-11-16
* @note begin modify by null
*/
public static String handleMinus(String str, Integer num) {
// 获得一个默认时区和语言环境获得一个日历
Calendar calendar = Calendar.getInstance();
String newStr = null;
if (str.toLowerCase().startsWith("y")) {
newStr = (calendar.get(Calendar.YEAR) - num) + "";
// 判断字符里面有几个y,如果只有一个,则取得最后两位,如果有4位,则返回4位
return newStr.substring(newStr.length() - str.length());
} else if (str.startsWith("M")) {
// 重新设置月份
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - num);
// 否则直接返回
return ((calendar.get(Calendar.MONTH) + 1) < 10) ? "0"
+ (calendar.get(Calendar.MONTH) + 1) : (calendar
.get(Calendar.MONTH) + 1)
+ "";
} else if (str.startsWith("d")) {
calendar.set(Calendar.DAY_OF_MONTH, calendar
.get(Calendar.DAY_OF_MONTH)
- num);
return (calendar.get(Calendar.DAY_OF_MONTH) < 10) ? "0"
+ calendar.get(Calendar.DAY_OF_MONTH) : calendar
.get(Calendar.DAY_OF_MONTH)
+ "";
} else if (str.startsWith("h")) {
calendar.set(Calendar.HOUR_OF_DAY, calendar
.get(Calendar.HOUR_OF_DAY)
- num);
return (calendar.get(Calendar.HOUR_OF_DAY) < 10) ? "0"
+ calendar.get(Calendar.HOUR_OF_DAY) : ""
+ calendar.get(Calendar.HOUR_OF_DAY);
} else if (str.startsWith("m")) {
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - num);
return (calendar.get(Calendar.MINUTE) < 10) ? "0"
+ calendar.get(Calendar.MINUTE) : calendar
.get(Calendar.MINUTE)
+ "";
} else if (str.startsWith("s")) {
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - num);
return (calendar.get(Calendar.SECOND) < 10) ? "0"
+ calendar.get(Calendar.SECOND) : calendar
.get(Calendar.SECOND)
+ "";
} else {
calendar.set(Calendar.MILLISECOND, calendar
.get(Calendar.MILLISECOND)
- num);
return calendar.get(Calendar.MILLISECOND) + "";
}
}
/**
* \brief 将当前日志按照指定的格式输出字符串
*
* @param aMask
* @return
* @attention 这里只是对当前日志进行转换
* @author toto
* @date 2013-11-15
* @note begin modify by null
*/
public static String date2String(String aMask) {
StringBuffer sb = new StringBuffer(aMask);
if (aMask.contains("[") && aMask.contains("]")) {
// 再将[]里面的字符串取出来,让后把它替换成想要的字符串
Pattern pattern = Pattern.compile("(\\[\\w*\\+{0,}\\-{0,}\\d{0,}\\])");
// 获得Matcher
Matcher matcher = pattern.matcher(sb.toString());
while (matcher.find()) {
String subMask = matcher.group(0);
System.out.println("subMask = " + subMask);
// 再判断这个字符串中是否有"+"号。如果有再进行处理
if (subMask.contains("+")) {
String tempSubMask = subMask.replace("[", "").replace("]",
"");
String[] temp = tempSubMask.split("\\+");
String replacedString = handleAdd(temp[0], Integer
.parseInt(temp[1]));
sb = new StringBuffer(sb.toString().replace(subMask,
replacedString));
continue;
}
if (subMask.contains("-")) {
String tempSubMask = subMask.replace("[", "").replace("]",
"");
String[] temp = tempSubMask.split("\\-");
String replacedString = handleMinus(temp[0], Integer
.parseInt(temp[1]));
sb = new StringBuffer(sb.toString().replace(subMask,
replacedString));
continue;
}
// 判断,如果有"["或"]"这种的字符还要另行处理
SimpleDateFormat df = new SimpleDateFormat(subMask);
String replacedString = df.format(new Date());
// 将字串中的类似[yyyy]这样的串变成2013年这样的串
sb = new StringBuffer(sb.toString().replace(subMask,
replacedString));
}
return sb.toString().replace("[", "").replace("]", "");
} else {
return aMask;
}
}
/**
* \brief 判断字符串中是否有特定字母
*
* @param args
* @attention
* @author toto
* @date 2013-11-15
* @note begin modify by 修改人 修改时间 修改内容摘要说明
*/
//public static void main(String[] args) {
// System.out.println("YYYY".toLowerCase());
// System.out.println(date2String("SEVP_NMC_WEAP_SOB_ER24_ACHN_LNO_P9_[yy][MM][dd][HH][mm][FFF]xx.MIC"));
// System.out.println(date2String("[yyyy]-[MM]-[dd] [HH]:[mm]:[ss] [E]"));
// System.out.println(date2String("[yyyy].[MM].[dd] [G] 'at' [HH]:[mm]:[ss] [z]"));
// System.out.println(date2String("[y][mm][dd][hh]_[mm][dd][hh]_[mm][dd].000"));
//System.out.println(date2String("[y+1][MM][dd][hh]_[mm][dd][hh]_[mm][dd].000"));
// // boolean b =
// Pattern.compile("(?i)[a-z]").matcher("ada123456").find();
// // System.out.println("是否包含指定字符? + " + b);
// //
// // Pattern pattern = pattern.compile("yy");
//
// SimpleDateFormat df = null;
// Date chgDate = null;
// try {
//
// } catch (Exception e) {
// // TODO: handle exception
// }
// System.out.println(date2String("MM月dd日08时-dd日08时"));
// System.out.println(date2String("[YY][MM][DD][HH]00[MiMi]"));
// System.out.println(date2String("[y][mm][dd][hh]_[mm][dd][hh]_[mm][dd].000"));
// System.out.println(date2String("-----------------------------------------"));
// String str =
// "SEVP_NMC_WEAP_SOB_ER24_ACHN_LNO_P9_[YY+1][MM][DD][HH][mm][FFF]xx.MIC";
// Pattern pattern = Pattern.compile("(\\[\\w*\\+{0,}\\d{0,}\\])");
// //Pattern pattern = Pattern.compile("(\\[\\w*\\])");
//
// Matcher matcher = pattern.matcher(str);
// while (matcher.find()) {
// System.out.println("Group 0:" + matcher.group(0));// 得到第0组——整个匹配
// System.out.println("Group 1:" + matcher.group(1));//
// 得到第一组匹配——与(or)匹配的
// System.out.println("Group 2:" + matcher.group(2));//
// 得到第二组匹配——与(ld!)匹配的,组也就是子表达式
// System.out.println("Start 0:" + matcher.start(0) + " End 0:"
// + matcher.end(0));// 总匹配的索引
// System.out.println("Start 1:" + matcher.start(1) + " End 1:"
// + matcher.end(1));// 第一组匹配的索引
// System.out.println("Start 2:" + matcher.start(2) + " End 2:"
// + matcher.end(2));// 第二组匹配的索引
// System.out.println(str.substring(matcher.start(0),
// matcher.end(1)));// 从总匹配开始索引到第1组匹配的结束索引之间子串——Wor
// }
//获取当前日历类
//Calendar calendar = Calendar.getInstance();
//calendar.set(Calendar.YEAR, Calendar.YEAR+1);
//System.out.println(calendar.get(Calendar.YEAR)+1);
//calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)+3);
//System.out.println(calendar.get(Calendar.MONTH));
//calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 3);
//calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 3);
//System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
//}
}