3

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!

TRS
  • 490
  • 2
  • 11
  • 29
  • if you want data from database then execute query for it and other then tell me. – Newts Jan 19 '12 at 06:33
  • are you facing problem with formatting or db query.. Pl. specify – Vinay Jan 19 '12 at 06:36
  • Thankz Newts.I want to take the current and last dates and after set to textviews. – TRS Jan 19 '12 at 06:37
  • thankz vinay. how can i load backdate(3 years ago) to text view? minus from current date? if yes how can i do this? – TRS Jan 19 '12 at 06:39

2 Answers2

3

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
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

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
Jin35
  • 8,602
  • 3
  • 32
  • 52