0

How to display weekdate in single textview.

I have a following code as below. which show week date as a string in logcat.

    Calendar c = Calendar.getInstance();

    // Set the calendar to tuesday of the current week
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.add(Calendar.DATE, week * 5);
    c.add(Calendar.WEEK_OF_YEAR, week); //February
    // Print dates of the current week starting on Monday
    SimpleDateFormat df = new SimpleDateFormat("E dd yyyy", Locale.US);

    for (int i = 0; i < 5; i++) {

        //System.out.println(df.format(c.getTime()));
        String string= df.format(c.getTime());

        Log.d("", "MY YEAR OF MONTH=="+string); //[TUE 25 2014, WED 26 2014, THU 27 2014, FRI 28 2014, SAT 01 2014]
        c.add(Calendar.DAY_OF_MONTH, 1);
    //But this textview show only last date(sat 01 2014).
       textView.setText(string);

MY question is how to show all week dates in single texview. above textView.setText(string); show only last date, but i want whole week date in single textview.

Thanks

user3283148
  • 101
  • 4
  • 13
  • I don't fully understand the question: You want to display all seven days of the week as a long string of text in a `TextView` ? Is it necessary to use a `Calendar` and a `SimpleDateFormat`? It seems like you could simply get the info you need then make a call to `String.format()` and set that as the text in your view. – Eric Dand Feb 25 '14 at 18:24
  • Yes you are right i want to display all seven days of the week as a long string of text in a TextView .would you like to send me a code please – user3283148 Feb 25 '14 at 18:29
  • How do you want it formatted, and what days ought to go into it? – Eric Dand Feb 25 '14 at 18:32
  • e.g right from this week [MON 24 2014,TUE 25 2014 WED 26 2014, THU 27 2014, FRI 28 2014, SAT 01 2014], because in my project i want to compare each date in week with some type of text. – user3283148 Feb 25 '14 at 18:36

1 Answers1

0

You can use two approaches. One, use the method append() and the second you can concatenate previously added value in the string variable with the new one.

Ex: append()

for (int i = 0; i < 5; i++) {
        String string= df.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
       textView.append(string);
}

Ex 2: Concatenate

Place the String variable outside the for loop and use +=

String string = "";
for (int i = 0; i < 5; i++) {
    string += df.format(c.getTime());
    c.add(Calendar.DAY_OF_MONTH, 1);      
}
textView.setText(string);
Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48
  • Thanks for your help dear. i have one little bit confusion related to my project. i have a main linearLayout and other is item layout having textview of week dates, now i am inflating this item(date) in main linearlayout, but how can i show all week date in form of list. using texview.setText(string); so that it will show all week day when i add this linearlayout with CustomView – user3283148 Feb 25 '14 at 18:55
  • @user3283148 That wasn't the question you asked in the first place. Anyway, why don't you use a ListView? P.S. Do accept the answer if it has helped you in anyway. – Joel Fernandes Feb 26 '14 at 07:07