简体   繁体   English

Scala字符串到Java Util日期转换

[英]Scala String to Java Util Date Conversion

Hi the Conversion of String to Date is not possible here.. 您好,此处无法将字符串转换为日期。

I search and use several methods but the error can not change.. 我搜索并使用了几种方法,但错误无法改变..

here the date format is var q and convert that to formatedDate that is String 这里的日期格式是var q并将其转换为formatedDate ,即String

then the String convert into util date.. 然后将String转换为util日期..

SearchDate is from method parameter and the value of SearchDate is "Thu Aug 29 00:00:00 IST 2013" SearchDate来自方法参数 ,SearchDate的值为“Thu Aug 29 00:00:00 IST 2013”

    var q: Date = SearchDate
    var dateStr = q.toString()
    var formatter: DateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy")
    var dat = formatter.parse(dateStr)
    var cal: Calendar = Calendar.getInstance()
    cal.setTime(dat)
    var formatedDate = cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR)
    println("formatedDate : " + formatedDate)
    val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateStr)// Error Occured Here..

the error is 错误是

Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Aug 29 00:00:00 IST 2013" at java.text.DateFormat.parse(Unknown Source) 线程“main”中的异常java.text.ParseException:Unparseable date:“Thu Aug 29 00:00:00 IST 2013”​​at java.text.DateFormat.parse(Unknown Source)

please share your answers .. 请分享你的答案..

You're trying to parse "E MMM dd HH:mm:ss Z yyyy" formatted date string as "yyyy-MM-dd HH:mm" formatted one, in this line: 您试图将"E MMM dd HH:mm:ss Z yyyy"格式化的日期字符串解析为"yyyy-MM-dd HH:mm"格式化的一行,在此行中:

val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateStr)

No wonder it fails. 难怪它失败了。

Maybe you took the wrong date format for second SimpleDateFormat instance by mistake? 也许您错误地为第二个SimpleDateFormat实例采用了错误的日期格式? Or maybe you're passing wrong parameter to parse() ? 或者你可能将错误的参数传递给parse()

edit 编辑

It looks like you actually wanted to call: 看起来你真的想打电话:

val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dat)

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

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