Brandon Englert
Brandon Englert

Reputation: 161

Why does "Date d = new Date();" return an error?

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.

Upvotes: 5

Views: 12381

Answers (3)

Rav
Rav

Reputation: 3

Import java.util.Date and your problem will be resolved.

Upvotes: 0

Thane Anthem
Thane Anthem

Reputation: 4093

From http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html for constructor Date(long time):

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Instead, take a look at http://developer.android.com/reference/android/os/SystemClock.html

Upvotes: 0

musaul
musaul

Reputation: 2341

You are probably using java.sql.Date. You want to be using java.util.Date.

Upvotes: 38

Related Questions