Getting historical stock quotes using yahoofinance API

Question:
How to get historical stock quotes using yahoofinance API?

Answer:
Here is an example on how to get historical stock quotes using the YahooFinance API.

Code:
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import yahoofinance.Stock;
import yahoofinance.YahooFinance;
import yahoofinance.histquotes.HistoricalQuote;
import yahoofinance.histquotes.Interval;
 
public class YahooQuotes {
 
  public static void main(String[] args) throws IOException {
 
    Calendar from = new GregorianCalendar(2015, 5, 1);
    Calendar to = new GregorianCalendar(2015, 5, 10);
 
    Stock stock = YahooFinance.get("GOOG");
 
    List<HistoricalQuote> history = stock.getHistory(from, to, Interval.DAILY);
    for (HistoricalQuote hq : history) {
      System.out.print(hq.getSymbol() + " - ");
      System.out.print(hq.getClose() + " - ");
      System.out.println(hq.getDate().getTime());
    }    
 
  }
}

Output:
$ java YahooQuotes
GOOG - 536.690002 - Wed Jun 10 00:00:00 EDT 2015
GOOG - 526.690002 - Tue Jun 09 00:00:00 EDT 2015
GOOG - 526.830017 - Mon Jun 08 00:00:00 EDT 2015
GOOG - 533.330017 - Fri Jun 05 00:00:00 EDT 2015
GOOG - 536.700012 - Thu Jun 04 00:00:00 EDT 2015
GOOG - 540.309998 - Wed Jun 03 00:00:00 EDT 2015
GOOG - 539.179993 - Tue Jun 02 00:00:00 EDT 2015
GOOG - 533.98999 - Mon Jun 01 00:00:00 EDT 2015

How to get a single StockQuote using yahoofinance API?

Question:
How to get a StockQuote using yahoofinance API?

Answer:
Here is a simple example on how to get a single quote using the Java Finance Quotes API for Yahoo.Finance

Code:
import java.io.IOException;
import yahoofinance.Stock;
import yahoofinance.YahooFinance;
import yahoofinance.quotes.stock.StockQuote;
 
public class YahooStockQuoteAPI {
 
  public static void main(String[] args) throws IOException {
    YahooFinance yahoo = new YahooFinance();
    Stock stock = yahoo.get("GOOG");
    StockQuote sq = stock.getQuote();
 
    System.out.println("Symbol: " + sq.getSymbol());
    System.out.println("Price: " + sq.getPrice());
    System.out.println("Date: " + sq.getLastTradeTime().getTime());
  }
}

Output:
$ java YahooStockQuoteAPI 
Symbol: GOOG
Price: 662.20
Date: Fri Oct 16 16:00:00 EDT 2015

Converting BigDecimal to an int

Question:
How to Convert a BigDecimal to an int?
Converting BigDecimal to Integer

Answer:
bigdecimal.intValue();

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
 
    BigDecimal big = new BigDecimal(123.456);
    int x = big.intValue();
    System.out.println(x);
  }
}
 

Output:
$ java BigDecimalTest
123

How to multiply a BigDecimal by an integer in Java?

Question:
How to multiply a BigDecimal by an int in Java?
Multiplication with BigDecimals.

Answer:
bigdecimal.multiply(new BigDecimal(int))

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
 
    BigDecimal bd = new BigDecimal(2.0);
    int x = 5;
    bd = bd.multiply(new BigDecimal(x));
    System.out.println(bd);
  }
}
 

Output:
$ java BigDecimalTest
10

How to test if a BigDecimal is less then zero?

Question:
How to test if a BigDecimal is less then zero?
How to compare BigDecimal to an int?

Answer:
number.compareTo(BigDecimal.ZERO)

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
    BigDecimal bd = new BigDecimal(-1.5);
 
    if (bd.compareTo(BigDecimal.ZERO) < 0) {
      System.out.println(bd + " is less then zero");
    }
  }
}
 

Output:
$ java BigDecimalTest
-1.5 is less then zero

How to write a java program that will ask for the length and width of a rectangle and then multiply them together and create a rectangle out of astrisks

Question:
How to write a java program that will ask for the length and width of a rectangle and then multiply them together and create a rectangle out of astrisks?

Code:
import java.util.Scanner;
 
public class Rectangle {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);
 
    System.out.print("Enter height: ");
    int h = sc.nextInt();
 
    System.out.print("Enter width: ");
    int w = sc.nextInt();
 
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        System.out.print("*");
      }
      System.out.println();
    }
  }
}

Output:
$ java Rectangle
Enter height: 5
Enter width: 20
********************
********************
********************
********************
********************

Find closest value to 0 for Float in Java?

Question:
Find closest value to 0 for Float in Java?

Answer:
Float.MIN_VALUE

Code:
public class MyFloat {
  public static void main(String[ ] args) {
    Float f = new Float(Float.MIN_VALUE);
    System.out.format("%.50f%n", f);
  }
}
 

Output:
$ java MyFloat
0.00000000000000000000000000000000000000000000140130