繁体   English   中英

将字符串“22/08/2013”​​转换为日期格式 2013/08/22

[英]Convert String “22/08/2013” to date format 2013/08/22

这是我在 Scala 中的代码

 var s:String ="22/08/2013"
 var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
 var  date:Date = simpleDateFormat.parse(s);
 println(date)

日期不变。 日期格式与22/08/2013相同,2013/08/22没有变化

如何在scala中将格式dd/mm/yyyy更改为yyyy/mm/dd

您需要定义一个SimpleDateFormat ,它首先解析您的字符串并从中获取Date 然后将其转换为任何格式。

 var s:String ="22/08/2013"
 var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
 var  date:Date = simpleDateFormat.parse(s);
 val ans = new SimpleDateFormat("yyyy/mm/dd").format(date) 
 println(ans)

我试过这个对我有用。

val s: String = "22/08/2013"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("yyyy/mm/dd")
println(df.format(date))

John Vishal 的回答中有一个微妙的错误:mm 将 08 定义为分钟,而不是月。 改用MM。

更正的代码:

val s = "2013_08_14"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy_MM_dd")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("dd-MMM-yyyy")
println(df.format(date))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM