-2

I am new to programming and I am try to learn java by myself , using the free to use materials available in the internet . And I was writing a simple if else code. Which I do not know why it is not working .

Code :

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class if_else_if_else{
    public static void main(String[] args) {
        Date date=new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String dayWeekText = new SimpleDateFormat("EEEE").format(date);

        System.out.println("'" + dayWeekText + "'");

        if (dayWeekText == "Monday"){
            System.out.println("It is Monday . ");
        }
        else if (dayWeekText == "Tuesday"){
            System.out.println("It is Tuesday . ");
        }
        else if (dayWeekText == "Wednesday"){
            System.out.println("It is Wednesday . ");
        }
        else if (dayWeekText == "Thrusday"){
            System.out.println("It is Thrusday . ");
        }
        else if (dayWeekText == "Friday"){
            System.out.println("It is Friday . ");
        }
        else if (dayWeekText == "Saturday"){
            System.out.println("It is Saturday . ");
        }
        else if (dayWeekText == "Sunday"){
            System.out.println("It is Sunday . ");
        }
        else {
            System.out.println("Something is wrong . ");
        }
    }
}

Please help me .

  • 2
    Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels May 21 '22 at 21:29
  • 1
    In your situation, consider using a switch block instead as it seems to me that it would be a cleaner construct for this particular code. Otherwise, if you need to use if blocks, then change `if (dayWeekText == "Monday"){` to `if (dayWeekText.equals"Monday")){` – Hovercraft Full Of Eels May 21 '22 at 21:29
  • 1
    And consider changing from Date and Calendar to the newer and easier to use Java "Date Time API" – Hovercraft Full Of Eels May 21 '22 at 21:32
  • 1
    e.g., `LocalDate localDate = LocalDate.now(); String dayOfWeek = localDate.format(DateTimeFormatter.ofPattern("EEEE")); System.out.println("It is " + dayOfWeek);` So no need for switch block or if/else blocks. – Hovercraft Full Of Eels May 21 '22 at 21:39
  • 1
    Or even better, in one line of code: `System.out.println("It is " + LocalDate.now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.US));` – Hovercraft Full Of Eels May 21 '22 at 21:49
  • 1
    I strongly recommend you don’t use `Date`, `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the last one in particular notoriously troublesome. So nothing that you should learn about. Instead learn and use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) as demonstrated in the good comments by @HovercraftFullOfEels. – Ole V.V. May 22 '22 at 13:06

0 Answers0