当前位置:   article > 正文

java 判断今天_Java 判断某个具体时间是否属于当天范围(24H)

java 判断时间是不是当天24点之前

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

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/260607
推荐阅读
相关标签
  

闽ICP备14008679号