-3

I'm newbies in Java and Android Studio also. I'm studying the both of the first answers of this question : Stack Overflow Question : Display the current Times

I reproduced the fist answer so no problem until the second answer with this code :

        import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);

            Calendar c = Calendar.getInstance();
            System.out.println("Current time => "+c.getTime());

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = df.format(c.getTime());
            // formattedDate have current date/time
            Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();

            // Now we display formattedDate value in TextView
            TextView txtView = new TextView(this);
            txtView.setText("Current Date and Time : "+formattedDate);
            txtView.setGravity(Gravity.CENTER);
            txtView.setTextSize(20);
            setContentView(txtView);
        }

    }

When I'm trying to make the project, I have got the following error :

Error:(21, 9) error: cannot find symbol class SimpleDateFormat 

I don't how know how to declare the symbol as Android Studio is proposing several issue that are not working for me. Thanks.

dubis
  • 378
  • 1
  • 7
  • 22

2 Answers2

3

just write below to your java file;

import java.text.SimpleDateFormat;
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • Right but that was strange because when I typed the combo `Alt+ Enter` Android Studio didn't propose to import class. It proposed something different, and now if I'm deleting the import line it's proposing the import class. That could be a refresh problem of the Studio Dev. – dubis Aug 11 '17 at 13:30
0

just add this import:

import java.text.SimpleDateFormat;

this will sure fix it