BullShark
BullShark

Reputation: 85

Android getting date

I am trying to use this code I found here on Stackoverflow. It seems to work for many people on Android because many people gave it thumbs up.

However, Eclipse will not let me run it even. It's giving me an error and saying that there is not an empty constructor for Date(). It has 3 other constructors that take arguments, but those seem to be for setting the new Date object to a specific time and date.

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());

Upvotes: 1

Views: 132

Answers (2)

Srikanth Pai
Srikanth Pai

Reputation: 926

First Define a constant for the date format

public static final String DATE_FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss";

The below code will get you date and time stamp in the above format

            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIMESTAMP);
            Calendar cal = Calendar.getInstance();
            String TimeStampSTARTTIME = sdf.format(cal.getTime()); 

Upvotes: 0

Simon Dorociak
Simon Dorociak

Reputation: 33515

However, Eclipse will not let me run it even. It's giving me an error and saying that there is not an empty constructor for Date(). It has 3 other constructors that take arguments, but those seem to be for setting the new Date object to a specific time and date.

Most likely it's is caused because you are imported java.sql.date but you need to use java.util.date

Look at reference of java.sql.Date and java.util.Date

Difference is that java.sql.Date has only two constructors with parameter(s) and java.util.Date also constructor(except others) without parameters - it you are looking for.

So change your import into:

import java.util.Date;

and now it will work.

Upvotes: 2

Related Questions