Z_Z Z_Z - 25 days ago 9
Java Question

Parsing Date in an if-statement and showing only results in stipulated time range

Currently I'm writing a java program to find out on which day a hammer or other candlestick pattern was formed. The user has to input 2 dates as arguments when executing the program, e.g. java ReadingTest 2016-09-03 2016-10-31, and the program will look for hammer pattern from 2016-09-03 to 2016-10-31.
The code is as follows:

import java.io.*;
import java.util.*;
import java.text.*;

public class ReadingTest
{
public static void main(String[] args) throws IOException,ParseException
{
//Import file to java
File file = new file("table.csv");

//Read the file
Scanner infile = new Scanner(file);

//Skip the first line in table.csv
infile.nextLine();

//Define format of date
SImpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//Name the variables user enters
Date start = sdf.parse(args[0]);
Date end = sdf.parse(args[1]);

//Create ArrayList for each column of data
ArrayList<String> date = new ArrayList<String>();
ArrayList<Double> open = new ArrayList<Double>();
ArrayList<Double> high = new ArrayList<Double>();
ArrayList<Double> low = new ArrayList<Double>();
ArrayList<Double> close = new ArrayList<Double>();

while (infile.hasNext())
{
//Tokenize columns by comma
String[] data = infile.nextLine().split(",");
//Organize each column of data to one index of data array
date.add(data[0]);
open.add(Double.parseDouble(data[1]));
high.add(Double.parseDouble(data[2]));
low.add(Double.parseDouble(data[3]));
close.add(Double.parseDouble(data[4]));
}
//Show options and ask user to choose
System.out.println("1. Hammer");
System.out.println("2. Three white soldiers");
System.out.println("3. Bullish kicker");

//Record user input and execute corresponding code
Scanner input = new Scanner(System.in);
int choice = input.nextInt();

if (choice == 1)
for (int i = 0; i < date.size(); i++)
if (close.get(i) > open.get(i) &&
close.get(i) > ((high.get(i)) + (low.get(i)))/2 &&
((close.get(i) - low.get(i))/2 > (high.get(i) - close.get(i)))
System.out.println("Pattern found: " + date.get(i));
}
}


The code works perfectly till here. However, the output in the last line of code is in the dd/MM/yyyy format and I tried to use sdf.parse(date.get(i)) instead of date.get(i) to show results in yyyy-MM-dd format. Running the code with sdf.parse(date.get(i)) returns the following error:

Exception in thread "main" java.text.ParseException: Unparseable date:
"25/10/2016" at
java.text.DateFormat.parse(Unknown source) at ReadingTest.main(ReadingTest.java:59)


I also tried to display only dates showed a hammer pattern using

(date.get(i).after(start) && date.get(i).before(end))


and results in

error: cannot find symbol
symbol: method before(Date)
location: class String


And the CSV file looks like this:

Date Open High Low Close
31/10/2016 58.25 58.65 58.2 58.35
28/10/2016 58.95 59 58.3 58.35
.
.
.
1/8/2016 50.8 51.1 50.75 50.8


How the code should be amended to make it work?

ncw ncw
Answer

I guess what you want is this

SimpleDateFormat hammerFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat slashFormat = new SimpleDateFormat("dd/MM/yyyy");

so you can parse your date to the yyyy-MM-dd representation like this

hammerFormat.format(slashFormat.parse(date.get(i))));

Full Code

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class ReadingTest {
    public static void main(String[] args) throws IOException, ParseException {
        // Import file to java
        File file = new File("table.csv");

        // Read the file
        Scanner infile = new Scanner(file);

        // Skip the first line in table.csv
        infile.nextLine();

        // Define format of date
        SimpleDateFormat hammerFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat slashFormat = new SimpleDateFormat("dd/MM/yyyy");

        // Name the variables user enters
        Date start = hammerFormat.parse(args[0]);
        Date end = hammerFormat.parse(args[1]);

        // Create ArrayList for each column of data
        ArrayList < String > date = new ArrayList < String > ();
        ArrayList < Double > open = new ArrayList < Double > ();
        ArrayList < Double > high = new ArrayList < Double > ();
        ArrayList < Double > low = new ArrayList < Double > ();
        ArrayList < Double > close = new ArrayList < Double > ();

        while (infile.hasNext()) {
            // Tokenize columns by comma
            String[] data = infile.nextLine().split(",");
            // Organize each column of data to one index of data array
            date.add(data[0]);
            open.add(Double.parseDouble(data[1]));
            high.add(Double.parseDouble(data[2]));
            low.add(Double.parseDouble(data[3]));
            close.add(Double.parseDouble(data[4]));
        }
        // Show options and ask user to choose
        System.out.println("1. Hammer");
        System.out.println("2. Three white soldiers");
        System.out.println("3. Bullish kicker");

        // Record user input and execute corresponding code
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();

        if (choice == 1)
            for (int i = 0; i < date.size(); i++)
                if (close.get(i) > open.get(i) && close.get(i) > ((high.get(i)) + (low.get(i))) / 2 && ((close.get(i) - low.get(i)) / 2 > (high.get(i) - close.get(i))))
                    System.out.println("Pattern found: " + hammerFormat.format(slashFormat.parse(date.get(i))));
    }
}

Hope this helps!

Comments