×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

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

By IncludeHelp Last updated : November 15, 2024

In Scala programming language, there is an option for the programmer to use libraries of java because of its interoperability with java.

There are a few methods to extract the date of a program,

Using java.time.year

The java.time.year library is used to get the value of the current year.

Syntax

val data_int = Year.now.getvalue

Example

import java.time.Year

object MyClass {
    def main(args: Array[String]): Unit = {
        val year: Int = Year.now.getValue; 
        printf(" "+year)
    }
}

Output

2019

Using java.time.localDate

The java.time.localDate library is used to get the current date and we can extract year also using its method.

Syntax

val data_int = LocalDate.now.getvalue

Example

import java.time.LocalDate

object MyClass {
    def main(args: Array[String]): Unit = {
        val year: Int = LocalDate.now.getYear; 
        printf(" "+year)
    }
}

Output

2019

Using java.util.calendar

The java.util.calendar is used to to get calendar information using the getInstance method. And passing the Calendar.YEAR to it will return the value of Year.

Syntax

val year = Calendar.getInstance.get(Calendar.YEAR)

Example

import java.util.Calendar

object MyClass {
    def main(args: Array[String]): Unit = {
        val year: Int = Calendar.getInstance.get(Calendar.YEAR); 
        printf(" "+year)
    }
}

Output

2019

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.