-2

I have a text file display the following:

      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

How can I read in this file and convert all the months in String to month in int display. "Mar" to 3, "Jun" to 6. etc

My code so far:

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.
*******************************************************

Thanks!

Katherine
  • 257
  • 2
  • 6
  • 15

3 Answers3

2

Here is a very simple example to process your file and split the string line and obtain the date object.

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();
    }
  }
}

EDIT: Added your month requirement

Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26
0

You can use SimpleDateFormat as shown below:

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);
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0
    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();
    }
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
Neil Lane
  • 31
  • 5