I have this string got from JIRA as the date/time I need to compare which one is earlier:"21/Sep/12 2:01 PM". How can I do this in Groovy?
Thanks Jirong
-
1Could you edit the question and let us know what you have already tried to do. The more details the better.Avetis– Avetis2015年03月09日 14:11:32 +00:00Commented Mar 9, 2015 at 14:11
-
I am new to Groovy. As an system admin, just try to do a quick script. The date/time output from JIRA is like this and I need to compare it to the current time.user1288329– user12883292015年03月09日 14:14:56 +00:00Commented Mar 9, 2015 at 14:14
-
2Hi. Please check what is on-topic here and consider asking at stackoverflowBenni– Benni2015年03月09日 14:43:02 +00:00Commented Mar 9, 2015 at 14:43
1 Answer 1
Based on the requirements you have put down I assume that you already have the date somewhere in code (if not check out com.atlassian.jira.component.ComponentAccessor). As for comparing the dates here is a short script and if you need more dates then just stack them up in an array and loop through them.
import java.text.SimpleDateFormat;
import java.util.*;
class DateTimeComparer
{
public Date convertDateTime(String element) throws Exception
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm a");
return dateFormat.parse(element);
}
public static String compareDateTimes(Date date1, Date date2)
{
if (date1.after(date2)) return "date1 is after date2";
if (date1.before(date2)) return "date1 is before date2";
if (date1.equals(date2)) return "date1 is equal to date2";
}
public static void main(String[] args) throws Exception
{
DateTimeComparer dateTimeComparer = new DateTimeComparer();
Date convertedDate = dateTimeComparer.convertDateTime("21/Sep/12 2:01 PM");
Date today = new Date();
String comparedDates = dateTimeComparer.compareDateTimes(today, convertedDate);
println comparedDates;
}
}
-
That doesn't look like groovy.user40980– user409802015年03月09日 15:41:43 +00:00Commented Mar 9, 2015 at 15:41
-
@MichaelT You can verify it at groovyconsole.appspot.comAvetis– Avetis2015年03月09日 15:43:22 +00:00Commented Mar 9, 2015 at 15:43