赞
踩
Java代码分享:package com.what21;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
/**
* @param inputJudgeDate
* @return
*/
public static boolean isToday(Date inputJudgeDate) {
boolean flag = false;
// 获取当前系统时间
long longDate = System.currentTimeMillis();
Date nowDate = new Date(longDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(nowDate);
String subDate = format.substring(0, 10);
// 定义每天的24h时间范围
String beginTime = subDate + " 00:00:00";
String endTime = subDate + " 23:59:59";
Date paseBeginTime = null;
Date paseEndTime = null;
try {
paseBeginTime = dateFormat.parse(beginTime);
paseEndTime = dateFormat.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
if (inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
flag = true;
}
return flag;
}
/**
* @param args
*/
public static void main(String[] args) {
// 测试时间1
String testTimeOne = "2018-10-31 10:25:11";
// 测试时间2
String testTimeTwo = "2018-11-09 10:09:51";
// 时间格式化
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date psTestTimeOne = null;
Date psTestTimeTwo = null;
try {
psTestTimeOne = format.parse(testTimeOne);
psTestTimeTwo = format.parse(testTimeTwo);
} catch (ParseException e) {
e.printStackTrace();
}
boolean timeOneIsToday = DateUtils.isToday(psTestTimeOne);
boolean timeTwoIsToday = DateUtils.isToday(psTestTimeTwo);
System.out.println("测试时间输出为true,则处于当天24h范围内,false反之。");
System.out.println("测试时间一:" + timeOneIsToday);
System.out.println("测试时间二:" + timeTwoIsToday);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。