Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Here’s my code below:

public class Question

    public static void main(String args[])

    {

        int year;    

        Calendar current = Calendar.getInstance(); //Creating the calendar class object

        year = current.get(Calendar.YEAR); //Initializing year with calendar class object

        System.out.println ("Current Year: "+year); // Printing year

    } 

}

The above code runs when I import java.util.Calendar. I want this to run without having to import java.util package.

How to use the calendar class in Java without importing any package? 

1 Answer

0 votes
by (19.7k points)

You can use the fully qualified class name in Java. It’s the package name followed by class name separated by a period. 

 

public class Question

    public static void main(String args[])

    {

        int year;    

        java.util.Calendar current = java.util.Calendar.getInstance(); //to create the calendar class object

        year = current.get(java.util.Calendar.YEAR); //initialize  year with calendar class object

        System.out.println ("Current Year: "+year); //to print the year

    } 

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 20, 2021 in Java by rahulnayar01123 (6.1k points)

Browse Categories

...