Home »
Code Examples »
Scala Code Examples
Scala - How do I get the current month as an integer or as a string? Code Example
The code for How do I get the current month as an integer or as a string?
import java.util.Calendar
import java.text.SimpleDateFormat
object Example {
def main(args: Array[String]) = {
// Getting the current month as an integer value
val monthAsInt = Calendar.getInstance.get(Calendar.MONTH)
println(monthAsInt)
// Getting the current month as a three-character string
val dateTime = Calendar.getInstance.getTime
val dateFormat = new SimpleDateFormat("MMM")
val mon = dateFormat.format(dateTime)
println(mon)
}
}
/*
Output:
7
Aug
*/
Code by IncludeHelp,
on August 13, 2022 21:20