1

I have been stuck on this issue for the last two days. My issue is this: how can I display the date from one week to another week (Thursday to Thursday)? For example:

1/30/2014 to 2/6/2014

or

30 jan 2014 to 6 feb 2014 

when week is complete then it's change Like:

2/6/2014 to 2/13/2014

or

6 feb 2014 to 13 feb 2014

Any help or sample code will be highly appreciated.

An SO User
  • 24,612
  • 35
  • 133
  • 221
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

3 Answers3

1

try this,

String start_date = "01-30-2014";  // Start date
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                    Calendar cal = Calendar.getInstance();
                    try {
                        c.setTime(sdf.parse(start_date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    cal.add(Calendar.DATE, 7);  // number of days to add,in your case its 7
                    SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
                    String to_date = sdf1.format(cal.getTime()); 

Actually i don't like android Calendar( I prefer Joda-Time) but above solution should work for you

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
dd619
  • 5,910
  • 8
  • 35
  • 60
  • I am sorry, how is this related to `Android` ? =) – An SO User Jan 31 '14 at 06:37
  • I am talking about the calendar instance used in the code,Calendar API is very confusing. – dd619 Jan 31 '14 at 06:39
  • Well, you could write your answer to complement mine by showing the OP how to get the time of next week as `long` =) – An SO User Jan 31 '14 at 06:41
  • well,i don't know about `setMaxDate()` but we can set any date to calendar instance and retrieve time in milliseconds as `long MiliTime=cal.getTimeInMillis()`. – dd619 Jan 31 '14 at 06:46
  • That's it then, bro ! `cal.getTimeInMillis()`'s return gets passed to `setMaxDate()`. Good as done ! =) – An SO User Jan 31 '14 at 06:49
1

Finally i got a solution and solve my problem:

in oncreate:

TextView tv_chart_menuvotes = (TextView) findViewById(R.id.tv_chart_menuvotes);
String csPrevThur = getPreviousThursday();
    String csNextThur = getNextThursday();
    tv_chart_menuvotes.setText("Vote from " + csPrevThur + " To "+ csNextThur);

outside the oncreate:

public String getPreviousThursday() {
    String csDate = "";
    int perSut = 0;
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    switch (day) {
    case Calendar.SUNDAY:
        perSut = -3;
        break;
    case Calendar.MONDAY:
        perSut = -4;
        break;
    case Calendar.TUESDAY:
        perSut = -5;
        break;
    case Calendar.WEDNESDAY:
        perSut = -6;
        break;
    case Calendar.THURSDAY:
        perSut = 0;
        break;
    case Calendar.FRIDAY:
        perSut = -1;
        break;
    case Calendar.SATURDAY:
        perSut = -2;
        break;
    }

    SimpleDateFormat mDF = new SimpleDateFormat("dd-MM-yyyy");
    calendar.add(Calendar.DAY_OF_MONTH, perSut);
    csDate = mDF.format(calendar.getTime());

    System.out.println("Prev Thursday >> " + csDate);

    return csDate;
}

public String getNextThursday() {
    String csDate = "";
    int perSut = 0;
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    switch (day) {
    case Calendar.SUNDAY:
        perSut = 4;
        break;
    case Calendar.MONDAY:
        perSut = 3;
        break;
    case Calendar.TUESDAY:
        perSut = 2;
        break;
    case Calendar.WEDNESDAY:
        perSut = 1;
        break;
    case Calendar.THURSDAY:
        perSut = 7;
        break;
    case Calendar.FRIDAY:
        perSut = 6;
        break;
    case Calendar.SATURDAY:
        perSut = 5;
        break;
    }

    SimpleDateFormat mDF = new SimpleDateFormat("dd-MM-yyyy");
    calendar.add(Calendar.DAY_OF_MONTH, perSut);
    csDate = mDF.format(calendar.getTime());

    System.out.println("NextThursday >> " + csDate);

    return csDate;
}
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
0

In your case, setMaxDate() is your friend. The docs say:

Sets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

So, get the time of the next week as long and use the method.

Have a look here Set Limit on the DatePickerDialog in Android?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221