【发布时间】:2009-09-06 13:28:13
【问题描述】:
目前我正在使用以下代码在 Groovy 中获取年、月、日、小时和分钟:
Date now = new Date()
Integer year = now.year + 1900
Integer month = now.month + 1
Integer day = now.getAt(Calendar.DAY_OF_MONTH) // inconsistent!
Integer hour = now.hours
Integer minute = now.minutes
// Code that uses year, month, day, hour and minute goes here
在这种情况下,使用getAt(Calendar.DAY_OF_MONTH) 表示月份中的某天似乎有点不一致。有没有更短的方法来获得一个月中的哪一天?
【问题讨论】:
-
你为什么现在.year + 1900?
-
Mykola: new Date().year 目前是 109,因此需要加上 1900。
-
但是为什么你把它加到某个“年份”而不使用它呢?
-
年、月、日、小时和分钟在后面的代码中使用。
-
毫秒或微秒呢?我试过 now.milliseconds 或 microseconds 但没有这样的属性