scala 隨機生成整數
In Scala programming language, there is an option for the programmer to use libraries of java because of its interoperability with java.
在Scala編程語言中,程序員可以選擇使用Java庫,因為它可以與Java互操作。
There are a few methods to extract the date of a program,
有幾種方法可以提取程序的日期 ,
1)Java 8年 (1) Java 8 Year)
The java.time.year library is used to get the value of the current year.
java.time.year庫用于獲取當前年份的值 。
Syntax:
句法:
val data_int = Year.now.getvalue
Program:
程序:
import java.time.Year
object MyClass {
def main(args: Array[String]) {
val year: Int = Year.now.getValue;
printf(" "+year)
}
}
Output
輸出量
2019
2)Java 8 localDate (2) Java 8 localDate )
The java.time.localDate library is used to get the current date and we can extract year also using its method.
java.time.localDate庫用于獲取當前日期 ,我們也可以使用其方法提取年份 。
Synatx:
Synatx:
val data_int = LocalDate.now.getvalue
Example:
例:
import java.time.LocalDate
object MyClass {
def main(args: Array[String]) {
val year: Int = LocalDate.now.getYear;
printf(" "+year)
}
}
Output
輸出量
2019
3)Java日歷 (3) Java 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.
java.util.calendar用于使用getInstance方法獲取日歷信息 。 并將Calendar.YEAR傳遞給它將返回Year的值。
Synatx:
Synatx:
val year = Calendar.getInstance.get(Calendar.YEAR)
Example:
例:
import java.util.Calendar
object MyClass {
def main(args: Array[String]) {
val year: Int = Calendar.getInstance.get(Calendar.YEAR);
printf(" "+year)
}
}
Output
輸出量
2019
翻譯自: https://www.includehelp.com/scala/how-to-get-the-current-year-as-an-integer-in-scala.aspx
scala 隨機生成整數