How to create new Date in Groovy at specific date and time

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I wonder if there is other way how to create new Date in Groovy at specific date and time than parse it from String with Date.parse method. Can I get complete list of Date creation in Groovy?

回答1:

You can use the existing Java methods to create a date:

// takes the date encoded as milliseconds since midnight, January 1, 1970 UTC def mydate = new Date(System.currentTimeMillis())  // create from an existing Calendar object def mydate = new GregorianCalendar(2014, Calendar.APRIL, 3, 1, 23, 45).time 

Groovy also provides some streamlined extensions for creating Dates. Date.parse() and Date.parseToStringDate() parse it from a String. The Date.copyWith() method builds a date from a map. You can use them like this:

// uses the format strings from Java's SimpleDateFormat def mydate = Date.parse("yyyy-MM-dd hh:mm:ss", "2014-04-03 1:23:45")  // uses a format equivalent to EEE MMM dd HH:mm:ss zzz yyyy def mydate = Date.parseToStringDate("Thu Apr 03 01:23:45 UTC 2014")  def mydate = new Date().copyWith(     year: 2014,      month: Calendar.APRIL,      dayOfMonth: 3,      hourOfDay: 1,     minute: 23,     second: 45) 


回答2:

The other answers are outdated as of Java 8 and later. The old legacy date-time classes such as java.util.Date/.Calendar/.GregorianCalendar have proven to be poorly designed, confusing, and troublesome.

java.time

The java.time framework supplants those old classes.

Groovy can use these classes of course. I do not know the Groovy syntax, but it should be easy to adapt this simple Java example.

LocalDate/LocalTime

The LocalDate and LocalTime classes have no concept of time zone. So they do not represent actual moments on the timeline.

LocalDate localDate = LocalDate( 2016 , Month.JANUARY , 2 ); // year, month, date. LocalTime localTime = LocalTime( 12 , 30 , 0 , 0 ); // hour, minute, second, nanosecond. 

ZonedDateTime

Apply a time zone (ZoneId) to get a ZonedDateTime. Use a proper time zone name, never the 3-4 zone abbreviations commonly seen.

ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.of( localDate , localTime , zoneId ); 

Instant

You should avoid the old date-time classes. But if required, you can convert. An Instant is the java.time class to represent a moment on the timeline in UTC with a resolution of nanoseconds. Look for new methods on the old java.util.Date class for converting to/from an Instant. We can extract the needed Instant object from our ZonedDateTime.

Nanosecond vs Millisecond

Note that converting from java.time involves data loss! The java.time classes support nanosecond resolution (up to 9 digits of decimal place on fractional second) while the old java.util.Date supports only milliseconds (up to 3 decimal places). Any 4th to 9th digit of fractional second is truncated. For example, 2016-04-28T02:05:05.123456789 is truncated to 2016-04-28T02:05:05.123.

Instant instant = zdt.toInstant(); java.util.Date utilDate = java.util.Date.from( instant ); 

Going the other direction, from java.util.Date to Instant. The 4th to 9th decimal place of fractional second will be zeros, the 1st-3rd for any milliseconds.

Instant instant = utilDate.toInstant(); 


回答3:

As far as I know there's no other way. You can also pass exact time in millis to constructor of Date class but first You need to get the time in millis.

Maybe this link will be helpful.

Or this code snippet:

def calendar = new GregorianCalendar(2011,1,7,15,7,23) def date = calendar.getTime() println date 


回答4:

Groovy is good.

new Date(2016-1900, 7, 16, 20, 32, 25) 

Tue Aug 16 20:32:25 PDT 2016

Note that the year must be massaged, but you can create a new Java Date representing any second you want.

You can see more Groovy Goodness at http://mrhaki.blogspot.com/2009/08/groovy-goodness-working-with-dates.html



回答5:

You can get current date using and format it the way you want using:

`/*  y: year  m: month  d: day  h: hour  m: minute */ Date().format('yyyy-MM-dd hh-mm')  Date().format('yy/mm/dd hh')  

And this will return a string.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!