I am new to Scala and I've learned it for the last few days. I have a doubt about Scala's date converting from string.
How to convert String to Date and time using Scala ?
Does it work the same way as Java?
When I do it that way, I get a compiler error and I can't convert that into Scala because I am new to Scala. And I know that it's not good to convert Java to Scala...
Please share your answer.
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
In scala we do not have any date-time library or function available, we can use java data time, calendar, Date library to deal with dates in Scala. By using these functions, we can convert our date to a string and vice versa, also we can format our date.
Spark to_date() – Convert String to Date format to_date() – function is used to format string ( StringType ) to date ( DateType ) column. Below code, snippet takes the date in a string and converts it to date format on DataFrame.
If you are using Java's util.Date then like in java:
val format = new java.text.SimpleDateFormat("yyyy-MM-dd") format.parse("2013-07-06")
docs for formating - SimpleDateFormat
or if you are using joda's DateTime, then call parse method:
DateTime.parse("07-06-2013")
docs - DateTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With