Android – Get Current Date and Time

By -
Problem: How to get current current date and time in Android?
Description:

For fetching current date and time, we use Calendar class to get the current instance of system clock of phone:

Calendar c = Calendar.getInstance();

Once we are having object of Calendar, we can retrieve date/time value in any desirable format, say for example
yyyy-MM-dd HH:mm:ss, now to convert in this format we use SimpleDateFormat.

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

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();

Download Full Example here: Android get current Date and Time.

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...