Question

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!

Was it helpful?

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

OTHER TIPS

What is the question? How to get date 3 years earlier? Then this code will be useful:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    //set up time to the last minute of today
    c.set(Calendar.HOUR, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);

    long to = c.getTimeInMillis();// end point of period

    c.add(Calendar.YEAR, -1 * yearCount);

    long since = c.getTimeInMillis();//start point of period
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top