欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java_有不同年份的温度数据文件,要求日期按年月升序、温度按降序 将温度数据排列出来

程序员文章站 2022-03-08 17:36:22
...
package experiment5.exp2;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*有不同年份的温度数据temperature.txt,要求日期按年月升序、温度按降序 将温度数据排列出来。(注意是年月,日不参于比较)
*比较日期查看:https://blog.csdn.net/xuchaoxin1375/article/details/109540645
* File file = new File("weather.txt");
Scanner scanner = new Scanner(file);
*/
public class Temperature {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        String s;
        File file = new File("D:\\OneDrive\\Java\\ProjectCodeInIDEA\\src\\experiment5\\exp2\\weather.txt");
        try {
            Scanner scanner = new Scanner(file);//读取数据
            //主体部分:
            /*对于nextLine()方法    :
            * 返回:the line that was skipped
遇错误 抛出:
java.util.NoSuchElementException – if no line was found
IllegalStateException – if this scanner is closed*/
            for(s=scanner.nextLine();scanner.hasNextLine();s=scanner.nextLine()){
                list.add(s);//尽管程序在运行过程中出错,但是能够执行的部分还是执行了
            }/*至此,成功读入数据到list中*/
            /*下面开始做解析和比较处理*/
            Comparator<String> comparator = new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    /*用compareTo()比较需要注意日期的格式要求.*/
                    int ret=(o1.substring(0, 7)+"-00").compareTo(o2.substring(0, 7)+"-00");
                    if(ret==0){
                        return Integer.parseInt(o2.substring(20, 22))-Integer.parseInt(o1.substring(20, 22));
                    }
                    return ret;
                }

             /*   @Override
                public boolean equals(Object obj) {
                    return false;
                }*/
            };
            Collections.sort(list,comparator);
            /*处理打印效果*/
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");/*string_date_format*/
            for(String x:list){
                try {
                    Date sd=sdf.parse(x.substring(0,19));/*根据sdf日期格式对象来调用parse()解析表达日期的字符串,返回一个日期对象sd(string->date)*/
                   // System.out.println(x.substring(0, 10)+" "+x.substring(20, 22)+".0");
                    System.out.println(sdf.format(sd)+"\t"+x.substring(20, 22)+".0");/*调用日期格式对象sdf的format()方法(该方法会返回一个字符串)来格式化日期对象sd*/
                    /*java.text.DateFormat @NotNull 
                    public final String format(java.util.Date date)
                    参数:
                    date – the time value to be formatted into a date-time string.
                    返回:
                    the formatted date-time string.
*/
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

测试数据:weather.txt
2015-10-01 14:21:08 34c
2015-10-02 14:01:33 36c
2016-01-01 11:21:06 32c
2016-10-01 12:01:12 37c
2017-12-01 12:11:58 23c
2016-10-02 12:21:03 41c
2016-10-03 12:24:22 27c
2017-07-01 12:31:51 41c
2017-07-02 12:11:02 43c
2017-07-03 12:05:03 44c
2015-06-01 10:21:01 35c
2015-06-05 13:01:33 33c
2015-06-04 11:11:08 32c
2015-06-02 13:07:14 33c
2016-08-11 10:41:25 39c
2016-08-07 15:24:52 36c
2017-09-12 11:16:47 40c
2017-09-03 14:29:53 38c
2017-09-10 16:01:12 37c
2017-09-01 10:25:43 36c
2018-01-01 01:30:18 10c
2018-01-02 03:30:07 10c
2018-03-07 11:09:10 12c
2018-03-21 22:01:20 15c
2018-03-11 21:47:06 11c
2018-03-30 05:03:58 10c
2018-06-01 12:04:10 25c
2018-06-11 20:30:30 21c
2018-06-30 14:02:28 30c
2018-10-01 14:05:14 21c
2018-10-25 12:30:09 25c
2018-10-27 23:12:33 23c
2018-10-30 09:50:00 24c
2019-06-01 07:12:42 28c
2019-06-04 12:30:42 32c
2019-06-10 19:02:19 30c
2019-06-25 15:01:50 36c
2019-09-01 09:22:12 30c
2019-09-11 10:20:02 22c
2019-09-20 09:22:12 27c
2019-09-25 18:08:55 20c
2020-01-01 12:12:32 18c
2020-01-04 11:30:42 10c
2020-01-10 09:23:29 11c
2020-06-05 11:21:30 36c
2020-06-12 15:01:10 36c
2020-06-26 15:13:15 36c
2020-06-28 20:04:18 36c
2020-09-01 09:22:12 30c
2020-09-11 10:20:02 22c
2020-09-20 09:22:12 27c
2020-09-30 18:08:55 20c
输出:java_有不同年份的温度数据文件,要求日期按年月升序、温度按降序 将温度数据排列出来

相关标签: 笔记