• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java对ArrayList<String>的时间排序

武飞扬头像
洛风白羊
帮助1

####### 此文仅仅是自己日常代码问题的一个记录而已,仅仅写给自己。

背景描述

由于项目需要,需要对时间进行降序排序,找了很多种方法,最后发现Collections.sort 这种方法竟然是非常好用的。
起初用Collections.sort(sat_start_time.get(k),Collections.reverseOrder());这个方法对时间排序,发现始终是乱序。最后采用代码里面的方法,Collections.sort 方法改写,顺利解决时间降序排序问题。

package ellip.dt.missionplanning.order;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class temp {
    public static void main(String[] args) {
        //产生一个列表,存放卫星和时间
        ArrayList<ArrayList<String>> station_sat=station_time_list();

        for (int i = 0; i < station_sat.size(); i  ) {
            System.out.println("第" i "地面站对卫星的时间窗口");
            System.out.println("排序前的时间=============" station_sat.get(i));

            //采用这种方法对时间进行降序排序,对方法改写。因为第一个是卫星,不参与排序,所以截取了从1到最后的字符串            Collections.sort(station_sat.get(i).subList(1,station_sat.get(i).size()),new Comparator<String>(){
                @Override
                public int compare(String o1,String o2) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建日期转换对象:年月日 时分秒,时间窗格式化
                    Date value2 = null;
                    Date value1 = null;
                    try {
                        value1 = sdf.parse(o1);//对字符串形式的时间进行格式化
                        value2 = sdf.parse(o2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return value2.compareTo(value1);
                }
            });
            System.out.println("降序排序后的时间排序结果===" station_sat.get(i));
        }

    }

    //构造卫星和时间,形成一个array
    public static ArrayList<ArrayList<String>> station_time_list(){
        ArrayList<ArrayList<String>> station_list_array = new ArrayList<>();
        ArrayList<String> station_time1 = new ArrayList<>();
        station_time1.add("sat1");
        station_time1.add("2022-10-16 4:33:51");
        station_time1.add("2022-10-16 4:42:51");
        station_time1.add("2022-10-16 7:33:51");
        station_time1.add("2022-10-16 7:42:51");
        station_time1.add("2022-10-16 10:33:51");
        station_time1.add("2022-10-16 10:42:51");
        station_list_array.add(station_time1);
        ArrayList<String> station_time2 = new ArrayList<>();
        station_time2.add("sat2");
        station_time2.add("2022-10-16 4:35:51");
        station_time2.add("2022-10-16 4:42:51");
        station_time2.add("2022-10-16 7:35:51");
        station_time2.add("2022-10-16 7:42:51");
        station_time2.add("2022-10-16 12:35:51");
        station_time2.add("2022-10-16 12:42:51");
        station_list_array.add(station_time2);

        return station_list_array;
    }



}

学新通

结果一目了然

第0地面站对卫星的时间窗口
排序前的时间=============[sat1, 2022-10-16 4:33:51, 2022-10-16 4:42:51, 2022-10-16 7:33:51, 2022-10-16 7:42:51, 2022-10-16 10:33:51, 2022-10-16 10:42:51]
降序排序后的时间排序结果===[sat1, 2022-10-16 10:42:51, 2022-10-16 10:33:51, 2022-10-16 7:42:51, 2022-10-16 7:33:51, 2022-10-16 4:42:51, 2022-10-16 4:33:51]
第1地面站对卫星的时间窗口
排序前的时间=============[sat2, 2022-10-16 4:35:51, 2022-10-16 4:42:51, 2022-10-16 7:35:51, 2022-10-16 7:42:51, 2022-10-16 12:35:51, 2022-10-16 12:42:51]
降序排序后的时间排序结果===[sat2, 2022-10-16 12:42:51, 2022-10-16 12:35:51, 2022-10-16 7:42:51, 2022-10-16 7:35:51, 2022-10-16

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhacjej
系列文章
更多 icon
同类精品
更多 icon
继续加载