8

I want to get day (Ex Sun, Mon, Tue, ..) from date

def date = new Date()
def day = date[Calendar.DAY_OF_MONTH]

result is 18, but i want to get Mon.

And if I have 2017-10-09T00:00:00.000, I want to get day of 2017-10-09T00:00:00.000

Could you help me please?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Bell Aimsaard
  • 483
  • 3
  • 5
  • 16
  • 1
    Use `Calendar.DAY_OF_WEEK` and map to the appropriate string in `{1: "Sunday", 2: "Monday", ...}`. You can naturally use an array if you offset the index by `-1`. – Reut Sharabani Sep 18 '17 at 08:28
  • You're getting `18` because you've passed `date[Calendar.DAY_OF_MONTH]`. That is today's date. Use `date[Calendar.DAY_OF_WEEK]` instead. – Procrastinator Sep 18 '17 at 08:32
  • if I have 2017-10-09T00:00:00.000, I want to get day of 2017-10-09T00:00:00.000 – Bell Aimsaard Sep 18 '17 at 08:50

4 Answers4

7

The below code can help. It gets today's date first which is 18 Sep 2017 and then thats passed into calendar instance

Calendar instance then get the day in integer form i.e. monday=2, Then once we get the value 2 using map we say its Monday

    def date = new Date()
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    def day = calendar.get(Calendar.DAY_OF_WEEK);


   def map=[
   1:"Sunday",
   2:"Monday",
   3:"Tuesday",
   4:"Wednesday",
   5:"Thursday",
   6:"Friday",
   7:"Saturday"]

   log.info map[day]
Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
3

Try Calendar.DAY_OF_WEEK. This returns an int which you can later map to the respective string.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Tridev Chaudhary
  • 368
  • 3
  • 11
  • If the builds lasts for more than a day will this value also change or is it set once at job start? – Jakub Jabłoński Dec 02 '19 at 09:46
  • @JakubJabłoński, this completely depends on your implementation. The returned int value from Calendar.DAY_OF_WEEK will change but it won't update the exiting variable, hence you will have to recall Calendar.DAY_OF_WEEK – Tridev Chaudhary Dec 03 '19 at 04:51
1

Try using SimpleDateFormat, with EE format which tells days of week.

try below code :

def date = new Date()
SimpleDateFormat("EE").format(date) // EE means days of week
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
1

You can use this to output a three character representation of the name of the week day:

theDate = "2017-10-09T00:00:00.000";
def parsedDate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSS", theDate);
def day = new java.text.SimpleDateFormat("EEE", Locale.ENGLISH).format(parsedDate);

or for the full name use EEEE pattern.

Make sure to specify the Locale as this could differ per server configuration.

ipper
  • 624
  • 4
  • 13
  • And if I have 2017-10-09T00:00:00.000, I want to get day of 2017-10-09T00:00:00.000 – Bell Aimsaard Sep 18 '17 at 08:37
  • 1
    I cannot compile this is error which happen. `Caught: groovy.lang.MissingPropertyException: No such property: �? for class: java.util.Date Possible solutions: day groovy.lang.MissingPropertyException: No such property: �? for class: java.util.Date Possible solutions: day at Test.run(Test.groovy:2)` – Bell Aimsaard Sep 18 '17 at 09:00
  • When I copy it from this page I see extra dots that are not supposed to be there (a dot on second line `theDate)​.;`). Just check that what you have copied corresponds with what is displayed. I tried the code on https://www.tutorialspoint.com/execute_groovy_online.php and there it runs fine... added extra line with `print day;` to see output. – ipper Sep 18 '17 at 12:29
  • Apparently my code snippet contained a hidden character. I removed it and now copy & pasting should work. – ipper Sep 19 '17 at 08:55