美文网首页
时间工具类总结

时间工具类总结

作者: 全球顶尖伪极客 | 来源:发表于2018-03-15 19:05 被阅读0次

默认:

//  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时:分:秒

1、比较年月日时分秒的字符串的大小20180315100900

     * 检查任务是否在有效期内
     * 时间比较格式:20180315100900
     */
    public boolean checkValidData(String startTime, String endTime) {
        boolean flag = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//年-月-日
        try {
            Date currentDate = new Date();
            String currentTime = dateFormat.format(currentDate);

            if (currentTime.compareTo(startTime) >= 0 && currentTime.compareTo(endTime) <= 0) {
                flag = true;
            }
            Log.i("", "开始时间: " + startTime + "\n当前时间:" + currentTime + "\n结束时间:" + endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

2.获取当前毫秒值时间

long s = System.currentTimeMillis();

3.

  /**
     * 检查任务是否在有效期内
     */
    public boolean checkValidData(XJTaskEntity taskEntity) {
        boolean flag = false;
        String beginDay = taskEntity.getTaskBeginDay();//开始日期
        String endDay = taskEntity.getTaskEndDay();//结束日期
        String beginHour = taskEntity.getTaskBeginHour();//开始小时分钟
        String endHour = taskEntity.getTaskEndHour();//结束小时分钟

        try {
            Date currentDate = new Date();

            Date beginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginDay.trim() + " 00:00:00");
            Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDay.trim() + " 23:59:59");

            if (currentDate.after(beginDate) && currentDate.before(endDate)) {

                if ("Y".equalsIgnoreCase(taskEntity.getIsCoercive())) {
                    //强制只能在规定时间内操作
                    String currentDay = new SimpleDateFormat("yyyy-MM-dd").format(currentDate);
                    Date beginDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(currentDay + " " + beginHour.trim());
                    Date endDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(currentDay + " " + endHour.trim());
                    if (currentDate.after(beginDate2) && currentDate.before(endDate2)) {
                        flag = true;
                    }

                } else {
                    flag = true;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

4.

   /**
     * 检查任务是否在有效期内 
     * 将20180315100900这种类型的字符串转为Date进行比较时间的有效期
     */
    public boolean checkValidData(String startTime, String endTime) {
        boolean flag = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//年-月-日
        try {
            Date currentDate = new Date();
            Date startDate = dateFormat.parse(startTime.trim());//开始时间
            Date endDate = dateFormat.parse(endTime.trim());//结束时间
            if (currentDate.after(startDate) && currentDate.before(endDate)) {
                flag = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


相关文章

  • 时间工具类总结

    默认: 1、比较年月日时分秒的字符串的大小20180315100900 2.获取当前毫秒值时间 3. 4.

  • 各种调优工具

    各种调优工具 总结: 三类工具 基础工具 (NSLog的方式记录运行时间.) 性能工具.检测各个部分的性能表现,找...

  • 时间工具类

    package com.zymotor.eutech.web.util; import org.springfra...

  • 时间工具类

    1.本周开始时间戳 - 以星期一为本周的第一天 public static String getWeekStart...

  • 时间工具类

    package com.liumu.util; import java.sql.Timestamp; import...

  • 时间工具类

    去除时间的时分秒

  • Android工具类---日期时间工具类

    前言 在任何APP开发中,日期和时间是无处不在的,例如QQ、微信,每条信息都会显示发送时间,还有空间、朋友圈每一条...

  • Date工具类

    记录日常开发总结的日期工具类 字符串转日期 获取指定时间范围列表

  • 卡顿优化总结

    最近在极客时间学习,做一下总结。 Android卡顿排查工具 常用的排查工具分为两种类型: instrument类...

  • iOS开发常用的工具类和宏定义

    iOS开发常用的工具类和宏定义 开发总结的工具类和宏 https://github.com/xiaoChen66...

网友评论

      本文标题:时间工具类总结

      本文链接:https://www.haomeiwen.com/subject/fftkqftx.html