I'm trying to create a time converter based on some sample cities and their respective time zones. I'm retrieving the current time in UTC, then adding or subtracting according to the offset from UTC for each timezone, then I'm adding or subtracting 12 to convert the time to its corresponding 12-hour format so it could be either am or pm. Then this information is showed on a TimePicker when the user selects a city from a spinner.
Now the problem is that I'm getting the correct hour, but for some time zones the am\pm is backwards. So for example, my local time is EST, and I want to convert to PST. Let's say it's 7pm, and I want to know the time in LA. Instead of showing 4pm, it shows 4am.
So I'm having trouble "correcting" the times. I used .HOUR_OF_DAY which I think is supposed to account for Daylight Saving, and I tried using HOUR but that wouldn't solve the problem and would just set the time back one hour. The corrective math with the twelve hours is needed to convert the 24-hour format to 12 -hour, but this doesn't work as I intended because, as I mentioned, while it does set to the correct hour, it doesn't account for the right am/pm according to the actual time. Also, .setIs24HourView is set to false.
Anyway, here's the function that takes care of this functionality:
public int convertTime(String city)
{
//Result of taking in the UTC time and adding/subtracting the offset
int offset = 0;
//gets the calender instance of time with GMT standard, then getting hour of day
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
int UTC = c.get(Calendar.HOUR_OF_DAY);
//set offset according to city
switch(city)
{
case "New York":
offset = UTC-4;
break;
case "London":
offset = UTC+1;
break;
case "Los Angeles":
offset = UTC-7;
break;
case "Dubai":
offset= UTC+4;
break;
case "Paris":
offset = UTC+2;
break;
case "Moscow":
offset = UTC+3;
break;
case "Cairo":
offset = UTC+2;
break;
case "Hong Kong":
offset = UTC+8;
break;
case "Beijing":
offset = UTC+8;
break;
case "New Delhi":
offset= UTC+5;
break;
case "Mexico City":
offset = UTC-5;
break;
case "Brasilia":
offset = UTC-3;
break;
}
//if the offset is in the AM
if(offset < 12)
{
//set am
offset = offset+12;
}
//if the offset is in the PM
else if(offset > 12)
{
//set pm
offset = offset-12;
}
else
//its twelve o'clock
offset = 12;
return offset;
}
And here's how it would appear in the app, for visualization: Time Converter
EDIT: Sorry, I should have added this as well. So the offset is returning the "conversion factor", which I used in the onItemSelected event for the spinner. So when the user selects an item from the spinner, this function reads the entry, and sets the time according to the offset value (that is, the hour, and minute too, but that's statically set since it will always return the correct minute):
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(convertSpinner.getSelectedItemPosition() == 0)
{
//display current/local time
int hour = c.get(Calendar.HOUR_OF_DAY);
convertTime.setHour(hour);
//currentTime.setHour(hour);
}
else if(convertSpinner.getSelectedItemPosition()== 1)
convertTime.setHour(conversionFactory("New York"));
else if(convertSpinner.getSelectedItemPosition()== 2)
convertTime.setHour(conversionFactory("London"));
else if(convertSpinner.getSelectedItemPosition()== 3)
convertTime.setHour(conversionFactory("Los Angeles"));
//... same processs for the other cities, shortened for obvious reasons
else
convertTime.setHour(12);
//set the minute
int minute = c.get(Calendar.MINUTE);
convertTime.setMinute(minute);
}
Also here's my main:
private TimePicker currentTime, convertTime;
private Spinner convertSpinner;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View v= inflater.inflate(R.layout.fragment_time, container, false);
convertTime = v.findViewById(R.id.convert_clock);
convertSpinner = v.findViewById(R.id.convert_spinner);
convertTime.setIs24HourView(false);
convertTime.setClickable(false);
//for convert spinner
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(getActivity(),R.array.time_cities, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
convertSpinner.setAdapter(adapter2);
convertSpinner.setOnItemSelectedListener(this);
return v;
}