java - 阅读文本并打印出其中的日期

标签 java string date calendar

我有一个包含此内容的文本文件

08:00 - 09:30
09:45 - 11:15
11:30 - 13:00
08/04/13
14:00 - 15:30
15:45 - 17:15
17:30 - 19:00 








MIS (CH27) UM
MIS (CH27) UM

每个空行对应一周中的一天,08/04/14 是一周的第一天。 我想读取文本文件,计算MIS发生的日期并将其打印出来。 这是我的程序,它只在结果中显示任何内容。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class UsingPattern {

public static final String SOURCE = "results/timetable1.txt";

/**
 * @param args
 * @throws ParseException 
 */
public static void main(String[] args) throws ParseException {      
    new UsingPattern().readFile(SOURCE);        
}
public void readFile(String filename) throws ParseException {       
    try {
        File file = new File(filename);
        FileReader reader = new FileReader(file);
        BufferedReader in = new BufferedReader(reader);
        String line;
        Calendar cal = new GregorianCalendar();                                 // Create a Calendar object
        while ((line = in.readLine()) != null) {                                // Read lines, check for end-of-file
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");     // Create a format for date input from text file                    
            Date date = null;
            if (line.matches("\\d{2}/\\d{2}/\\d{2}")) {                         // If the line has this format, put it as date
                date = dateFormat.parse(line);                                  
                cal.setTime(date);                                              // Sets this Calendar's time with the given Date.               
            }
            else {                                                              // Each empty line correspond to 1 day
                if ((in.readLine()) == " ") {                               
                    cal.add(Calendar.DAY_OF_MONTH, 1);                      
                }
                else {
                    if (line.matches("MIS")) {
                        String fday = dateFormat.format(cal.getTime()); 
                        System.out.println((fday)); 
                        System.out.println(line);
                    }   
                }                   
            }               
        }
        in.close();
    }       
    catch (IOException e) {
        e.printStackTrace();
    }

}

}

谁能指出我哪里做错了! 谢谢你!

最佳答案

我在您的代码中发现至少三个错误:

  1. 您在循环中从输入读取器读取下一行两次
  2. 在java中按值进行字符串比较时,必须使用equals而不是==
  3. 如果您想匹配像“MIS blablabla”这样的行,您应该使用 line.matches("MIS.*") 或更好的 line.startsWith("MIS")

关于java - 阅读文本并打印出其中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19513255/

相关文章:

c++ - 我应该在 ATL/WTL 中使用 CString、basic_string<TCHAR> 还是其他东西?

python - 可以将输入字符串转换为 Python 中的可调用函数对象吗?

linux - shell 脚本搜索并用日期替换字符串值

java - 用于分割和分类二进制或灰度图像的库

R/dplyr : How to only keep integers in a data frame?

javascript - JS 无法创建日期对象

excel - 在 Excel VBA 中连接日期后无法更改格式

java - java中使用可重入锁限制对共享数据的访问

Java自定义注释来限制对方法的访问

java - 我在 Xoom 上的 UI 看起来很难看/不像在 eclipse 中的样子