-2

I have this program that will change the hours and minutes of the values I get from Calendar.

So I'm changing only the hour, I'm doing a Timezone thing here. So, what I do is I make an array of the TimeZones at Strings.xml and put it on a spinner. And then, when I change the item on the spinner, I set the text of a textview to the selected value on the spinner.

I can do it up to here.

My problem lies in the conditional statements. I have a button that gets the text in the TextView and I will use that in my If statements. This is my Syntax.

This gets me the values from the Spinner to the TextView.

    Spinner TimezoneSelect = (Spinner)findViewById (R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.timzones, R.layout.support_simple_spinner_dropdown_item);
    TimezoneSelect.setAdapter(adapter);
    //final String SelectedTimeZone = TimezoneSelect.getSelectedItem().toString();
    TimezoneSelect.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            TimeZoneStatus = parent.getItemAtPosition(position).toString();
            TimeZoneDisplay.setText(TimeZoneStatus);

And this is the faulty If statement.

        public void onClick(View v) {
        // TODO Auto-generated method stub
        int newhour;
        String TimeZoneNow = TimeZoneStatus.trim().toString();
        String Jakarta = "UTC+7:00 (Jakarta)";
        if  ((TimeZoneNow == "UTC+7:00(Jakarta)") || (TimeZoneNow == Jakarta)) 
        //^lol desperate code
        {
            newhour = hour - 1;
            TimeText.setText(newhour + ":" + minutes);
        }

    }

   });

Help! :c

  • [Comparing Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pararth Oct 17 '14 at 04:50

3 Answers3

1

try this way

if(TimeZoneNow.equals(Jakarta)

used .equals() method for string comparison

M D
  • 47,665
  • 9
  • 93
  • 114
0

Try this:

In your case replace == with eqauls().

Explanation:

== operator is used to compare reference.

equals() is used to compare content.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

use this code

 if  ((TimeZoneNow .equalsIgnoreCase(Jakarta)) 
    //^lol desperate code
    {
        newhour = hour - 1;
        TimeText.setText(newhour + ":" + minutes);
    }
Meenal
  • 2,879
  • 5
  • 19
  • 43