# integer

## Kotlin: A for loop that counts up to some maximum integer value

As a brief note to self, I was just converting some Java code to Kotlin, and the correct way to convert this Java `for` loop that uses `i` as a counter:

``for (int i=0; i<tabLayout.tabCount; i++) { ...``

is with this Kotlin `for` loop:

``for (i in 0 until tabLayout.tabCount) { ...``

The key there for me is the `0 until` part of the syntax.

## How to get the current year as an integer in Scala

Scala FAQ: How do I get the current year as an integer (`Int` value) in Scala?

Solution: Use the Java 8 `Year` or `LocalDate` classes, or the older old Java `Calendar` class. The solutions are shown below.

## How to create a Java or Scala date from a Long value

Scala FAQ: How do I created a Scala `Date` from a `Long` value? When I give the Java Date class constructor a `Long` value it returns a date from 1970.

Solution: You need to multiply the `Long` value by 1000, and also make sure you pass a `Long` value into the `Date` constructor. The Scala REPL shows how this works:

## A Java method to determine if an integer is between a range

Last night I was writing some code for my Android football game, and decided it would be best if I had a `between` method, so I could write some code like this to show that I wanted to test to see if a number was between an integer range:

`if (between(distance, 8, 10)) { ...`

That could would be interpreted as, “If the `distance` is between the values `8` and `10`, do whatever is in the code block.” (I can make that code more readable in Scala, but in Java I think that’s the best I can do.)

## Java: How to round a float or double to an integer

Java math FAQ: How do I round a float or double to an integer in Java?

Solution: Use `Math.round()` to round a `float` (or `double`) to the nearest integer (`int`) in Java.

You can see how this works in the examples that follow, where the result is shown in the comment after each line: