Month as integer


(John) #1

I’m trying to write an app and select a month as an integer. I.e. January = 1 and June = 6. What is the easiest way to do this?


(Tim Slagle) #2

Can you describe what you are planning on doing? If i can better understand what the goal is I can help you get what you need :slight_smile:


(John) #3

Something along these lines:

section(“Configuration”) {
input “startMonth”, “enum”,
title: “Start on which month?”,
required: true,
multiple: false,
options: [
‘January’,
‘February’,
‘March’,
‘April’,
‘May’,
‘June’,

Then I have int currentMonth = localCalendar.get(Calendar.MONTH);

Ultimate goal is if currentMonth > startMonth {
dosomethingfancy()
}

I’ll have to edit this reply and clean it up from my computer tomorrow, but hopefully that helps.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

Many different ways to do this. Depends if you want a Groovy / Java expert to give the most efficient answer or want to experiment in order to learn and think creatively.

One way off the top of my head… One way I’ve used frequently… Change your enum choices to include numeric values that you can pull off the string and then convert to integer for the compare.

ie, “01 January“ or “6:June” …


(Brice; SmartRulesApp.com) #5

I believe Date.parse() should be able to do this for you - http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html#parse(java.lang.String,%20java.lang.String)


(Mike Maxwell) #6

wasn’t or isn’t there an enum option with a form similar to the following?
options: [ [1,‘January’],[2,‘Feburary’],… ] which displays the text in the device options, but returns the int values in the preference variable?
I’ve seen this in some code someplace, but have since not been able to find that example or play with this too much…
Currently I end up doing something like this:

Options: ["one","two","three"]
Then:
def prefInt
switch (settings.prefStr) {
		case "one":
			prefInt= 1
                        break
		case "two":
			prefInt= 2
                        break
                case "three":
			prefInt= 3
                        break
		default:
			prefInt= 1
                        break
	}

(Tim Slagle) #7

Should be able to add this to your enum options

options: ["january":1, "february":2, "march":3]

(Mike Maxwell) #8

yea baby!, that’s what I was looking for!


(John) #9

So with that it made my selection display as 1, 2, 3. I want it to display “January, February, March”. I flipped it around to [1:“January”,…] and got this error: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer @ line 213 which is:

if ((endMonth > startMonth && currentMonth > startMonth…

However, the debugger returns 6 for startMonth (june) and 8 for currentMonth (august).


(Mike Maxwell) #10

Cast it to an integer endMonth.toInteger()…


(John) #11

When I did that it was trying to make “january” an integer. This was my solution:

int startMonth=Integer.parseInt(startMonth.replaceAll("[\D]", “”));

now I am having other issues… 99 little bugs in the code, take 1 down, patch it around, 127 little bugs in the code…


(John) #12

For others looking for a solution, I also just found:

def thresholdValue = threshold as int