scala 转换为字符串_如何在Scala中将字符串转换为日期?

scala 转换为字符串

Scala中的字符串 (String in Scala)

String is a collection of characters that is mutable, i.e. its contents cannot be modified.

字符串是可变字符的集合,即,其内容无法修改。

Syntax for creating string:

创建字符串的语法:

    val string_name : String = "string_value"

Example of string:

字符串示例:

    val string = "Includehelp"

在Scala的日期 (Date in Scala)

Date is a variable that stores the date-time information for the date. It uses the Calendar class present in java calendar library imported using import java.util.calendar.

Date是一个变量,用于存储日期的日期时间信息。 它使用通过import java.util.calendar导入的Java日历库中存在的Calendar类。

在Scala中将字符串转换为日期 (Convert string to date in Scala)

As Scala uses the java-based date library we can use methods in java to convert string to date. So, here we will use the parse method.

由于Scala使用基于Java的日期库,因此我们可以使用Java中的方法将字符串转换为日期。 因此,这里我们将使用parse方法。

Syntax to convert a string to date using parse method:

使用解析方法将字符串转换为日期的语法

    val date = date_format_name.parse(string)

Program to convert string to date

程序将字符串转换为日期

// converting string to date in Scala
import java.text.SimpleDateFormat
import java.util.Date

object MyObject {
    def main(args: Array[String]) {
        val string = "2020-06-20"
        println("String is " + string)
        
        val dateformat = new SimpleDateFormat("yyyy-MM-dd")
        val date = dateformat.parse(string)
        
        println("Converted date is " + date)
    }
}

Output:

输出:

String is 2020-06-20
Converted date is Sat Jun 20 00:00:00 GMT 2020

Explanation:

说明:

Here, we have converted the string to date in Scala. For this, we have used the parse method in simpleDateFormat. After conversion, we have printed the date using the println statement.

在这里,我们已经在Scala中将字符串转换为日期。 为此,我们在simpleDateFormat中使用了parse方法。 转换后,我们使用println语句打印了日期。

翻译自: https://www.includehelp.com/scala/how-to-convert-a-string-to-date-in-scala.aspx

scala 转换为字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值