-2

I'm trying to do a simple IF check of a String that I know has been formatted correctly with:

Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(c.getTime());

At this point formattedDate is "09/24/2015". I can confirm that this is true:

remoteViews.setTextViewText( R.id.widget_date, formattedDate);

It's showing me the correct date. So I know it's good. So I test it:

if (formattedDate=="09/24/2015"){
//do some stuff
}

For whatever reason, that condition is not true, and nothing happens. I have tried everything from removing the slashes, to just changing the format around, but formattedDate never equals what that string clearly is.

Is there something I'm missing here?

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30

1 Answers1

0

Use .equals() to compare Strings. Like this

if (formattedDate.equals("09/24/2015")){
    //do some stuff
}

== tests for "reference equality", so whether a and b are referencing the same object
.equals(anotherObject) tests for "value equality", whether a and b are logically equal

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • Seriously?! (Not that I doubt you, because I've seen that before. But why is that the case? Why does == not work for this? Please elaborate. – durbnpoisn Sep 24 '15 at 17:48
  • @durbnpoisn use your google-fu. – Sterling Archer Sep 24 '15 at 19:09
  • I did. Except I was asking the wrong question. I was running on the assumption that the conversion from the date was the problem. I didn't think it was as simple as using the incorrect operator for comparison. I have like 12 apps working and up on Google Play. And never once had I run into this particular problem. Not once. – durbnpoisn Sep 24 '15 at 19:25