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.