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

发布于 2024-08-24 23:33:23 字数 221 浏览 7 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

染火枫林 2024-08-31 23:33:23

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

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()

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()
琉璃梦幻 2024-08-31 23:33:23

您可以使用 clearTime() 函数 Groovy 中的 ="http://groovy.codehaus.org/groovy-jdk/java/util/Calendar.html" rel="noreferrer">日历

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

如果有一个方便的方法来清除 java.util.Date 和/或 java.util.Calendar 的时间部分,那就太好了。
在许多用例中,仅比较日历或日期的月/日/年的部分是有意义的。本质上,它将在日历上执行以下操作:

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

You could use the clearTime() function of Calendar in 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.Date and/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:

void clearTime() {
    clear(Calendar.HOUR_OF_DAY)
    clear(Calendar.HOUR)
    clear(Calendar.MINUTE)
    clear(Calendar.SECOND)
    clear(Calendar.MILLISECOND) 
 }
盛夏已如深秋| 2024-08-31 23:33:23

是的,如果有这样的方法就好了。但如果你可以接受一个简短的函数,那么这是我的:

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

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

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()));
简单爱 2024-08-31 23:33:23

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

但是,使用 Groovy with 关键字,您可以压缩一点你的陈述

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

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

However, using Groovy with keyword, you can compress a little your statements

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
2024-08-31 23:33:23

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

一天开始

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

一天结束

def today = new Date()
def eod

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

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
}
旧时浪漫 2024-08-31 23:33:23

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

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

Starting from Java 8 use java.time solution:

LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
ldt.toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文