日期来自文本文件

时间:2011-09-25 17:27:29

标签: java

我只是想问一下是否可以在文本文件中找到所有可能格式的日期并打印结果。我能够打开文件并格式化日期,但我无法将这两个元素组合在一起。这是目前为止的代码。 我感谢任何帮助。谢谢

以下是要求和我的方法:

代码需要搜索所有标题,标题,文本和脚注。  代码搜索日期,例如星期几,月份,数字1到31,然后是1950年和1950年之间的一个月,一年。 2050年 代码需要获取日期以及上面最近的标题并获取适用的主要部分  代码需要获取页码。

日期:

import java.text.*;
import java.util.*;
public class Dates
{
 public static void main(String[] args)
 {
 Date d1 = new Date();
 DateFormat[] dfa = new DateFormat[6];
 dfa[0] = DateFormat.getInstance();
 dfa[1] = DateFormat.getDateInstance();
 dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT);
 dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM);
 dfa[4] = DateFormat.getDateInstance(DateFormat.LONG);
 dfa[5] = DateFormat.getDateInstance(DateFormat.FULL);

 for(DateFormat df : dfa)
 {
 System.out.println(df.format(d1));


 }
 DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL);
 String s = df2.format(d1);
 System.out.println(s);

 try
 {
 Date d2 = df2.parse(s);
 System.out.println("parsed = " +d2.toString());

 }
 catch(ParseException pe)
 {
 System.out.println("Parse Exception");
 }
 }
}

用于打开文件:

import javax.swing.*;
import java.io.*;
import java.util.*;
public class FileOpener {
 /**
 * use a dialog box to select a text file (.txt)
 * @return a Scanner for the selected file, or null if cancel selected
 */

 public static Scanner selectTextFile() {
 do {
 JFileChooser chooser = new JFileChooser();
 FileNameExtensionFilter filter = new FileNameExtensionFilter(
 "Text/Java files","doc", "txt", "java");
 chooser.setFileFilter(filter);
 int returnVal = chooser.showOpenDialog(null);
 try {
 if(returnVal == JFileChooser.APPROVE_OPTION) {
 return new Scanner(chooser.getSelectedFile());
 } 
 else {
 return null;
 }
 }
 catch (FileNotFoundException e) {
 JOptionPane.showMessageDialog(null, "Invalid file!",
 "error", JOptionPane.ERROR_MESSAGE); 
 }
 } while (true);
 }
 /**
 * given a String, uses a Scanner to count the number of words
 * @return number of words in the String
 */
 public static int countWordsOnLine(String line) {
 Scanner s = new Scanner(line);
   //int count = 0;
 while (s.hasNext()) {
 s.next();
  //count++;
 }
  //return count;
 }
 public static void main(String[] args) {
 // make Java look like your normal OS

 try {
 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 }
 catch (Exception e) { // ignore exceptions and continue
 }
 Scanner lineScanner = FileOpener.selectTextFile();
 int numberOfWords = 0;
 if (lineScanner!=null) {
 while (lineScanner.hasNextLine()) {
 numberOfWords += FileOpener.countWordsOnLine(
 lineScanner.nextLine());
 }
 System.out.println("The number of words is: " + numberOfWords);
 //System.out.println(getPageNumber());
 }
 }
}

将包含最终结果的表:

import javax.swing.JFrame;
 import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo extends JPanel {
 private boolean DEBUG = false;

 public SimpleTableDemo() {
 super(new GridLayout(1,0));

 String[] columnNames = {"HEADER",
 "SENTENCE",
 "PAGE",
 "DATE"};

 Object[][] data = {
 {" ", " ",
 " ", new Integer(5)},
 {" ", " ",
 " ", new Integer(3)},
 {" ", " ",
 " ", new Integer(2)},
 {" ", " ",
 " ", new Integer(20)},
 {" ", " ",
 " ", new Integer(10)}
 };

 final JTable table = new JTable(data, columnNames);
 table.setPreferredScrollableViewportSize(new Dimension(500, 70));
 table.setFillsViewportHeight(true);

 if (DEBUG) {
 table.addMouseListener(new MouseAdapter() {
 public void mouseClicked(MouseEvent e) {
 printDebugData(table);
 }
 });
 }

 //Create the scroll pane and add the table to it.
 JScrollPane scrollPane = new JScrollPane(table);

 //Add the scroll pane to this panel.
 add(scrollPane);
 }

 private void printDebugData(JTable table) {
 int numRows = table.getRowCount();
 int numCols = table.getColumnCount();
 javax.swing.table.TableModel model = table.getModel();

 System.out.println("Value of data: ");
 for (int i=0; i < numRows; i++) {
 System.out.print(" row " + i + ":");
 for (int j=0; j < numCols; j++) {
 System.out.print(" " + model.getValueAt(i, j));
 }
 System.out.println();
 }
 System.out.println("--------------------------");
 }

 /**
 * Create the GUI and show it. For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
 private static void createAndShowGUI() {
 //Create and set up the window.
 JFrame frame = new JFrame("SimpleTableDemo");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 //Create and set up the content pane.
 SimpleTableDemo newContentPane = new SimpleTableDemo();
 newContentPane.setOpaque(true); //content panes must be opaque
 frame.setContentPane(newContentPane);

 //Display the window.
 frame.pack();
 frame.setVisible(true);
 }

 public static void main(String[] args) {
 //Schedule a job for the event-dispatching thread:
 //creating and showing this application's GUI.
 javax.swing.SwingUtilities.invokeLater(new Runnable() {
 public void run() {
 createAndShowGUI();
 }
 });
 }
}

1 个答案:

答案 0 :(得分:1)

所有日期几乎肯定是不可能的。你打算如何解析这些?

  • 2012年3月第二个星期一
  • Donderdag 14 oktober aanstaan​​de。
  • 2012年春分之后的第一个满月之后的第一个星期天

这些都是日期,还有更多可能性。您可以编写一个脚本来查找大多数日期,但不是全部日期。

但是你最好的策略可能是定义一些常见的模式,并逐行扫描文本文件。