如何在 Android Studio 中为时间选择器设置正确的上午/下午时间?

问题描述

我正在尝试根据一些示例城市及其各自的时区创建一个时间转换器。我正在检索 UTC 中的当前时间,然后根据每个时区的 UTC 偏移量添加或减去,然后添加或减去 12 以将时间转换为其相应的 12 小时格式,因此它可以是 am 或下午。然后,当用户从微调器中选择一个城市时,此信息会显示在 TimePicker 上。

现在的问题是我得到了正确的小时,​​但对于某些时区,am\pm 是倒退的。例如,我的本地时间是 EST,我想转换为 PST。假设现在是晚上 7 点,我想知道洛杉矶的时间。显示的是凌晨 4 点,而不是下午 4 点。

所以我在“纠正”时间方面遇到了麻烦。我使用了 .HOUR_OF_DAY ,我认为它应该考虑夏令时,我尝试使用 HOUR 但这并不能解决问题,只会将时间设置回一小时。 需要使用 12 小时的校正数学将 24 小时格式转换为 12 小时,但这并不像我预期的那样工作,因为正如我所提到的,虽然它确实设置为正确的小时,​​但它没有考虑根据实际时间选择正确的上午/下午。 此外,.setIs24HourView 设置为 false。

无论如何,这是处理此功能的函数:

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;
    }

这是它在应用程序中的显示方式,用于可视化: Time Converter

编辑:抱歉,我也应该添加这个。所以偏移量返回了“转换因子”,我在旋转器的 onItemSelected 事件中使用了它。因此,当用户从微调器中选择一个项目时,此函数会读取条目,并根据偏移值设置时间(即小时和分钟也是如此,但这是静态设置的,因为它始终会返回正确的分钟) :

 @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);
  }

还有我的主要内容:

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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)