Java Show Strike Line by Method

Why does it show the hit string on getDate()

, getMonth()

and getYear()

. These methods are used to get the current date, month and year, but I don't know why it shows a strike on these methods.

code:

public class hello {

    public static void main(String[] args) {
        int days;
        int month;
        int year;

        days = 24;
        month = 10;
        year = 1994;

        System.out.println("Date of Birth: " + days + "/" + month + "/" + year);

        Date d = new Date();

        int t = d.getDate();
        int x = d.getMonth() + 1;
        int f = d.getYear() + 1900;

        System.out.println("Current Date: " + t + "/" + x + "/" + f);
    }
}

      

+3


source to share


3 answers


IDEs like Eclipse will remove methods if they are deprecated, meaning they are not recommended for use because there is a better alternative. See the JavadocsgetDate()

:

Obsolete. Since JDK version 1.1, it is replaced by Calendar.get(Calendar.DAY_OF_MONTH)

.



Using methods Calendar

:

Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);

      

+4


source


This is because they are outdated. If you set @deprecated in the above function information, it will hit methods in most IDEs.



These specific features are deprecated because the newer Calendar

is the better option.

+2


source


Try it.

        int days;
    int month;
    int year;

      days=24;
      month=10;         
      year=1994;

        System.out.println("Date of Birth: "+days+ "/" +month+ "/" +year);

        LocalDate dd = LocalDate.of(year, month, days);

    System.out.println("Current Date: " + dd);
    System.out.println("Month: " + dd.getMonth());
    System.out.println("Day: " + dd.getDayOfMonth());
    System.out.println("Year: " + dd.getYear());

    //If you would add year
    LocalDate newYear = dd.plusYears(10);
    System.out.println("New Date: " + newYear);

      

This is output:

Date of birth: 24/10/1994

Current date: 1994-10-24

Month: OCTOBER

Day: 24

Year: 1994

New date: 2004-10-24

0


source







All Articles