java 有没有比这更简单的方法来开始当天的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:11:35  来源:igfitidea点击:

Is there an easier way to get start of current day time than this?

javadatetimegroovycalendar

提问by Teo Choong Ping

I basically want to get zero or beginning hour for currrent day.

我基本上想获得当前日的零或开始时间。

def today = Calendar.instance
today.set(Calendar.HOUR_OF_DAY, 0)
today.set(Calendar.MINUTE, 0)
today.set(Calendar.SECOND, 0)
println today // Mon Mar 15 00:00:00 SGT 2010

回答by Miklos Csuka

Not simpler than the other solutions, but less lines:

并不比其他解决方案简单,但行数更少:

def now = new GregorianCalendar()
def today = new GregorianCalendar(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH))
println today.getTime()

回答by VonC

You could use the clearTime()functionof Calendarin Groovy:

您可以使用该clearTime()功能Calendar在Groovy:

def calendar = Calendar.instance
calendar.with {
  clearTime()
  println time
}

It'd be nice to have a convenience method that clears the time portion of a java.util.Dateand/or java.util.Calendar.
There are numerous use cases where it makes sense to compare month/day/year only portions of a calendar or date. Essentially, it would perform the following on Calendar:

最好有一个方便的方法来清除 ajava.util.Date和/或的时间部分java.util.Calendar
有许多用例可以仅比较日历或日期的月/日/年部分。本质上,它将在 Calendar 上执行以下操作:

void clearTime() {
    clear(Calendar.HOUR_OF_DAY)
    clear(Calendar.HOUR)
    clear(Calendar.MINUTE)
    clear(Calendar.SECOND)
    clear(Calendar.MILLISECOND) 
 }

回答by mikeyreilly

Yes, it would be nice to have to have such a method. But if you can settle for a short function then here's mine:

是的,拥有这样的方法会很好。但如果你能满足于一个简短的功能,那么这就是我的:

def startOfDay(t) {
    tz = TimeZone.getDefault();
    t +=tz.getOffset(t);
    t -= (t % 86400000);
    t-tz.getOffset(t);
}

print new Date(startOfDay(System.currentTimeMillis()));

回答by Nico Huysamen

Not quite sure if this is the best way, or indeed correct (not sure if timezone issues will arise from this), but it could work for simple use cases:

不太确定这是否是最好的方法,或者确实是正确的(不确定是否会由此产生时区问题),但它适用于简单的用例:

Start of day

一天的开始

def today = new Date()
def start = today.clearTime()

End of day

一天的结束

def today = new Date()
def eod

use (TimeCategory) {
    eod = today.clearTime() + 1.day - 1.millisecond
}

回答by Riduidel

According to Groovy datedocumentation, it seems that it's the optimal way.

根据Groovy 日期文档,这似乎是最佳方式。

However, using Groovy withkeyword, you can compress a little your statements

但是,使用带有关键字的Groovy ,您可以稍微压缩您的语句

def today = Calendar.instance
with(today) {
    set Calendar.HOUR_OF_DAY, 0
    set Calendar.MINUTE, 0
    set Calendar.SECOND, 0
}
println today // Mon Mar 15 00:00:00 SGT 2010

回答by MichailI

Starting from Java 8 use java.time solution:

从 Java 8 开始使用 java.time 解决方案:

LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
ldt.toString();