Search code examples
androiddatetimedatetime-generation

Get data between last three years,


I'm try to load data between lasst three years. i want to get data between current datetime and last 3 years back.

eg:-FROM 1/19/2009 TO current time (1/19/2012)

I get the current time as below.

String date= DateFormat.getDateInstance().format(new Date());

Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!


Solution

  • You can use Calender's add()

            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Log.d("current date",dateFormat.format(cal.getTime()));
            cal.add(Calendar.YEAR, -3);
            Log.d("3 years previous date",dateFormat.format(cal.getTime()));
    

    OUTPUT

    01-19 05:34:52.148: DEBUG/current date(556): 19/01/2012
    01-19 05:34:52.148: DEBUG/3 years previous date(556): 19/01/2009