今天在做发短信功能时需要限制用户在一天中只能够收到最多5次验证码,而数据库中对时间的存储格式为2016-3-10 20:08:28,这就需要将数据库中的数据提取日期(不包含时间)与当前的日期对比。下面是从网上摘的java中输出当前时间的各种方法:
package com.grace.test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class showDate {
public static void main(String[] args) throws ParseException {
Date d = new Date();
String s = null;
/** 输出格式: Mon May 05 15:23:58 CST 2014 */
System.out.println(d);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/** 输出格式: 2014年5月5日 星期一 */
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/** 输出格式: 14-5-5 */
s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);
/** 输出格式: 2014-5-05 00:00:00 大写H为24小时制 */
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = sdf.format(d);
System.out.println(s);
/** 输出格式: 2014-5-05 00:00:00 小写h为12小时制 */
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = sdf2.format(d);
System.out.println(s);
/** 输出格式: 20140505000000 */
DateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
s = sdf3.format(d);
System.out.println(s);
/** 字符串转换城日期格式 */
s = sdf.format(d);
Date today = sdf.parse(s);
System.out.println("字符串转成日期1:" + today);
System.out.println("字符串转成日期2:" + sdf.format(today));
/** 单独输出年月日时分秒等 */
Calendar c = Calendar.getInstance();
System.out.println("年: " + c.get(Calendar.YEAR));
// 月份从0开始,加1校正
System.out.println("月: " + (c.get(Calendar.MONTH) + 1) + "");
System.out.println("日: " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + c.get(Calendar.MINUTE));
System.out.println("秒: " + c.get(Calendar.SECOND));
System.out.println("当前时间毫秒数:" + c.getTimeInMillis());
System.out.println("当前时间: " + c.getTime());
}
}
我对从数据库中提取的时间与当天日期的比较处理的方法:
<pre class="java" name="code">/**
* PC注册-获取手机验证码——张子杰
* @param session
* @param mobile
* @return
* @throws ParseException
* @throws UnknownHostException
*/
@RequestMapping(value="/ssl/getRegMobileCode",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> getRegMobileCode3(HttpServletRequest request,HttpSession session,@RequestParam(value="mobile",required=false) String mobile) throws ParseException, UnknownHostException{
Map<String, String> map = new HashMap<String, String>(1);
String rand = null;
String msg = "0";
Long time = 0L;
//查询手机获取验证码的次数
List<PreventSMSPort> preventSMSPortMC;
preventSMSPortMC = userRegisterService.getCountMobile(mobile);
int m = 0;
for(int i=0;i<preventSMSPortMC.size();i++){
//提起数据库中的日期与当前日期比较,相同的则当天获取验证码条数+1
//m为当天获取验证码的条数
if(preventSMSPortMC.get(i).getCreateTime() != null){
String s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(preventSMSPortMC.get(i).getCreateTime());
Date d = new Date();
String q = null;
q = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
if(s.equals(q)){
m = m + 1;
}
}
}
if(m < 5){
System.out.println("m是小于5的");
//ip获取验证码时间次数
//String ip = request.getRemoteAddr().toString();
PreventSMSPort preventSMSPort = new PreventSMSPort();
PreventSMSPort preventSMSPortNew = new PreventSMSPort();
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();//获得本机IP
try{
//CustUser user = (CustUser)session.getAttribute(Constants.USER);
if(accountSetService.mobileExist(mobile)){
msg = "2";
}else{
time = checkAuthSecond(session, 1);
if(time<60 && time!=0){
msg = "3";
}else{
CustUser user = new CustUser();
rand = accountSetService.getMobileRandCode(user,mobile);
//保存该ip获取验证码记录
preventSMSPortNew.setMobileCode(rand);
preventSMSPortNew.setIsUse("1");
preventSMSPortNew.setExpireTime(new Date());
//新加的字段
preventSMSPortNew.setCreateTime(new Date());
preventSMSPortNew.setIp(ip);
preventSMSPortNew.setMobile(mobile);
//新加的字段结束
userRegisterService.savePreventSMSPort(preventSMSPortNew);
session.removeAttribute(Constants.MOBILE_RAND);
session.setAttribute(Constants.MOBILE_RAND, new StringBuffer(rand).append(":").append(mobile).toString());
session.setAttribute(Constants.MOBILE_NUM,1);
session.setAttribute(Constants.AUTH_MOBILE, new Date());
}
}
}catch(Exception e){
msg = "1";
e.printStackTrace();
logger.info(e.getMessage());
}
System.out.println("验证码为" + preventSMSPortNew.getMobileCode());
}else{
//当天发送的短信数目已经大于5,给出提示
msg = "10";
}
map.put("time", time.toString());
map.put("msg", msg);
return map;
}