Java 文本文件中的日期
我只是想问一下,是否有可能在文本文件中找到所有可能格式的日期并打印结果。我可以打开文件并格式化日期,但我无法将这两个元素结合起来。这是到目前为止的代码。 谢谢你的帮助,谢谢 以下是我的要求和方法: 代码需要搜索所有标题、标题、文本和脚注。 代码搜索日期,例如星期几、月份、数字1到31,后跟1950到2050之间的月份、年份。 代码需要获取日期以及上面最近的标题,并获取适用的主要部分 代码需要获取页码 截止日期:Java 文本文件中的日期,java,Java,我只是想问一下,是否有可能在文本文件中找到所有可能格式的日期并打印结果。我可以打开文件并格式化日期,但我无法将这两个元素结合起来。这是到目前为止的代码。 谢谢你的帮助,谢谢 以下是我的要求和方法: 代码需要搜索所有标题、标题、文本和脚注。 代码搜索日期,例如星期几、月份、数字1到31,后跟1950到2050之间的月份、年份。 代码需要获取日期以及上面最近的标题,并获取适用的主要部分 代码需要获取页码 截止日期: import java.text.*; import java.util.*; pu
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();
}
});
}
}
import javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
公共类SimpleTableDemo扩展了JPanel{
私有布尔调试=false;
公共SimpleTableDemo(){
超级(新网格布局(1,0));
String[]columnNames={“HEADER”,
“判决”,
“第页”,
“日期”};
对象[][]数据={
{" ", " ",
“”,新整数(5)},
{" ", " ",
“”,新整数(3)},
{" ", " ",
“”,新整数(2)},
{" ", " ",
“”,新整数(20)},
{" ", " ",
“”,新整数(10)}
};
最终JTable表=新JTable(数据、列名称);
表.setPreferredScrollableViewportSize(新维度(500,70));
表.setFillsViewPerthweight(真);
如果(调试){
表.addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
打印数据(表格);
}
});
}
//创建滚动窗格并将表添加到其中。
JScrollPane scrollPane=新的JScrollPane(表);
//将滚动窗格添加到此面板。
添加(滚动窗格);
}
私有无效printDebugData(JTable表){
int numRows=table.getRowCount();
int numCols=table.getColumnCount();
javax.swing.table.TableModel=table.getModel();
System.out.println(“数据值:”);
对于(int i=0;i
几乎可以肯定,所有日期都是不可能的。您将如何解析这些
- 2012年3月第二个星期一
- 东德达格14号奥克托伯·安斯坦德
- 2012年春分后第一次满月后的第一个星期日
但您最好的策略可能是定义一些常见模式,并逐行扫描文本文件。您可能希望对代码进行一致的缩进,以提高可读性。请将代码限制在相关部分,并提出具体问题。