获取所有月份以及比较两个时间的大小

来源:互联网 发布:云端瓷砖进销存软件 编辑:程序博客网 时间:2024/05/23 11:28
/**
     * 根据输入时间获取从输入时间到当前时间所有月份
     * @param mDateStr YYYY-MM
     * @return
     */
    private static List<String> getAllMonthList(String mCreateDateStr){
    List<String> mMonthList = new ArrayList<String>();
    String mDateStr = getCurrentDateYM();
    mMonthList.add(mDateStr);
   
    while(!mCreateDateStr.equals(mDateStr)){
   
    String[] dateArr = mDateStr.split("-");
    Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
    ca.set(Integer.valueOf(dateArr[0]), Integer.valueOf(dateArr[1]), 0);//月份是从0开始的,所以11表示12月 
    ca.add(Calendar.MONTH, -1); //月份减1 
    Date lastMonth = ca.getTime(); //结果 
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM"); 
   
    mDateStr = sf.format(lastMonth);
   
    mMonthList.add(mDateStr);
   
    System.out.print(mDateStr + "\n");
    };
   
    return mMonthList;

    }

/**
     * 比较两个时间的大小,判断输入时间在当前时间之前还是之后
     * @param inputDateYYYY-MM
     * @return
     */

public static void DateCompare(String inputDate,String currentDate) {
try {
//设定时间的模板
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//得到指定模范的时间
Date d1 = sdf.parse(inputDate);
Date d2 = sdf.parse(currentDate);
//比较
if((d1.getTime() - d2.getTime())>0) {
System.out.println("这一天还没过");
}else{
System.out.println("这一天已经过了");
}

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}