2

I have strings follow format:

2012.11.15 22:00:00 1.2971 1.2974 1.2959 1.296

I use follow code:

DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                String inputLine;
                in.readLine();
                while ((inputLine = in.readLine()) != null) {
                    StringTokenizer st = new StringTokenizer(inputLine);
                    Date date       = df.parse( st.nextToken() );
                    double open     = Double.parseDouble( st.nextToken() );
                    double high     = Double.parseDouble( st.nextToken() );
                    double low      = Double.parseDouble( st.nextToken() );
                    double close    = Double.parseDouble( st.nextToken() );
              ...

But method SimpleDateFormate get only date and space between date/time used as separator.
How get date with time together ?

Denis S.
  • 137
  • 2
  • 11

3 Answers3

3

Better use the split() method in String, for all practical purposes StringTokenizer is deprecated and it's still there only for backwards compatibility.

Try something like this:

String[] data = inputLine.split("\\s+");

Now each position in the resulting array will hold a piece of data, and you can proceed to convert it to the appropriate data types. In particular, for the date (which is made up of actual date plus time) do something like this:

Date date = df.parse(data[0] + " " + data[1]);

The advantage of the above is that you can forget about getting the next token in the tokenizer, and from the start you'll know how many elements were read (the array's length).

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    ...or possible use a more complex regex if the date/time can appear anywhere. `(\d{4}\.\d{2}\.\d{2}\s+\d{2}:\d{2}:\d{2}|\d+\.\d+)\s+` – Vicki Nov 15 '12 at 15:43
  • Thank you for advice. Can I change following code so? `double open = Double.parseDouble( data[3] ); double high = Double.parseDouble( data[4] ); double low = Double.parseDouble( data[5] ); double close = Double.parseDouble( data[6] );` – Denis S. Nov 15 '12 at 16:23
  • The `open` variable would be in position `data[2]`, I believe. Other than that yeah, you can do it. – Óscar López Nov 15 '12 at 16:27
  • Oh, definitely `data[2]` :) Will use split(), it is elegant solution. – Denis S. Nov 15 '12 at 16:46
2

Use:

Date date       = df.parse( st.nextToken() + " " + st.nextToken() );

to pass both "words" to the SimpleDateFormat object.

dashrb
  • 952
  • 4
  • 10
1

You create a StringTokenizer with one parameter constructor (the string to tokenize). This constructor assumes that any whitespace is a separater between distinct tokens. Your date representation has the space in the middle. Hence StringTokenizer chops out half of the date, leaving another for the second token.

As you do use spaces later to separate other fields, the straightforward solution is simply to read both tokens and rebuild the combined field:

 Date date = df.parse( st.nextToken()+" "+ st.nextToken());
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103