Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date conversion

Tags:

date

scala

I have a date variable

var date: Date = new Date() 

then I have converted this date to String:

var dateStr = date.toString() 

now I need to convert back this String to date. I have tried both:

1:

   var stringToDate: Date = date2Str.asInstanceOf[Date] 

and 2:

stringToDate: Date = new SimpleDateFormat("dd.MM.yyyy").parse(dateStr); 

But in both case I got the error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date 
like image 226
Echo Avatar asked Mar 21 '11 12:03

Echo


People also ask

How do you convert dates?

Convert text dates by using the DATEVALUE function Select a blank cell and verify that its number format is General. Click the cell that contains the text-formatted date that you want to convert. Press ENTER, and the DATEVALUE function returns the serial number of the date that is represented by the text date.

How do you convert date to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

What is the English date of shrawan?

This year, the month of Sawan will begin from 25 July 2021. Devotees of Lord Shiva consider this month very auspicious and observe it with full enthusiasm and devotion. Sawan, the fifth month of the Hindu calendar, falls during the month of July-August.

How do you calculate Hijri year?

Year Gregorian = Year Muslim × 0.97 + 622. If you want to convert a Gregorian date to a Muslim date, you need to subtract 622 from the original year and then multiply by 1.03: Year Muslim = (Year Gregorian– 622) × 1.03.


1 Answers

I see a couple of problems in your code, but this works fine:

scala> val format = new java.text.SimpleDateFormat("dd-MM-yyyy") format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@9586200  scala> format.format(new java.util.Date()) res4: java.lang.String = 21-03-2011  scala> format.parse("21-03-2011") res5: java.util.Date = Mon Mar 21 00:00:00 CET 2011 
like image 81
Wilfred Springer Avatar answered Sep 20 '22 14:09

Wilfred Springer