March 6 2017

Java program convert decimal to binary

Java program convert decimal to binary

Decimal number system is a base 10 number system using digits for 0 to 9 whereas binary number system is base 2 and uses 0 and 1.

Algorithm to convert Decimal to Binary number

  1. Divide the decimal number by 2 and store the remainder
  2. Store the quotient back to the input number variable
  3. Repeat step 1 and 2 till quotient becomes zero.
  4. Binary number will be the remainders in above process in reverse order

Example             Continue reading

January 10 2017

Java BufferedWriter Example

Java BufferedWriter Example

BufferedWriter class writes text to a character-output stream, buffering characters and provides efficient writing of single characters, arrays, and strings. Compared to FileWriter, BufferedWriter writes relatively large amount of data to a file at once. The buffer size may be specified, or the default size may be used.

BufferedWriter provides a newLine() method to create platform-specific line separators automatically. Wrap the BufferedWriter around a FileWriter, to get access to high level methods.

Key methods: close(), flush(), newLine(), write()

Example Continue reading

August 17 2016

Java calendar last day of month

Java calendar last day of month

Following are the different ways to get last day of the month

  • Set the Calendar object to be in the month for which you need the last date, getActualMaximum(Calendar.DAY_OF_MONTH) will give you the last date of that month.
  • Setting the calendar to the first of next month and subtract a day.

calendar.get(Calendar.DAY_OF_WEEK) returns int values from 1 to 7(1-Sunday and 7-Saturday)

Example Continue reading