scala提取字符串中数字_如何在Scala中以字符串或数字的形式获取日期,月份和年份?...

scala提取字符串中数字

The “calendar” class handles working with date and time in Scala, the class generates the current time in the following format,

“ calendar”类处理Scala中的日期和时间 ,该类以以下格式生成当前时间,

    Thu Apr 23 06:10:37 GMT 2020

We can extract different parts like date, month, and year.

我们可以提取日期,月份和年份等不同部分。

计划在Scala中获取日期 (Program to get date in Scala)

import java.util.Calendar
import java.text.SimpleDateFormat

object MyClass {
   
    def main(args: Array[String]) {
   
        val cal = Calendar.getInstance
        val dateTime = cal.getTime
        
        println("Full date information : " + dateTime)
        
        val dateFormat = new SimpleDateFormat("dd")
        val date = dateFormat.format(dateTime)
        println("Date is : " + date)
        
        val dateFormat2 = new SimpleDateFormat("MMM")
        val month = dateFormat2.format(dateTime)
        println("Month is : " + month)
        
        val dateFormat3 = new SimpleDateFormat("YYYY")
        val year = dateFormat3.format(dateTime)
        println("Year is : " + year)
    }
}

Output

输出量

Full date information : Fri Apr 24 16:59:22 GMT 2020
Date is : 24
Month is : Apr
Year is : 2020

提取月份号 (Extracting month number)

In Scala, we can extract the string in the form of the number instead of a string. The date format provides an option for this too.

在Scala中,我们可以提取数字形式的字符串而不是字符串。 日期格式也为此提供了一个选项。

The format “MM” does our work.

格式“ MM”完成了我们的工作。

程序将月份提取为数字 (Program to extract month as a number)

import java.util.Calendar
import java.text.SimpleDateFormat

object MyClass {
   
    def main(args: Array[String]) {
   
        val cal = Calendar.getInstance
        val dateTime = cal.getTime
        
        val dateFormat = new SimpleDateFormat("MM")
        val month = dateFormat.format(dateTime)
        
        println("Month number is : " + month)
    }
}

Output

输出量

Month number is : 04

格式化月份字符串 (Formatting month string)

We can use string formatting methods like toUpperCase and toLowerCase to extract month as an uppercase string or lowercase string.

我们可以使用诸如toUpperCase和toLowerCase之类的字符串格式化方法来将month提取为大写或小写字符串。

Program:

程序:

import java.util.Calendar
import java.text.SimpleDateFormat

object MyClass {
   
    def main(args: Array[String]) {
   
        val cal = Calendar.getInstance
        val dateTime = cal.getTime
        
        val dateFormat = new SimpleDateFormat("MMM")
        val month = dateFormat.format(dateTime).toUpperCase
        println("Month is : " + month)
        
        val Lmonth = dateFormat.format(dateTime).toLowerCase
        println("Month is : " + Lmonth)
    }
}

Output

输出量

Month is : APR
Month is : apr


翻译自: https://www.includehelp.com/scala/how-to-get-date-month-and-year-as-string-or-number-in-scala.aspx

scala提取字符串中数字

    原文作者:cumudi0723
    原文地址: https://blog.csdn.net/cumudi0723/article/details/107802254
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞