这是我在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?
你需要定义一个 "简单日期格式"("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)
我试过了,它对我有效。
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))