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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18502962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:36:31  来源:igfitidea点击:

Convert String "22/08/2013" to date format 2013/08/22

scaladatedate-formatsimpledateformat

提问by Prasanth A R

This is my code in scala

这是我在 Scala 中的代码

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

the date is not change. the format of date same as 22/08/2013there is no change to 2013/08/22

日期不变。格式日期相同22/08/2013存在于2013年8月22日没有变化

How to change the format dd/mm/yyyy to yyyy/mm/dd in scala

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

回答by Jatin

You need to define a SimpleDateFormatwhich first parses your string and get Datefrom it. And then convert it to whatever format.

您需要定义一个SimpleDateFormat首先解析您的字符串并从中获取的 a 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)

回答by Vishal John

I tried this it works for me.

我试过这个对我有用。

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