java时间相关
来源:互联网 发布:淘宝模特有什么要求 编辑:程序博客网 时间:2024/06/06 09:34
对时间字符串进行比较和处理
判断当前输入的时间必须是大于当天的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
}
- JAVA 时间相关
- java 时间相关类
- java API 时间相关
- Java 时间相关备忘
- date java 时间相关
- JAVA时间相关操作
- java时间相关
- java date时间相关处理
- java 获取时间相关总结
- Java时间日期相关开发
- Java 相关时间工具类
- Java-时间相关的处理
- java中的时间相关类简单应用
- java中时间相关的操作
- Java中关于时间的相关操作
- Java 时间表示的相关类
- java日期时间相关问题小结
- java 时间相关的类互相转换
- Mybatis之动态sql
- 前端px与rem换算
- Linux安装jdk7开发环境
- Andorid Studio NDK开发-编译OpenSSL类库
- 每天一个linux命令(9):touch 命令
- java时间相关
- 欢迎使用CSDN-markdown编辑器
- c#中 int和String互相转换
- (8)Spark 2.0.0 查看job 历史日志
- Android 类库
- Cramfs、JFFS2、YAFFS2的全面对比
- bug等小问题
- ubuntu 16.04:Couldn't import dot_parser, loading of dot files will not be possible.
- centos7下配置apache伪静态页