English 中文(简体)
比较 Groovy 日期忽略日期
原标题:Compare Groovy dates ignoring days

如何比较 Groovy 忽略日期的日期? 类似 : < pargen@ MM- yyyyyy_ 1 & gt; MM- yyyy_ 2/ strong >

最佳回答

你可以做到这一点:

int compareIgnoringDays( Date a, Date b ) {
  new Date( a.time ).with { newa ->
    new Date( b.time ).with { newb ->
      newa.set( date:1 )
      newb.set( date:1 )
      newa.compareTo( newb )
    }
  }
}

你可以测试一下:

Date a = Date.parse(  yyyy/MM/dd ,  2012/05/23  )
Date b = Date.parse(  yyyy/MM/dd ,  2012/05/24  )
Date c = Date.parse(  yyyy/MM/dd ,  2012/06/01  )

assert compareIgnoringDays( a, b ) == 0
assert compareIgnoringDays( b, a ) == 0
assert compareIgnoringDays( a, c ) == -1
assert compareIgnoringDays( c, a ) == 1

另一种不同的写法是:

int compareIgnoringDays( Date a, Date b ) {
  [ a, b ].collect { new Date( it.time ) }   // Clone original dates
          .collect { it.set( date:1 ) ; it } // Set clones to 1st of the month
          .with { newa, newb ->
            newa.compareTo( newb )           // Compare them (this gets returned)
          }
}
问题回答

您可以比较两个这样的日期 :

def myFormat =  MM/dd/yyyy 
if(Date.parse(myFormat,  02/03/2012 ) >= Date.parse(myFormat,  03/02/2012))
{...}




相关问题
Weird Time Issue in Python

Problem with using times in Python. Terminal > Python >>> calendar.timegm(datetime.datetime.now().utctimetuple()) 1258449380 This time indicates GMT: Tue, 17 Nov 2009 09:16:20 GMT Eclipse ...

How does a .NET process get the culture information?

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. This has caused a ...

Best way to get maximum Date value in java?

I m writing a bit of logic that requires treating null dates as meaning forever in the future (the date in question is an expiration date, which may or may not exist). Instead of putting in special ...

Date format in SQL Server

Short Question: What s the best date format to use in SQL Server? Long Explanation: We re converting our database from mysql to SQL Server. In mysql we always used int(11) to avoid the daylight ...

Converting asp.net/sql web app to be ime zone savy

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time. Now for displaying these dates back to the user, I ...

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression? I ve got a form where users can input a copyright date (as a ...

DateTime in PropertyGrid in .Net

I m currently working on some DateTime properties in a PropertyGrid in c#.Net. Im using the default drop-down datetime picker. My question: Is there any way to show the full date with the time? By ...

热门标签