Rusty Shackleford
Rusty Shackleford

Reputation: 5

Android Studio: "Cannot resolve 'getDateInstance'" error when using DateFormat

I'm trying to create an application that involves prompting the user with a DatePickerDialog and outputting the selected date in the surrounding activity, as per this tutorial: https://www.youtube.com/watch?v=KZoijdM4DsY.

Here's the class in which the error is occurring:

package com.example.austin.fitlog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class IndexActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

    TextView txtDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        setTitle("Home");
        txtDate = (TextView) findViewById(R.id.txtDate);
    }

    public void onClickChangeDate(View view) {
        DatePickerFragment datePickerFragment = new DatePickerFragment();
        datePickerFragment.show(getSupportFragmentManager(), "datePicker");
    }

    private void setDate(final Calendar calendar) {
        final DateFormat dateFormat = new DateFormat.getDateInstance(DateFormat.MEDIUM);
        txtDate.setText(dateFormat.format(calendar.getTime()));
    }

    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        setDate(calendar);
    }

    public static class DatePickerFragment extends DialogFragment {
        public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
        }
    }
}

I'm getting a "Cannot resolve symbol 'getDateInstance'" error in the following line:

final DateFormat dateFormat = new DateFormat.getDateInstance(DateFormat.MEDIUM);

I'm at a loss as to why this is happening. The method getDateInstance even appears in the autofill values but turns red immediately after being typed.

I've provided the correct import (java.text.DateFormat) and I've even tried running 'Invalidate Caches / Restart' as well as modifying my dependencies.

Here's my current build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.austin.fitlog"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.+'
}

If anyone could help resolve this, it would be greatly appreciated.

Upvotes: 0

Views: 1307

Answers (1)

Matt Clark
Matt Clark

Reputation: 28619

Simply remove the new keyword.

final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);

The method getDateInstance is a static method that returns an Object, not an object that you are constructing.

Upvotes: 1

Related Questions