如何在Scala中将String转换为日期时间? [英] How to convert String to date time in Scala?

查看:804
本文介绍了如何在Scala中将String转换为日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala的新手,最近几天我已经学到了它。我对Scala的日期从字符串转换有疑问。

I am new to Scala and I've learned it for the last few days. I have a doubt about Scala's date converting from string.

如何使用Scala将String转换为日期和时间?

How to convert String to Date and time using Scala ?

它的工作方式与Java相同吗?

Does it work the same way as Java?

当我这样做时,出现编译器错误,我无法将其转换为Scala,因为我是是Scala的新手。而且我知道将Java转换为Scala是不好的...

When I do it that way, I get a compiler error and I can't convert that into Scala because I am new to Scala. And I know that it's not good to convert Java to Scala...

请分享您的答案。

推荐答案

如果您正在使用Java的util.Date,则就像在Java中一样:

If you are using Java's util.Date then like in java:

val format = new java.text.SimpleDateFormat("yyyy-MM-dd")
format.parse("2013-07-06")

用于格式化的文档- SimpleDateFormat

,或者如果您使用的是joda的DateTime,则调用解析方法:

or if you are using joda's DateTime, then call parse method:

DateTime.parse("07-06-2013")

docs-DateTime

这篇关于如何在Scala中将String转换为日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆