In this tutorial i’m going to show you four different ways to get current date in android.
Note : For doing this we will use Calendar, Date, SimpleDateFormate and System class.
Method (1)
This is a first method and in this method we’ll use Calendar and SimpleDateFormate class.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
String date = sdf.format(c.getTime());
Method (2)
This is a second method and in this method we’ll use Calendar class to get current date in another way.
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)
This is a third method and in this method we’ll use Date and SimpleDateFormate class.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
Method (4)
This is a final method and in this method we’ll use System and SimpleDateFormate class to get current date.
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.