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);

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐