Nikunj Patel
Nikunj Patel

Reputation: 22066

System.currentTimeMillis() give wrong output android

when i am print System.currentTimeMillis()

give me :

11-03 14:47:05.400: INFO/System.out(7579): date is :: 14475410111

What is the correct procedure to get entire date with time.?

Upvotes: 5

Views: 4034

Answers (5)

Paresh Mayani
Paresh Mayani

Reputation: 128428

To get current date and time in Android, You can use:

Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());

Output:

Current time => Thu Nov 03 15:00:45 GMT+05:30 2011

FYI, once you have this time in 'c' object, you can use SimpleDateFormat class to get date/time in desired format.

Final Solution:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sring formattedDate = df.format(c.getTime());     // c is Calendar object
System.out.println("========> formatted date => "+formattedDate);

Output: ========> formatted date => 2011-11-03 15:13:37

Upvotes: 5

teoREtik
teoREtik

Reputation: 7916

Use SimpleDateFormat

Upvotes: 0

Reno
Reno

Reputation: 33792

Date now = new Date(System.currentTimeMillis());

By the way : currentTimeMillis()

Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.

Upvotes: 3

Dave Newton
Dave Newton

Reputation: 160191

Use the Date class.

(What makes you think that's wrong? You're asking for the time in milliseconds.)

Upvotes: 0

Dinesh Prajapati
Dinesh Prajapati

Reputation: 9510

Yes This is the correct way to get the current time and date. afte that you need to convert it to desired format.

public static String getDateFromTimeStamp(String timeStamp){
        return new Date(Long.parseLong(timeStamp)).toString();
    }

Upvotes: 2

Related Questions