获得当前系统日期 星期几、小时
public static void main(String[] args) {
Date date=new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
int h = cal.get(Calendar.HOUR_OF_DAY);
System.err.println(w);
System.err.println(h);
Date d = new Date();
System.err.println("d.getHours()===="+d.getHours());
}
信用卡有效日期校验
/****
* boolean result=checkBankEffectiveData("0917");
* 信用卡有效日期校验
* @param effectiveData
* @return
*/
private static boolean checkBankEffectiveData(String effectiveData) throws Exception {
String month=effectiveData.substring(0,2);
String year=effectiveData.substring(2,4);
effectiveData="20"+year+"-"+month+"-01";
String pattern = "yyyy-MM-dd";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date bankEffectiveData = dateFormat.parse(effectiveData);
Date nowdate = new Date();
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(bankEffectiveData);
c2.setTime(nowdate);
int result = c1.compareTo(c2);
if (result >= 0)
return true;
else
return false;
}
两个日期相差多少分钟
/***
* 两个日期相差多少分钟
* @param beginTime beginTime<endTime
* @param endTime
* @return
*/
public static Long differenceTime(Date beginTime, Date endTime) {
long begintime = beginTime.getTime();
long endtime = endTime.getTime();
long diff;
long diffTime;
if (begintime < endtime) {
diff = endtime - begintime;
} else {
diff = begintime-endtime;
}
// diffTime = diff / (60 * 60 * 1000);
// System.err.println("相差" + diffTime + "小时");
diffTime = diff / (60 * 1000);
System.err.println("相差" + diffTime + "分钟");
return diffTime;
}
Calendar
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); //获取东八区时间
int year = c.get(Calendar.YEAR); //获取年
int month = c.get(Calendar.MONTH) + 1; //获取月份,0表示1月份
int day = c.get(Calendar.DAY_OF_MONTH); //获取当前天数
int first = c.getActualMinimum(c.DAY_OF_MONTH); //获取本月最小天数
int last = c.getActualMaximum(c.DAY_OF_MONTH); //获取本月最大天数
int time = c.get(Calendar.HOUR_OF_DAY); //获取当前小时
int min = c.get(Calendar.MINUTE); //获取当前分钟
int xx = c.get(Calendar.SECOND); //获取当前秒
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String curDate = s.format(c.getTime()); //当前日期
System.err.println("当前时间:"+year + "-" + month + "-"+ day + " "+time + ":" + min +":" + xx);
System.err.println("第一天和最后天:" + first +"," + last);
System.err.println("当前日期curDate====:" + curDate);
// 当前时间:2017-8-24 12:37:17
// 第一天和最后天:1,31
// 当前日期curDate====:2017-08-24 12:37:17
日期校验
private static final long SEVEN_DAYS=1000*60*60*24*7;
private static final long ONE_DAYS=1000*60*60*24;
private static final long ONE_HOUR=1000*60*60;
private static final int MAX_COUNTLIMIT = 5000;
/****
* 日期校验
* @param currDate
* @param beginDate
* @param endDate
* @return
*/
private boolean dateValidate(Date currDate, Date beginDate,Date endDate) {
long currLong = currDate.getTime();//当前时间
long beginLong = beginDate.getTime();//开始时间
long endLong =endDate.getTime();//结束时间
long diffBegin=currLong-beginLong;
long diffEnd=currLong-endLong;
long diffBeginEnd = endLong-beginLong;//判断日期时候合法>0
if(diffBegin<0||diffEnd<0||diffBeginEnd<0){
System.err.println("日期非法");
return false;
}
if (diffBegin > SEVEN_DAYS || diffEnd > SEVEN_DAYS) {
System.err.println("时间范围大于7日");
return false;
}
return true;
}