0

In a program, I use the following procedure to convert EUR in DOLLAR or vice-versa. In general, this procedure works fine with whatever currency.

public double getRate(String from, String to)
{
    BufferedReader reader = null;
    try
    {
        URL url = new URL("http://quote.yahoo.com/d/quotes.csv?f=l1&s=" + from + to + "=X");
        reader = new BufferedReader(new    InputStreamReader(url.openStream()));

        String line = reader.readLine();

        if (line.length() > 0)
        {
            return Double.parseDouble(line);
        }
    }
    catch (IOException | NumberFormatException e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
        if (reader != null)
        {
            try
            {
                reader.close();
            }
            catch(IOException e)
            {
            }
        }
    }

    return 0;
}

My problem is that I want to create a similar method for historical data. Basically, I need a method with the following signature:

public double getRate(String from, String to, Date date) {
   ...
}

that I can call in this way:

getRate("USD", "EUR", new SimpleDateFormat( "yyyyMMdd" ).parse( "20160104" ))

to get the value in EUR of 1$ in 2016/01/04 or whatever date in the past. I read lot of thread on StackOverflow and other similar website ma no solution found. I need a solution using a free service.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Salvatore D'angelo
  • 1,019
  • 3
  • 14
  • 39
  • Does yahoo offer this kind of data? – wvdz Jan 09 '17 at 13:39
  • 2
    Is this a question about how to code it in Java or how to use the web site to get historical conversion rates? – Peter Lawrey Jan 09 '17 at 13:39
  • Both, but most important are the second one. I want a way to retrieve this info from the web. If there is also java code it is better but I can also implement it by myself. – Salvatore D'angelo Jan 09 '17 at 13:43
  • Yahoo offer only current data. I haven't found a way to get these historical info from yahoo finance. – Salvatore D'angelo Jan 09 '17 at 13:44
  • At least for testing purpose, here is a site (don't know how reliable it is) that uses a date parameter : http://currencies.apps.grandtrunk.net/ – Arnaud Jan 09 '17 at 13:49
  • Thank you, Berger. Yes, this is a good answer. Here a possible link http://currencies.apps.grandtrunk.net/getrate/2016-01-04/usd/eur to convert 1$ in EUR on date 2016-01-04. The only problem I see is that data seems not compatible with Yahoo finance. – Salvatore D'angelo Jan 09 '17 at 14:13

1 Answers1

0

Thanks to Berger's answer I could implement my method. Here how it looks like. I hope it could be useful for someone else in this forum.

public double getRate(String from, String to, Date date) {
    BufferedReader reader = null;
    try {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = df.format(date);
        URL url = new URL("http://currencies.apps.grandtrunk.net/getrate/" + dateString +"/" + from + "/" + to);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = reader.readLine();
        if (line.length() > 0) {
            return Double.parseDouble(line);
        }
    } catch (IOException | NumberFormatException e) {
        System.out.println(e.getMessage());
    } finally {
        if (reader != null) try { reader.close(); } catch(IOException e) {}
    }

    return 0;
}
Salvatore D'angelo
  • 1,019
  • 3
  • 14
  • 39