/**
* 检查两个时间差别是否在一个月内,日期格式为yyyy/MM/dd
* @param strFrom 日期1
* @param strTo 日期2
* @return boolean 日期是否在一个月以内
*/
private boolean checkMonthBetweenOneMonth (String strFrom,String strTo) {
int year = 0;
int month = 0;
int day = 0;
//如果为null,返回false
if (strFrom == null || strTo == null) {
return false;
}
//格式不正确,返回false
if (!cmnFunction.CheckDate(strFrom) || !cmnFunction.CheckDate(strTo)) {
return false;
}
//格式化字符串
strFrom = formatDate(strFrom);
strTo = formatDate(strTo);
//取得年月日
year = Integer.parseInt(strFrom.substring(0, 4));
month = Integer.parseInt(strFrom.substring(5, 7));
day = Integer.parseInt(strFrom.substring(8, 10));
Calendar FirstMonth = Calendar.getInstance();
//设置第一个日期
FirstMonth.set(year, month, day);
//取得年月日
year = Integer.parseInt(strTo.substring(0, 4));
month = Integer.parseInt(strTo.substring(5, 7));
day = Integer.parseInt(strTo.substring(8, 10));
//初始化下一月
Calendar lastMonth = Calendar.getInstance();
//设置下一个日期
lastMonth.set(year, month, day);
//FirstMonth的后一个月的日期值
FirstMonth.add(Calendar.MONTH,1);
//定义日期格式
SimpleDateFormat formatDate=new SimpleDateFormat("yyyyMMdd");
//format处理strFrom
strFrom = formatDate.format(FirstMonth.getTime());
//format处理strTo
strTo = formatDate.format(lastMonth.getTime());
//如果FirstMonth的后一个月的日期值大于lastMonth,返回false
if (strTo.compareTo(strFrom) > 0) {
return false;
}
else {
return true;
}
}
/**
* 在格式满足checkDate()格式的前提下,格式化日期,使满足10位长度,一位的补0
* 例如:2004/2/05 -> 2004/02/05
* 2004/02/5 -> 2004/02/05
* 2004/2/5 -> 2004/02/05
* @param strDate 入力日期字符串
* @return 格式化后的日期
*/
private static String formatDate(String strDate) {
if (strDate == null) {
return null;
}
String strFormatedDate = "";
if (strDate.length() == 10) {
return strDate;
}
else if (strDate.length() == 8) {
strFormatedDate =
strDate.substring(0, 5)
+ "0"
+ strDate.substring(5, 7)
+ "0"
+ strDate.substring(7, 8);
}
else if (strDate.length() == 9 && strDate.charAt(7) == '/') {
strFormatedDate =
strDate.substring(0, 8) + "0" + strDate.substring(8, 9);
}
else if (strDate.length() == 9 && strDate.charAt(6) == '/') {
strFormatedDate =
strDate.substring(0, 5) + "0" + strDate.substring(5, 9);
}
return strFormatedDate;
}
* 检查两个时间差别是否在一个月内,日期格式为yyyy/MM/dd
* @param strFrom 日期1
* @param strTo 日期2
* @return boolean 日期是否在一个月以内
*/
private boolean checkMonthBetweenOneMonth (String strFrom,String strTo) {
int year = 0;
int month = 0;
int day = 0;
//如果为null,返回false
if (strFrom == null || strTo == null) {
return false;
}
//格式不正确,返回false
if (!cmnFunction.CheckDate(strFrom) || !cmnFunction.CheckDate(strTo)) {
return false;
}
//格式化字符串
strFrom = formatDate(strFrom);
strTo = formatDate(strTo);
//取得年月日
year = Integer.parseInt(strFrom.substring(0, 4));
month = Integer.parseInt(strFrom.substring(5, 7));
day = Integer.parseInt(strFrom.substring(8, 10));
Calendar FirstMonth = Calendar.getInstance();
//设置第一个日期
FirstMonth.set(year, month, day);
//取得年月日
year = Integer.parseInt(strTo.substring(0, 4));
month = Integer.parseInt(strTo.substring(5, 7));
day = Integer.parseInt(strTo.substring(8, 10));
//初始化下一月
Calendar lastMonth = Calendar.getInstance();
//设置下一个日期
lastMonth.set(year, month, day);
//FirstMonth的后一个月的日期值
FirstMonth.add(Calendar.MONTH,1);
//定义日期格式
SimpleDateFormat formatDate=new SimpleDateFormat("yyyyMMdd");
//format处理strFrom
strFrom = formatDate.format(FirstMonth.getTime());
//format处理strTo
strTo = formatDate.format(lastMonth.getTime());
//如果FirstMonth的后一个月的日期值大于lastMonth,返回false
if (strTo.compareTo(strFrom) > 0) {
return false;
}
else {
return true;
}
}
/**
* 在格式满足checkDate()格式的前提下,格式化日期,使满足10位长度,一位的补0
* 例如:2004/2/05 -> 2004/02/05
* 2004/02/5 -> 2004/02/05
* 2004/2/5 -> 2004/02/05
* @param strDate 入力日期字符串
* @return 格式化后的日期
*/
private static String formatDate(String strDate) {
if (strDate == null) {
return null;
}
String strFormatedDate = "";
if (strDate.length() == 10) {
return strDate;
}
else if (strDate.length() == 8) {
strFormatedDate =
strDate.substring(0, 5)
+ "0"
+ strDate.substring(5, 7)
+ "0"
+ strDate.substring(7, 8);
}
else if (strDate.length() == 9 && strDate.charAt(7) == '/') {
strFormatedDate =
strDate.substring(0, 8) + "0" + strDate.substring(8, 9);
}
else if (strDate.length() == 9 && strDate.charAt(6) == '/') {
strFormatedDate =
strDate.substring(0, 5) + "0" + strDate.substring(5, 9);
}
return strFormatedDate;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12379366/viewspace-52951/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12379366/viewspace-52951/