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:

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

package com.example.austin.fitlog;

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;

    protected void onCreate(Bundle savedInstanceState) {
        txtDate = (TextView) findViewById(;

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

    private void setDate(final Calendar calendar) {
        final DateFormat dateFormat = new DateFormat.getDateInstance(DateFormat.MEDIUM);

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

    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: ''

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ''

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