日期工具类

/**
* 判断巡检时间是否在当前日期后面一周内
* @param planTime
* @return
* @throws ParseException
*/
private boolean isAfterToSeven(Date plTime) throws ParseException 
{
Date nowDate = new Date();
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(nowDate);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(plTime);
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
int between = day2 - day1;
if(between >= 7)
{
return  true;
}
else
{
return false;
}
}

/**
     * 判断当前日期是星期几
     * @param pTime 需要判断的时间
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
public static int dayForWeek(String pTime) throws Exception 
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}

/** 
  * 获取今天之后几天的时间 
  * @param d 
  * @param day 
  * @return 
  */  
public static String getDateAfter(Date d,int day)
{  
Calendar now =Calendar.getInstance();  
now.setTime(d);  
now.set(Calendar.DATE,now.get(Calendar.DATE)+day);  

SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(now.getTime());
return time;  
} 

/**
     * 获得两个日期间相差的天数
     * @param endtime
     * @param starttime
     * @return
     */
    public static long getsubdates(String endtime,String starttime){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
long days=0;
    try { 
       Date d1 = df.parse(endtime); 
       Date d2 = df.parse(starttime); 
       long diff = d1.getTime() - d2.getTime(); 
       days = diff / (1000 * 60 * 60 * 24); 
    } catch (Exception e) { } 
    return days;
}

/**
* 获取当前月的上一个月的时间
* @return
*/
public String getlastDate()
{
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -7);
SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
String time = format.format(c.getTime());
System.out.println(time);
return time;

}
/**
 * 获取本月共有多少天 
 */
private static void getMonDayNum() {
java.util.Calendar cal = java.util.Calendar.getInstance();
int maxDay = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
System.out.println(maxDay);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值