将字符串 "22082013 "转换为日期格式20130822。

问题描述 投票:-1回答:3

这是我在scala中的代码

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

日期是不会改变的。同日 22082013与第20130822号决议没有变化

如何在scala中把格式ddmmyyyy改为yyyymmdd?

scala date date-format simpledateformat
3个回答
7
投票

你需要定义一个 "简单日期格式"("yyy-mm-dd"); var date:Date = simpleDateFormat.parse(s); println(date)... 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)

2
投票

我试过了,它对我有效。

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))

0
投票

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))
© www.soinside.com 2019 - 2024. All rights reserved.