Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印日期和时间_Java_Date_Time - Fatal编程技术网

Java 打印日期和时间

Java 打印日期和时间,java,date,time,Java,Date,Time,我有一个CSV文件,其中包含以下数据: red, 03/11/2014, 11:00, 10 blue, 04/11/2014, 12:00, 15 pink, 03/11/2014, 15:00, 50 blue, 05/11/2014, 14:00, 15 pink, 02/11/2013, 12:00, 10 green, 03/12/2014, 23:00, 1 red, 03/11/2013, 23:11, 11 我试图打印日期和时间,但一直失败。 当 public static f

我有一个CSV文件,其中包含以下数据:

red, 03/11/2014, 11:00, 10
blue, 04/11/2014, 12:00, 15
pink, 03/11/2014, 15:00, 50
blue, 05/11/2014, 14:00, 15
pink, 02/11/2013, 12:00, 10
green, 03/12/2014, 23:00, 1
red, 03/11/2013, 23:11, 11
我试图打印日期和时间,但一直失败。 当

public static final String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";
改为

public static final String DATE_FORMAT = "dd/MM/yyyy";
但那只会打印日期

守则:

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class RangeCalculator {
private File file;
public static final String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";

public class Entry {
    String colour;
    String date;
    String time;
    String noise;

    public Entry(String colour, String date, String time, String noise) {
        super();
        this.colour = colour;
        this.date = date;
        this.time = time;
        this.noise = noise;
    }

    // implement getters and setters here if necessary

    @Override
    public String toString() {
        return "Entry [colour=" + colour + ", date=" + date + ", time="
                + time + ", noise=" + noise + "]";
    }
}

public RangeCalculator(String fileName) {
    file = new File(fileName);
}

private List<Entry> computeRange(String from, String to)
        throws FileNotFoundException, ParseException {

    List<Entry> result = new LinkedList<>();

    SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT_DATA);
    Date fromDate = formatter.parse(from);
    Date toDate = formatter.parse(to);

    Scanner scanner = new Scanner(file);
    scanner.useDelimiter("[,\n]");

    // maybe you want/need to skip the first line of the file
    // if (scanner.hasNextLine()) {
    //     scanner.nextLine();
    // }

    while (scanner.hasNextLine()) {         
        String color = scanner.next();
        String date = scanner.next().substring(1);
        String time = scanner.next().substring(1);
        String noise = scanner.next().substring(1);

        Date currDate = formatter.parse(date);
        if (!currDate.before(fromDate) && !currDate.after(toDate)) {
            result.add(new Entry(color, date, time, noise));
        }
    }

    scanner.close();
    return result;
}

private void printEntries(List<Entry> entries) {
    for (Entry entry : entries) {
        System.out.println(entry.toString());
    }
}

public static void main(String[] args) {
    RangeCalculator app = new RangeCalculator("Data.csv");

    List<Entry> calculatedEntries = null;
    try {
        calculatedEntries = app.computeRange("03/11/2014 11:00", "04/11/2014 12:00");
    } catch (FileNotFoundException e) {
        System.err.println("ERROR: File not found!");
        System.exit(1);
    } catch (ParseException e) {
        System.err.println("ERROR: Wrong date format!");
        System.exit(1);
    }

    app.printEntries(calculatedEntries);
    System.exit(0);
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Scanner;
公共类范围计算器{
私有文件;
公共静态最终字符串日期\时间\格式\数据=“dd/MM/yyyy HH:MM”;
公开课入学{
弦色;
字符串日期;
串时间;
弦噪声;
公共输入(字符串颜色、字符串日期、字符串时间、字符串噪音){
超级();
这个颜色=颜色;
this.date=日期;
这个时间=时间;
这个。噪音=噪音;
}
//如有必要,在此处实现getter和setter
@凌驾
公共字符串toString(){
return“Entry[color=“+color+”,date=“+date+”,time=”
+时间+”,noise=“+noise+”];
}
}
公共范围计算器(字符串文件名){
文件=新文件(文件名);
}
私有列表计算机范围(字符串从,字符串到)
抛出FileNotFoundException、ParseException{
列表结果=新建LinkedList();
SimpleDataFormat格式化程序=新的SimpleDataFormat(日期\时间\格式\数据);
Date fromDate=格式化程序.parse(from);
Date toDate=formatter.parse(to);
扫描仪=新扫描仪(文件);
scanner.useDelimiter(“[,\n]”);
//也许你想/需要跳过文件的第一行
//if(scanner.hasNextLine()){
//scanner.nextLine();
// }
而(scanner.hasNextLine()){
字符串颜色=scanner.next();
字符串日期=scanner.next().substring(1);
字符串时间=scanner.next().substring(1);
字符串噪声=scanner.next().substring(1);
Date currDate=格式化程序.parse(日期);
如果(!currDate.before(fromDate)和&!currDate.before(toDate)){
结果。添加(新条目(颜色、日期、时间、噪音));
}
}
scanner.close();
返回结果;
}
私有无效打印条目(列表条目){
对于(条目:条目){
System.out.println(entry.toString());
}
}
公共静态void main(字符串[]args){
RangeCalculator应用程序=新的RangeCalculator(“Data.csv”);
列表calculatedEntries=null;
试一试{
calculatedEntries=app.computeRange(“03/11/2014 11:00”、“04/11/2014 12:00”);
}catch(filenotfounde异常){
System.err.println(“错误:找不到文件!”);
系统出口(1);
}捕获(解析异常){
System.err.println(“错误:日期格式错误!”);
系统出口(1);
}
应用程序打印条目(calculatedEntries);
系统出口(0);
}
}

感谢您的帮助。

您正在尝试使用包含日期和时间的格式化程序仅解析日期。 您只需更换这一行:

Date currDate = formatter.parse(date);
为此:

Date currDate = formatter.parse(date + " " + time);

您正在尝试使用包含日期和时间的格式化程序仅解析日期。 您只需更换这一行:

Date currDate = formatter.parse(date);
为此:

Date currDate = formatter.parse(date + " " + time);

当您将日期和时间组件作为两个单独的字段获取时,您需要在使用之前附加它们。因此,下文应为:


Date currDate=格式化程序.parse(日期+“”+时间)

当您将日期和时间组件作为两个单独的字段获取时,您需要在使用之前将它们都追加。因此,下文应为:


Date currDate=格式化程序.parse(日期+“”+时间)

如果采用这种格式dd/MM/yyyy HH:MM,那么它将同时解析日期和时间。如果采用这种格式dd/MM/yyyy HH:MM,那么它将同时解析日期和时间