Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String to date time in Scala?

Tags:

datetime

scala

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.

like image 319
Prasanth A R Avatar asked Jun 08 '13 05:06

Prasanth A R


People also ask

How do I convert a string to a date?

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.

How do you write dates in Scala?

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.

How do I cast a date on Spark?

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.


1 Answers

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

like image 168
4lex1v Avatar answered Oct 07 '22 21:10

4lex1v