java时间相关

简介: java时间相关

对时间字符串进行比较和处理


     判断当前输入的时间必须是大于当天的23:59:59的:

String dateTime = "2017-12-13 15:50:23"//界面输入时间
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime()) //当天时间精确到23:59:59
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        Date beginDate = sdf.parse(dateTime)
        Date endDate = sdf.parse(mystrdate)
        if(endDate.time-beginDate.time>=0){
            print("11111")
        }else{
            print("22222")
        }

     获取当前系统时间:

SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        print(mystrdate)


  判断时间为多久之前进行相关页面的返回:

static String simpleRangeTimes(Long time) {
        Long cTime = getTime().time - time
        if (cTime > 0 && cTime < 600000l) return "刚刚" //0分钟<X<10分钟
        if (cTime > 600000l && cTime < 3600000l) return "1小时前"//10分钟≤X<60分钟,显示“1小时前”
        if (cTime > 3600000l && cTime < 7200000l) return "2小时前"//60分钟≤X<120分钟,显示“2小时前”
        else return formatDate(time, "yyyy-MM-dd")
    }


或者另外一种方式

/**
     * 和当前时间比较,简洁显示
     * 1小时之前,显示到分钟;24小时之前显示到小时;1个月前显示到天
     * @param time
     * @return
     */
    static String simpleRangeTime(Long time) {
        Long cTime = getTime().time - time
        if (cTime < 60000l) return "刚刚"
        if (cTime < 3600000l) return (cTime / (1000 * 60) as int) + "分钟前"
        if (cTime < 86400000l) return (cTime / (1000 * 60 * 60) as int) + "小时前"
        if (cTime < 2592000000l) return (cTime / (1000 * 60 * 60 * 24) as int) + "天前"
        else return "很久以前"
    }


使用时间计算年龄:

/**
     * 计算年龄
     * @param birthday
     * @return
     */
    static int getAge(long birthday) {
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        born.setTimeInMillis(birthday)
        now.setTimeInMillis(new Date().getTime())
        def age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR)
        if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
            age -= 1;
        }
        return age
    }
    /**
     * 计算年龄
     * @param birthday , 格式yyyy-MM-dd
     * @return
     */
    static int getAge(String birthday) {
        if (birthday) {
            return getAge(formatDate(birthday))
        }
        return 0
    }


目录
相关文章
|
存储 资源调度 安全
H3C CAS系列 一、CAS初认识
对于虚拟化,可能第一时间大家想到的是虚拟机,而对于虚拟机大家可能第一时间想到的就是我们大多数人都可能比较熟悉的VMware系列产品,比如常用VMware Workstation Pro 、VMware esxi。 而今天我带大家一起认识一款我们国产的虚拟化软件 H3C CAS。
2940 0
|
监控 安全 Cloud Native
高性能数据访问中间件 OBProxy(七):安全、协议和监控
经过本系列前六篇文章的分布式特性介绍,相信大家已经了解了 OBProxy 在 OceanBase 数据库整体架构下的作用。本篇文章我们将换一个视角,介绍一些偏“中间件”的功能:安全、协议和监控功能。从 OBProxy 整体来看,安全、协议和监控属于产品层,因此更加贴近用户和开发者,大家了解起来比较容易,我们用一篇文章来统一介绍。1. 安全功能OBProxy 的安全功能和 OBProxy 的使用场景
589 97
高性能数据访问中间件 OBProxy(七):安全、协议和监控
|
算法
动态规划—区间DP(一)
AcWing算法提高课内容,本文讲解 动态规划
492 0
动态规划—区间DP(一)
《剑指offer》-数字在排序数组中出现的次数
统计一个数字在排序数组中出现的次数。 首先吐槽下出题人的用词,啥叫排序数组?“排序”是个动词好么,“有序”作为一个形容词表示状态,修饰“数组”,才是合适的。 题目考察二分查找,首先找到指定数字最先出现的位置,然后找到最后出现的位置,他们的距离+1就是个数。
952 0
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16754 62
|
7天前
|
域名解析 人工智能 Linux
|
1天前
|
人工智能 安全 网络安全
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置