如何获得今天的日期? [英] How to get today's Date?
本文介绍了如何获得今天的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
today = today.withTime(0,0, 0,0);
但没有Joda-Time,只有java.util.Date。
不推荐使用.setHours()等方法。是否有更正确的方式?
解决方案
Date today = new Date();
today.setHours(0); //分钟和秒相同
由于方法已被弃用,您可以使用日历
:
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY,0); //分钟和秒相同
如果你需要一个 Date
对象到底,只需调用 now.getTime()
In other words, I want functionality that provides Joda-Time:
today = today.withTime(0, 0, 0, 0);
but without Joda-Time, only with java.util.Date.
Methods such as .setHours() and etc. are deprecated. Is there are more correct way?
解决方案
Date today = new Date();
today.setHours(0); //same for minutes and seconds
Since the methods are deprecated, you can do this with Calendar
:
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0); // same for minutes and seconds
And if you need a Date
object in the end, simply call now.getTime()
这篇关于如何获得今天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文