GIF.gif项目遇到一个需求:选择开始时间,然后结束时间默认是开始时间的后一个月,效果如下
思路分析:
假如开始时间选择了2017年12月22日,那么结束时间就变成了2018年1月22日,要实现这种效果,我之前还纠结了一下,最后想到用android中的Calendar来实现比较快一点
实现过程的核心方法
/**
* 包含当前日期12个月之后
* @return
*/
public static String getCurrent12MonthAfter(String dateType,String dataTime) {
// Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(dateType, Locale.getDefault( ));
// String currentTime = sdf.format(date);
try {
Date data = sdf.parse(dataTime);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(data);
if (Calendar.MONTH - 12 != 0) {
rightNow.add(Calendar.YEAR, 0);// 年份不变
rightNow.add(Calendar.MONTH, +1);
Date dataNow = rightNow.getTime();
String nowCurrentTime = sdf.format(dataNow);
return nowCurrentTime;
} else {
rightNow.add(Calendar.YEAR, +1);// 年份加1
rightNow.add(Calendar.MONTH, 1);
Date dataNow = rightNow.getTime();
String nowCurrentTime = sdf.format(dataNow);
return nowCurrentTime;
}
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
最后附上项目中用的完整的日期工具类
public class TimeUtils {
public static String getCurrentTime() {
return getCurrentTime("yyyy-MM-dd HH:mm:ss");
}
public static String getCurrentTime2() {
return getCurrentTime("yyyyMMddHHmmss");
}
public static String getCurrentTime3() {
return getCurrentTime("yyyyMMdd");
}
public static String getCurrentTime(String format) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
String currentTime = sdf.format(date);
return currentTime;
}
public static String getDate(){
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return sDateFormat.format(new java.util.Date());
}
/**
* 包含当前日期12个月之后
* @return
*/
public static String getCurrent12MonthAfter(String dateType,String dataTime) {
// Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(dateType, Locale.getDefault( ));
// String currentTime = sdf.format(date);
try {
Date data = sdf.parse(dataTime);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(data);
if (Calendar.MONTH - 12 != 0) {
rightNow.add(Calendar.YEAR, 0);// 年份不变
rightNow.add(Calendar.MONTH, +1);
Date dataNow = rightNow.getTime();
String nowCurrentTime = sdf.format(dataNow);
return nowCurrentTime;
} else {
rightNow.add(Calendar.YEAR, +1);// 年份加1
rightNow.add(Calendar.MONTH, 1);
Date dataNow = rightNow.getTime();
String nowCurrentTime = sdf.format(dataNow);
return nowCurrentTime;
}
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
/**
* 将string 19910924 --》 1991-09-24
* @param date
* @return
*/
public static String getFormatDate(String date){
StringBuilder sb = new StringBuilder();
sb.append(date.substring(0,4));
sb.append("-");
sb.append(date.substring(4,6));
sb.append("-");
sb.append(date.substring(6,8));
return sb.toString();
}
/**
* startDate 格式 yyyy-MM-dd
* currentDate 格式 yyyy-MM-dd
* 比较日期的方法:开始时间与当前时间比较,小于当前时间的话,不能下一步
* @return
*/
public boolean compareDate(String startDate,String currentDate){
ArrayList<String> mStartDateArr = getYearMonthDate(startDate);
ArrayList<String> mCurrentDateArr = getYearMonthDate(currentDate);
if (mStartDateArr == null || mStartDateArr.size() <= 0){
return false;
}
if (mCurrentDateArr == null || mCurrentDateArr.size() <= 0){
return false;
}
int mYearNum = Integer.valueOf(mStartDateArr.get(0));
int mMonthNum = Integer.valueOf(mStartDateArr.get(1));
int mDayNum = Integer.valueOf(mStartDateArr.get(2));
int mCurrentYearNum = Integer.valueOf(mCurrentDateArr.get(0));
int mCurrentMonthNum = Integer.valueOf(mCurrentDateArr.get(1));
int mCurrentDayNum = Integer.valueOf(mCurrentDateArr.get(2));
if (mYearNum < mCurrentYearNum){
return false;
}
if (mYearNum == mCurrentYearNum && mMonthNum < mCurrentMonthNum){
return false;
}
if (mYearNum == mCurrentYearNum && mMonthNum == mCurrentMonthNum && mDayNum < mCurrentDayNum){
return false;
}
return true;
}
/**
* 根据日期获取年月日的方法
* @param date
* @return
*/
private ArrayList<String> getYearMonthDate(String date){
ArrayList<String> mDateList = new ArrayList<String>();
mDateList.clear();
String separators = "-";
if (TextUtils.isEmpty(date)){
return null;
}
String[] split = date.split(separators);
mDateList.add(split[0]);
mDateList.add(split[1]);
mDateList.add(split[2]);
return mDateList;
}
}
网友评论