How to replace getDate() from the type Date which is deprecated?

Yuri picture Yuri · Sep 21, 2015 · Viewed 9.7k times · Source

I have an existing program that I have to correct . It contains these lines :

        Date startDate = new Date();
        int day = startDate.getDate() - 1;

but getDate() from the type Date is deprecated so i have to change it using Calender. I tried this :

Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.DATE, -1);
int day= startDate.getTime();

but this results into following error :

Type mismatch: cannot convert from Date to int

Answer

FullStack picture FullStack · Sep 21, 2015

One more good option is to use like :

System.out.println(DateFormat.getDateInstance().format(new Date()));

It will print the current date.

If you need time along with date then you can use like :

System.out.println(DateFormat.getDateTimeInstance().format(new Date()));