0

I am getting error for cannot find symbol

getInstance(),Calendar.DATE,Calendar.MONTH,Calendar.YEAR

This only occurs when I am running offline on VS Code. On running in on an online IDE (Hacker Rank), I get compilation successful. JDK 11 on desktop, JDK 8 on Hacker Rank. I have tried running it on multiple IDEs and get compilation successful only on JDK 8

import java.util.*;
import java.lang.*;
import java.io.*;

class Calendar {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int day = sc.nextInt();
        int month = sc.nextInt();
        int year = sc.nextInt();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, day);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.YEAR, year);

        System.out.println(c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, new Locale("en", "US")).toUpperCase());
    }

}
5
  • 1
    I recommend you don’t use Calendar. That class is poorly designed and long outdated. For your use you probably need LocalDate from java.time, the modern Java date and time API instead: LocalDate.of(year, month, day). Commented Mar 24, 2019 at 10:24
  • By VS do you mean Visual Studio?? Could you show us your import statement? Commented Mar 24, 2019 at 10:25
  • 1
    Can you reduce your code to a minimum that still reproduces the error and then include full source code? It sounds a lot like you're missing an import but can't be sure. Commented Mar 24, 2019 at 10:25
  • 3
    Don't name your class Calendar when you what do use java.util.Calendar! Commented Mar 24, 2019 at 10:30
  • Easy and modern way: System.out.println(LocalDate.of(year, month, day).getDayOfWeek());. Prints for example SUNDAY.. Commented Mar 24, 2019 at 13:16

1 Answer 1

2

Your problem is that you have named your class Calendar, and then are trying to use a system class named Calendar. Calling Calendar.getInstance() is failing to compile because the compiler is looking for a method named getInstance() to be defined in YOUR Calendar class. Name your class something else, and I think your code will compile and work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Also calling the Java until class with full name will work, like so: java.util.Calendar c = java.util.Calendar.getInstance(); ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.