Why does "Date d = new Date();" return an error?
11,753
You are probably using java.sql.Date
. You want to be using java.util.Date
.
Author by
Brandon Englert
Updated on June 11, 2022Comments
-
Brandon Englert almost 2 years
This has probably been asked and answered a million times, but I can't seem to find a solution anywhere. Upon starting an activity in an android app, I want to display the current date and time. From what I understand the date part can be done simply with the following:
Date d = new Date(); d.getTime(); CharSequence s = DateFormat.format("EEEE, MMMM d, yyyy", d.getTime()); TextView date = (TextView)findViewById(R.id.dateText); date.setText(s); TextView time = (TextView)findViewById(R.id.timeText); time.setText(s);
In eclipse it gives me an error and says that the constructor date is undefined. I chose the auto fix option and it added a 0 as a parameter in the Date constructor. This produced a date, but the date is Dec. 31, 1969. What am I missing here?
This is probably trivial, but I'm still new to this stuff.
Thanks in advance for any advice.
-
Thane Anthem about 13 yearsThis is indeed probably why the compiler is complaining about requiring a parameter to the date constructor.
-
Brandon Englert about 13 yearsChanging to java.util.Date worked. I need to re-format the date so I may post again if I can't get that figured out pretty quick. Thanks for your help
-
musaul about 13 yearsNo worries. This page shows the date formats you can use ... developer.android.com/reference/android/text/format/… - Also, pressing the 'tick' next to my block would be nice. :)
-
Alastair Pitts about 13 years@Brandon Englert: If this was the correct answer, don't forget to mark this as the accepted answer (the tick) :)
-
Shashanth over 7 yearsIs there any much deference between yours answer and @musaul 's answer?
-
Rav over 7 yearsNope and why will there be? After all that's the correct answer, and I've quite a few times faced this same situation which Brandon did and the solution I've given worked for me. So, I posted my reply.