-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

closed as unclear what you're asking by user40980, Dan Pichelman, gnat, user53019, Kilian Foth Mar 16 '15 at 14:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Could you edit the question and let us know what you have already tried to do. The more details the better. – AvetisG Mar 9 '15 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 Mar 9 '15 at 14:14
  • 2
    Hi. Please check what is on-topic here and consider asking at stackoverflow – Benni Mar 9 '15 at 14:43
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;
        }
   }

​ ​

Not the answer you're looking for? Browse other questions tagged or ask your own question.