0



I have a String ArrayList filled with dates + time. The format is: "11 04 2017 - 12:45:54"

I would like to change the format so it only shows the date: "11 04 2017" and save it to another array.

How can i cut the last few characters of an array?

package com.example.hodor.stringcut;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.EditText;

    import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private EditText textView, textView1;


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

    textView = (EditText) findViewById(R.id.editText);
    textView1 = (EditText) findViewById(R.id.editText1);

    ArrayList<String> listDate = new ArrayList<>();
    ArrayList<String> listDate1 = new ArrayList<>();

    listDate.add("10 - 07 - 2017 - 12:32:45");
    listDate.add("10 - 07 - 2017 - 12:35:45");
    listDate.add("10 - 07 - 2017 - 12:36:45");
    listDate.add("10 - 07 - 2017 - 12:38:45");
    listDate.add("10 - 07 - 2017 - 12:30:45");

    textView.setText(listDate[1]);

    for (int i = 0; i < listDate.length; i++) {

        listDate1[i] = listDate[i].substring(0, listDate[i].length(), -10);
        textView.setText(listDate1[1]);
    }
}

}

Anybody can help me please?
Roxby
  • 3
  • 2

2 Answers2

0

Try this. It works for me

package com.example.hodor.stringcut;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    private EditText textView, textView1;

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

        textView = (EditText) findViewById(R.id.editText);
        textView1 = (EditText) findViewById(R.id.editText1);

        ArrayList<String> listDate = new ArrayList<>();
        ArrayList<String> listDate1 = new ArrayList<>();

        listDate.add("10 - 07 - 2017 - 12:32:45");
        listDate.add("10 - 07 - 2017 - 12:35:45");
        listDate.add("10 - 07 - 2017 - 12:36:45");
        listDate.add("10 - 07 - 2017 - 12:38:45");
        listDate.add("10 - 07 - 2017 - 12:30:45");

        textView.setText(listDate.get(1));

        for (int i = 0; i < listDate.size(); i++) {
            String date = listDate.get(i);
            listDate1.add(date.substring(0, date.length() -11));

        }
        textView.setText(listDate1.get(1));
    }
}
NIKHIL NEDIYODATH
  • 2,703
  • 5
  • 24
  • 30
0

Your initial substring(0, length - 11) concept was correct. You're simply having a problem with types.

listDate is of type ArrayList. The substring method isn't applicable for it. It's applicable for its elements which are String types.

You're also mixing up the ArrayList type with java primitive arrays. Here's some example of how to utilize an ArrayList.

But once you comfortable with that, this code...

public static void main(String[] args) {
    String date = "10 - 07 - 2017 - 12:30:45";
    String modifiedDate = date.substring(0, date.length() - 10);
    System.out.println(modifiedDate);
}

Gives an output of...

10 - 07 - 2017

At a higher level, if what you're trying to do is parse dates then the link Brandon shared will be much more apt.

Staros
  • 3,232
  • 6
  • 30
  • 41