RSS

Code to retrieve current Date and Time in android

18 Jun

In Java, you can get the current date time via following two classes – Date and Calender. And, later use SimpleDateFormate class to convert the date into user friendly format.


package com.nikshit;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class retrieveCurrentDateTime {
public static void main(String[] args) {

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));

//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

}
}

Output:
2012/05/18 19:12:17

Advertisements
 
Leave a comment

Posted by on June 18, 2012 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: