Samual Sam has Published 2492 Articles

Display numbers with leading zeroes in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

2K+ Views

To display numbers with leading zeros in Java, useDecimalFormat("000000000000").Let us first set the format with DecimalFormat class −DecimalFormat decFormat = new DecimalFormat("000000000000");Now, let us display some numbers with leading zeros −String res = decFormat.format(298989); System.out.println(res); res = decFormat.format(565); System.out.println(res);The following is an example −Example Live Demoimport java.text.DecimalFormat; public class Demo { ... Read More

SimpleDateFormat(“HH.mm.ss”) in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

9K+ Views

The format SimpleDateFormat(“HH.mm.ss”) displays time. To work with SimpleDateFormat class, we have imported the following package.import java.text.SimpleDateFormat;Here, we are using the SimpleDateFormat class to display date and time. Let us set it for the format we want i.e time - HH.mm.ss −Format f = new SimpleDateFormat("HH.mm.ss"); String strResult = f.format(new ... Read More

Display a currency value in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

152 Views

To display a currency in Java, use the following DecimalFormat −DecimalFormat decFormat = new DecimalFormat("\u00a4#, ##0.00");Since, we have used the DecimalFormat class, therefore do not forget to import the following package −import java.text.DecimalFormat;Now, let us learn how to display percentage −decFormat.format(877.80) decFormat.format(8.19) decFormat.format(9897.88)The above will be displayed as −$877.80 $8.19 ... Read More

Display day number with SimpleDateFormat('d') in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

162 Views

To display the day number, use the SimpleDateFormat('d') as shown below −Format f = new SimpleDateFormat("d"); String strDay = f.format(new Date()); System.out.println("Day Number = "+strDay);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date −import java.text.Format; import ... Read More

DecimalFormat("##E0") in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

55 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("##E0") and use the format() method as well.DecimalFormat decFormat = new DecimalFormat("##E0"); System.out.println(decFormat.format(-189.8787)); System.out.println(decFormat.format(8.19)); System.out.println(decFormat.format(9897.88));Since, we have used DecimalFormat class in Java, therefore importing the following package is a must −import java.text.DecimalFormat;The following is the complete ... Read More

How to get Column name on ResultSet in Java with MySQL?

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

546 Views

To get the column name on the result set, you need to use getMetaData() method. The prototype of getMetadata() is as follows −ResultSetMetaData getMetaData throws SQLException;Create a MySQL table with 5 column names. The query to create a table is as follows −mysql> create table javagetallcolumnnames    -> (   ... Read More

Java Program to round number to fewer decimals

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

110 Views

Let’s say we need to round number to 3 decimal places, therefore use the following format −DecimalFormat decFormat = new DecimalFormat("0.000");Now, format the number −decFormat.format(37878.8989)Since we have used the DecimalFloat class above, therefore do import the following package −import java.text.DecimalFormat;The following is an example that rounds a number to 3 ... Read More

Display year with SimpleDateFormat('yyyy') in Java

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

410 Views

Using the SimpleDateFormat(“yyyy”) to display year −// displaying year Format f = new SimpleDateFormat("yyyy"); String strYear = f.format(new Date()); System.out.println("Year = "+strYear);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date −import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;The ... Read More

Java Program to copy an array from the specified source array

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

114 Views

Use arraycopy() method in Java to copy an array from the specified source array.Here, we have two arrays −int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30};Now, we will use the arraycopy() method to copy the first two elements of the 1st array ... Read More

Java Program to format date in mm-dd-yyyy hh:mm:ss format

Samual Sam

Samual Sam

Updated on 30-Jul-2019 22:30:24

6K+ Views

Let us format date in mm-dd-yyyy hh:mm:ss format −// displaying date in mm-dd-yyyy hh:mm:ss format Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss"); String str = f.format(new Date()); System.out.println("Current Date in MM-dd-yyyy hh:mm:ss format = "+str);Since we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, ... Read More

Advertisements