从文本文件中解析日期并将月份转换为数字

Parse a date from a text file and convert the month to a number

我有一个文本文件显示以下内容:

      Date    Opening    Closing
 6-Mar-2006   11022.47   10958.59
 9-Jun-2006   11005.66   10972.28
 7-Dec-2006   10957.31   10980.69
28-Feb-2006   11096.75   10993.41
 8-Mar-2006   10977.08   11005.74

我如何读取此文件并将字符串中的所有月份转换为整数显示中的月份。 "Mar"3"Jun"6。等等

到目前为止我的代码:

Scanner in= new Scanner(System.in);
        int N=in.nextInt();
        in.close();

        Scanner input=new Scanner(new File("file.txt"));
        while (input.hasNextLine()){
            String line=input.nextLine();
            String[] fields=line.split(" ");
            String date=fields[0];
            String[] datefields=date.split("-");
            String month=datefields[1];
*******************************************************
This is where I want to do the conversion.
*******************************************************

谢谢!

您可以使用 SimpleDateFormat 如下所示:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
Date date = formatter.parse(dateInString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1; 
System.out.println("Month number = " + month);

这是一个非常简单的示例,用于处理您的文件并拆分字符串行并获取日期对象。

public class FileReaderExample
{
  public static void main(String[] args)
  {
    File file = new File("d:\text.txt");
    try
    {
      BufferedReader br = new BufferedReader(new FileReader(file));
      String line;
      int lineNo = 1;
      while ((line = br.readLine()) != null)
      {
        // ignore the first line of       Date    Opening    Closing
        if (lineNo != 1)
        {
          String[] itemsOnLine = line.trim().split("\s+");
          System.out.println("Your date is : " + itemsOnLine[0]);
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
          Date yourDate = simpleDateFormat.parse( itemsOnLine[0]);
          System.out.println(yourDate);
          Calendar calendar= Calendar.getInstance();
          calendar.setTime(yourDate);
          // Account for month starting at 0
          int month = calendar.get(Calendar.MONTH) +1 ;
          System.out.println("The month of the date is " + month);
        }
        lineNo++;
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
  }
}

编辑: 添加了您的月份要求

    try {
        Scanner input = new Scanner(new File("file.txt"));
        int lineNumber = 1;
        while (input.hasNextLine()) {
            String line = input.nextLine();
            if (lineNumber > 1) {      // not the header line
                String[] itemsOnLine = line.trim().split("\s+");   //Space delimited

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");  //format the date
                Date yourDate = simpleDateFormat.parse(itemsOnLine[0]);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(yourDate);
                int month = calendar.get(Calendar.MONTH) + 1;   //Date is 0 based so add 1 to the month
                System.out.println("stack.run " + month);
            }
            lineNumber++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }