-1

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

asked Mar 9, 2015 at 14:05
3
  • 1
    Could you edit the question and let us know what you have already tried to do. The more details the better. Commented 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. Commented Mar 9, 2015 at 14:14
  • 2
    Hi. Please check what is on-topic here and consider asking at stackoverflow Commented Mar 9, 2015 at 14:43

1 Answer 1

2

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;
 }
 }

​ ​

answered Mar 9, 2015 at 15:34
2
  • That doesn't look like groovy. Commented Mar 9, 2015 at 15:41
  • @MichaelT You can verify it at groovyconsole.appspot.com Commented Mar 9, 2015 at 15:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.