3

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

then the String convert into util date..

SearchDate is from method parameter and the value of SearchDate is "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)

please share your answers ..

1 Answer 1

8

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:

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? Or maybe you're passing wrong parameter to parse() ?

edit

It looks like you actually wanted to call:

val date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dat)
Sign up to request clarification or add additional context in comments.

8 Comments

The one you used in the first SimpleDateFormat
how to convert E MMM dd HH:mm:ss Z yyyy format to yyyy-MM-dd HH:mm this format
You first call parse with the first date format, get a date, and then you call format on the second formatter with that date. See the edit.
@Mila Date doesn't have any format information, I assumed that when you want a date in particular format, then you actually want is date formatted as a String using that format.
@Mila you DID that in the fourth line of your code. The only problem there is that IST acronym is ambigous - there are many timezones called IST.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.