In this tutorial I will show you 4 different ways to get current date in android.
For doing this we will use Calendar, Date, SimpleDateFormate and System class.
How to Get Current Date in Android?
Method 1:
In this method we will use Calendar and SimpleDateFormate class.
1 2 3 |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar c = Calendar.getInstance(); String date = sdf.format(c.getTime()); |
Method 2:
We can use Calendar class to get current date in another way.
1 2 3 4 5 |
Calendar c = Calendar.getInstance(); int day = c.get(Calendar.DAY_OF_MONTH); int month = c.get(Calendar.MONTH); int year = c.get(Calendar.YEAR); String date = day + "/" + (month+1) + "/" + year; |
Method 3:
In this method we will use Date and SimpleDateFormate class.
1 2 |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); String date = sdf.format(new Date()); |
Method 4:
Another method in which we will use System and SimpleDateFormate class to get current date
1 2 |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); String date = sdf.format(System.currentTimeMillis()); |
Comment below if you know any other way to get current date in android.
Hi,
What if I want to get a network or internet time as users can change cell phone time manually?