Get Date and Time in Android

In Android, to get time and date you have to first create the object of calender then you can format it using simpledateformat class according to your need. At last it returns you a date and time as per your format in String return type.

Here is the sample code for getting date and time as string.

Code :

public String getDateTime()	{
	Calendar c = Calendar.getInstance(); 
	SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
	return df.format(c.getTime());
}
Advertisements

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