日期到字符串解析

时间:2015-05-22 08:33:09

标签: java parsing gregorian-calendar

在这里给出一个文本文件:

       Date    Opening    Closing
 6-Mar-2012   500         1000
 9-Jun-2011   110         970
 7-Dec-2015   1090        980.69
28-Feb-2010   500         800

我需要在GregorianCalendar中构建日期并在String中使用日期构造函数。我的输出中的日期显示为Null,是否有修复的想法?

输出:

The date is null, the opening is 500, and the closing is 800   

日期被声明为GregorianCalendar,但是,构造函数在String中使用日期,并且应该期望“dd-MM-yyyy”形式。

D类:

public class Djia implements Comparable<Djia> {
    // instance variables
    private GregorianCalendar date; 
    private double opening;
    private double closing; 

    public Djia(String dt, double opening, double closing) throws ParseException {
        DateFormat stringDate = new SimpleDateFormat("dd-MM-yyyy");
        Date date = stringDate.parse(dt);
        Calendar gCal = new GregorianCalendar();
        gCal.setTime(date);
        this.opening = opening;
        this.closing = closing;
    }

    public String date() {
        String date;
        SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
        date = dateformat.format(this.date.getTime());
        return date;
    }

    public double opening() {
        return opening;
    }

    public double closing() {
        return closing;
    }

    //...
}

主要方法:
我在这里完成了解析和格式化,因为几个月是给定文本文件中的String表示。首先,我将几个月解析为Integer表示,然后从Date格式化为String,因为Djia类只将Date作为字符串。

Scanner input = new Scanner(new File("file.txt"));
while (input.hasNextLine()) {
    String line = input.nextLine();
    String[] fields = line.split(" ");
    String date = fields[0];
    SimpleDateFormat simpleDate = new SimpleDateFormat("d-MMM-yyyy");
    Date nDate = simpleDate.parse(date);
    Format stringDate = new SimpleDateFormat("dd-MM-yyyy");
    String newDate = stringDate.format(nDate); 
    double closing = Double.parseDouble(fields[2]);
    double opening = Double.parseDouble(fields[1]);
    Djia newDjia = new Djia(newDate, opening, closing);
    //...
}

2 个答案:

答案 0 :(得分:1)

也许你在行的开头有空格字符:

尝试更改该行:

String[] fields=line.split(" ");

String[] fields=line.trim().split(" ");

通过添加trim()调用,将从行的开头和结尾删除任何空格字符。

或做:

 String dateString = line.substring(0, 12);

如果总是有相同的长度并且对齐线的日期部分。

答案 1 :(得分:1)

你必须在你的主要部门做到这一点:

  • 在SimpleDateFormat中设置您的区域设置
  • 使用拆分创建列表(不是数组),并删除空 元素

结果如下:

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Scanner;

public class Djia {

    private GregorianCalendar date;
    private double opening;
    private double closing;

    public Djia(String dt, double opening, double closing)
            throws ParseException {

        DateFormat stringDate = new SimpleDateFormat("dd-mm-yyyy");
        Date date = stringDate.parse(dt);
        GregorianCalendar gCal = new GregorianCalendar();
        gCal.setTime(date);

        this.opening = opening;
        this.closing = closing;
        this.date = gCal;
    }

    public String date() {
        String date;
        SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/YYYY");
        date = dateformat.format(this.date.getTime());
        return date;

    }

    public static void main(String[] args) {
        try {
            Scanner input = new Scanner(new File("file.txt"));
            while (input.hasNextLine()) {
                String line = input.nextLine();
                LinkedList fields=new LinkedList<String>(Arrays.asList(line.trim().split(" ")));
                for (int i=fields.size()-1; i>=0; i--) {
                    if (fields.get(i).equals("")) {
                        fields.remove(i);
                    }
                }
                String date = (String) fields.get(0);
                SimpleDateFormat simpleDate = new SimpleDateFormat("d-MMM-yyyy", Locale.US);
                Date nDate = simpleDate.parse(date);
                Format stringDate = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
                String newDate = stringDate.format(nDate);
                double closing = Double.parseDouble((String) fields.get(2));
                double opening = Double.parseDouble((String) fields.get(1));
                Djia newDjia = new Djia(newDate, opening, closing);
                System.out.println("The date is " + newDate + ", the opening is " + opening + ", and the closing is " + closing);

            }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}