Java内置日期操作和比较 [英] Java Built In Date Manipulation and Comparison

查看:120
本文介绍了Java内置日期操作和比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个日期时间戳,然后添加10个小时,然后进行线程检查以查看是否已经过了。

I'm looking to create a datetime stamp, then add 10 hours to it, then have a thread check to see if the time has elapsed.

我读了通过,时间比较,但似乎有点复杂/复杂的东西这么简单。特别是如果你的时间比较跨越午夜。

I read through, Time comparison but it seems a bit complicated/convoluted for something so simple. Especially if your time comparison goes across midnight.

我的理解是,java的底层日期时间假设是一个很长的时间,如果这是真的,有没有一个简单的方法再添加一个长时间,如等于10个小时的数字?还是其他一些方法,如添加两个日期?

My understanding is that java's underlying datetime, is suppose to be a long, if this is true, is there a simple way to add another long to it, such as the number equivalent of 10 hours? Or some other means such as adding two dates?

注意:解决方案需要成为核心java的一部分,不能成为第三方lib的一部分。 p>

Note: The solution needs to be part of core java, can't be part of a 3rd party lib.

推荐答案

您可以使用日历执行这个数学,

You can use a Calendar to perform that math,

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 10); // Add 10 hours.
Date date2 = cal.getTime(); // Now plus 10 hours.
Date date = new Date(); // Now.

这篇关于Java内置日期操作和比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆