0

I want to display the current date as the first position in a TextView form consecutive 3 months in a list as (current,previous, next). How can I do this?

    private void CalculateDates() {

    //to calculate the date I've used this function
      DateRange = new ArrayList<String>();
      boolean loopVal = true;
      SimpleDateFormat formatter = new SimpleDateFormat(" E ddMMMyyyy");
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.MONTH, -1);

    //textView is the TextView view that should display it
        String StartDate = formatter.format(calendar.getTime());
        calendar.add(Calendar.MONTH, 2);
        String EndDate = formatter.format(calendar.getTime());
        DateRange.add(StartDate);
        Date date = null;
        try {
                while (loopVal) {
                    date = formatter.parse(StartDate);

                    Calendar cal = Calendar.getInstance();

                    cal.setTime(date);
                }
        }catch(Exception e){

        }   
   }
lakshmi
  • 21
  • 3
  • 9
  • 1
    what do you want is not clear. –  Aug 04 '16 at 10:24
  • I just want to display the current date from the consecutive 3 months like(previous, current,next)they are in a list – lakshmi Aug 04 '16 at 10:44
  • Possible duplicate of [how to get a list of dates between two dates in java](http://stackoverflow.com/questions/2689379/how-to-get-a-list-of-dates-between-two-dates-in-java) – Basil Bourque Aug 06 '16 at 00:55
  • Please search Stack Overflow before posting. This has been covered many times already. – Basil Bourque Aug 06 '16 at 00:55

1 Answers1

1

You can try like this I do for you.

Edit Like this

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tView = (TextView) findViewById(R.id.textView1);

    ArrayList<String> threDate = get3Dates();
    //tView.setText(threDate.toString());
    String dates = "";

    for (int i = 0; i < threDate.size(); i++) {
        dates+= threDate.get(i).toString()+" ";
    }

    tView.setText(dates);
}


private ArrayList<String> get3Dates(){

    SimpleDateFormat formatter = new SimpleDateFormat(" E dd MMM yyyy");

    ArrayList<String> three_date = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance();

    //Current Date
    String current_date = formatter.format(calendar.getTime());
    three_date.add(current_date);//only date
    //three_date.add("Present Date: "+current_date);//or with label 

    //Previous Date
    calendar.add(Calendar.MONTH, -1);
    String previous_date = formatter.format(calendar.getTime());
    three_date.add(previous_date);//only date
    //three_date.add("Previous Date: "+previous_date);//or with label

    //Future Date
    calendar.add(Calendar.MONTH, 2);
    String future_date = formatter.format(calendar.getTime());
    three_date.add(future_date);//only date
    //three_date.add("Next Date: "+future_date);//or with label

    return three_date;
}

}

And Layout xml like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
   </HorizontalScrollView>
</LinearLayout>

I do another example for you. Here you can Click Next Previous to get Next and previous date. Layout xml like below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Navigable(Next-Priv) Example"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="10" >

    <TextView
        android:id="@+id/tvPrev"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.5"
        android:gravity="left|center_vertical"
        android:text="&lt;&lt;"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7"
        android:gravity="center_vertical|center_horizontal"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvNext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.5"
        android:gravity="right|center_vertical"
        android:text=">>"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="Simple TextView with Scroll:"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
</HorizontalScrollView>

Java code in activity class

public class MainActivity extends ActionBarActivity {


TextView tvPriv, tvDate, tvNext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvPriv = (TextView) findViewById(R.id.tvPrev);
    tvDate = (TextView) findViewById(R.id.tvDate);
    tvNext = (TextView) findViewById(R.id.tvNext);

    final SimpleDateFormat formatter = new SimpleDateFormat(" E dd MMM yyyy");
    final Calendar calendar = Calendar.getInstance();
    String curent_date = formatter.format(calendar.getTime());
    tvDate.setText(curent_date);


    tvPriv.setOnClickListener(new View.OnClickListener(){
          @Override
          public void onClick(View v){
              calendar.add(Calendar.MONTH, -1);
              String prev_date = formatter.format(calendar.getTime());
              tvDate.setText(prev_date);
          }
      });



    tvNext.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            calendar.add(Calendar.MONTH, +1);
            String next_date = formatter.format(calendar.getTime());
            tvDate.setText(next_date);
        }
    });



    //Simple textView example
    TextView tView = (TextView) findViewById(R.id.textView1);

    ArrayList<String> threDate = get3Dates();
    //tView.setText(threDate.toString());
    String dates = "";

    for (int i = 0; i < threDate.size(); i++) {
        dates+= threDate.get(i).toString()+" ";
    }

    tView.setText(dates);


   }



private ArrayList<String> get3Dates(){

    SimpleDateFormat formatter = new SimpleDateFormat(" E dd MMM yyyy");

    ArrayList<String> three_date = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance();

    //Current Date
    String current_date = formatter.format(calendar.getTime());
    three_date.add(current_date);//only date
    //three_date.add("Present Date: "+current_date);//or with label 

    //Previous Date
    calendar.add(Calendar.MONTH, -1);
    String previous_date = formatter.format(calendar.getTime());
    three_date.add(previous_date);//only date
    //three_date.add("Previous Date: "+previous_date);//or with label

    //Future Date
    calendar.add(Calendar.MONTH, 2);
    String future_date = formatter.format(calendar.getTime());
    three_date.add(future_date);//only date
    //three_date.add("Next Date: "+future_date);//or with label

    return three_date;
}

}
eddie
  • 1,252
  • 3
  • 15
  • 20
  • Hi Haque,Thanks for your reply but, this is not exactly matches with my question. Actually I have a list of dates for 3 months displayed in a textview , in that I want to display the current date at the first position for the previous and upcoming dates and all I'll scroll with the help of scroll bars to see. – lakshmi Aug 05 '16 at 11:12
  • Horizontally I just want to scroll the dates like a calendar, in that I want to have the present date as the first one. thank u.. – lakshmi Aug 05 '16 at 11:23
  • which view(TextView, LIstView etc) you want to use? Do you want to show it in one page? –  Aug 05 '16 at 11:30
  • You want scroll with what? –  Aug 05 '16 at 14:34
  • I'm using a gridview in that I want to scroll. – lakshmi Aug 08 '16 at 04:27
  • have you solve your problem? what do you mean by scroll? do you mean swipe the grid left or right or really scroll? –  Aug 08 '16 at 05:20