美元到欧元的历史汇率

美元到欧元的历史汇率

本文介绍了美元到欧元的历史汇率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中,我使用以下过程将EUR转换为DOLLAR,反之亦然.通常,此过程适用于任何货币.

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) {
   ...
}

我可以这样称呼:

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

以获得2016/01/04或过去任何日期的1美元的欧元值.我在StackOverflow和其他类似网站上阅读了很多线程,但未找到解决方案.我需要使用免费服务的解决方案.

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.

推荐答案

感谢Berger的回答,我可以实现我的方法.在这里看起来像什么.我希望它对本论坛中的其他人可能有用.

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

这篇关于美元到欧元的历史汇率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:52