Java 根据时区字符串列表中的GMT时间对时区进行排序
我正在尝试根据GMT时间在用户首选项页面中显示时区。 我陷入困境的地方是,我正试图根据字母顺序的GMT时间对这个时区进行排序。像Java 根据时区字符串列表中的GMT时间对时区进行排序,java,sorting,timezone,Java,Sorting,Timezone,我正在尝试根据GMT时间在用户首选项页面中显示时区。 我陷入困境的地方是,我正试图根据字母顺序的GMT时间对这个时区进行排序。像 (GMT+3:00) Indian/Mayotte (GMT+3:00) W-SU (GMT+3:30) Asia/Tehran (GMT+3:30) Iran (GMT+4:00) Asia/Baku (GMT+4:00) Asia/Dubai (GMT+4:00) Asia/Muscat (GMT+4:00) Asia/Tbilisi (GMT+4:00) Asi
(GMT+3:00) Indian/Mayotte (GMT+3:00) W-SU (GMT+3:30) Asia/Tehran
(GMT+3:30) Iran (GMT+4:00) Asia/Baku (GMT+4:00) Asia/Dubai
(GMT+4:00) Asia/Muscat (GMT+4:00) Asia/Tbilisi (GMT+4:00)
Asia/Yerevan (GMT+4:00) Etc/GMT-4 (GMT+4:00) Europe/Samara
(GMT+4:00) Indian/Mahe (GMT+4:00) Indian/Mauritius (GMT+4:00)
Indian/Reunion (GMT+4:00) NET (GMT+4:30) Asia/Kabul (GMT+5:00)
Antarctica/Mawson (GMT+5:00) Asia/Aqtau
我现在使用comparator得到的结果是,它仍然没有根据字符串中的时间正确排序:
我需要根据字母数字和-+运算符进行排序,比如-10、-9、-1,0,1,2,3。。。有人能帮忙吗
public class ComboDropDownItem {
private String key;
private String value;
public ComboDropDownItem(String key, String value) {
this.key = key;
this.value = value;
}
public String getValue() {
return value;
}
public String getKey() {
return key;
}
}
public class Timezoner {
public static void main(String args[]) {
List<ComboDropDownItem> comboDropDownItem = new ArrayList<>();
String[] ids = TimeZone.getAvailableIDs();
for (String id : ids) {
comboDropDownItem.add(
new ComboDropDownItem(TimeZone.getTimeZone(id).getID(),
getTimeZone(TimeZone.getTimeZone(id))));
}
Collections.sort(comboDropDownItem,
new Comparator<ComboDropDownItem>() {
public int compare(ComboDropDownItem s1, ComboDropDownItem s2) {
return s1.getValue().compareToIgnoreCase(s2.getValue());
}
}); // Need to sort the GMT timezone here after getTimeZone() method call
for (ComboDropDownItem instance : comboDropDownItem) {
System.out.println(instance.getValue());
}
}
private static String getTimeZone(TimeZone tz) {
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
- TimeUnit.HOURS.toMinutes(hours);
minutes = Math.abs(minutes);
String result = "";
if (hours > 0) {
result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());
} else if (hours < 0) {
result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());
} else {
result = String.format("(GMT) %s", tz.getID());
}
return result;
}
}
公共类ComboDropDownItem{
私钥;
私有字符串值;
公共ComboDropDownItem(字符串键、字符串值){
this.key=key;
这个值=值;
}
公共字符串getValue(){
返回值;
}
公共字符串getKey(){
返回键;
}
}
公共类时区器{
公共静态void main(字符串参数[]){
List comboDropDownItem=新建ArrayList();
字符串[]id=TimeZone.getAvailableIDs();
用于(字符串id:ids){
comboDropDownItem.add(
新建ComboDropDownItem(TimeZone.getTimeZone(id).getID(),
getTimeZone(TimeZone.getTimeZone(id));
}
Collections.sort(comboDropDownItem,
新比较器(){
公共整数比较(ComboDropDownItem s1、ComboDropDownItem s2){
返回s1.getValue().compareToIgnoreCase(s2.getValue());
}
});//在调用getTimeZone()方法后,需要在此处对GMT时区进行排序
for(ComboDropDownItem实例:ComboDropDownItem){
System.out.println(instance.getValue());
}
}
私有静态字符串getTimeZone(时区tz){
long hours=时间单位.millizes.toHours(tz.getRawOffset());
long minutes=TimeUnit.millizes.toMinutes(tz.getRawOffset())
-时间单位。小时。至分钟(小时);
分钟=Math.abs(分钟);
字符串结果=”;
如果(小时数>0){
结果=String.format(((GMT+%d:%02d)%s),小时,分钟,tz.getID();
}否则如果(小时<0){
结果=String.format(((GMT%d:%02d)%s),小时,分钟,tz.getID();
}否则{
结果=String.format(“(GMT)%s”,tz.getID());
}
返回结果;
}
}
首先使用的原始偏移量对时区
值进行排序,例如
List<TimeZone> comboDropDownItem = new ArrayList<>();
String[] ids = TimeZone.getAvailableIDs();
for (String id : ids) {
comboDropDownItem.add(TimeZone.getTimeZone(id));
}
Collections.sort(comboDropDownItem,
new Comparator<TimeZone>() {
public int compare(TimeZone s1, TimeZone s2) {
return s1.getRawOffset() - s2.getRawOffset();
}
}); // Need to sort the GMT timezone here after getTimeZone() method call
for (TimeZone instance : comboDropDownItem) {
System.out.println(getTimeZone(instance) + instance.getID());
}
这是一种执行方法:
private ArrayList<String> allTimeZone=new ArrayList<String>;
String[] options = TimeZone.getAvailableIDs();
for (int i = 0; i < options.length; i++) {
allTimeZone.add(options[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, allTimeZone);
sp_select_timezone.setAdapter(adapter);
sp_select_timezone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
for (int i = 0; i < allTimeZone.size(); i++) {
if (allTimeZone.get(i).contains((CharSequence) parentView.getItemAtPosition(position))) {
// City_id = allCitiesItems.get(i).getCityId();
Log.e("TimeZone", " " + allTimeZone.get(i));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
private ArrayList allTimeZone=new ArrayList;
String[]options=TimeZone.getAvailableIDs();
对于(int i=0;i
private ArrayList<String> allTimeZone=new ArrayList<String>;
String[] options = TimeZone.getAvailableIDs();
for (int i = 0; i < options.length; i++) {
allTimeZone.add(options[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, allTimeZone);
sp_select_timezone.setAdapter(adapter);
sp_select_timezone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
for (int i = 0; i < allTimeZone.size(); i++) {
if (allTimeZone.get(i).contains((CharSequence) parentView.getItemAtPosition(position))) {
// City_id = allCitiesItems.get(i).getCityId();
Log.e("TimeZone", " " + allTimeZone.get(i));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});