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());
    }

}
Nuruddin
  • 23
  • 2
  • 7
  • 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](https://docs.oracle.com/javase/tutorial/datetime/) instead: `LocalDate.of(year, month, day)`. – Ole V.V. Mar 24 '19 at 10:24
  • By *VS* do you mean Visual Studio?? Could you show us your import statement? – Ole V.V. Mar 24 '19 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. – JussiV Mar 24 '19 at 10:25
  • 3
    Don't name your class `Calendar` when you what do use `java.util.Calendar`! – Edwardth Mar 24 '19 at 10:30
  • Easy and modern way: `System.out.println(LocalDate.of(year, month, day).getDayOfWeek());`. Prints for example `SUNDAY`.. – Ole V.V. Mar 24 '19 at 13:16

1 Answers1

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.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • 1
    Also calling the Java until class with full name will work, like so: `java.util.Calendar c = java.util.Calendar.getInstance(); ...` – JussiV Mar 24 '19 at 11:56