Found 7442 Articles for Java

Space format specifiers in Java

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

2K+ Views

For format specifier, import the following package −import java.util.Formatter;Create a formatter object and set space format specifier −Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);The following is an example displaying different forms of space format specifiers −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); ... Read More

Left justify output in Java

Revathi Satya Kondra
Updated on 24-Dec-2024 12:04:36

3K+ Views

In Java, the left-justifying output means aligning text or data to the left within a specified width, with extra spaces on the right to fill the remaining space. It is commonly used when displaying tabular data or formatting strings. Left justification can be obtained using methods such as String.format() or printf(), which allow you to specify the width of the output field and the alignment of the content. Including a minus sign after the %, makes it left justified. Note − By default, output is right justified To display the Left justify output in Java is quite easy. Let us learn the following methods: ... Read More

Vertically align numeric values in Java using Formatter

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

509 Views

To vertically align numeric values in Java, use Formatter. For working with Formatter class, import the following package.import java.util.Formatter;Take an array −double arr[] = { 2.5, 4.8, 5.7, 6.5, 9.4, 8.4, 9.5, 10.2, 11.5 };While displaying this double array values, use the %f to set spaces −for (double d : arr) { f.format("%12.2f %12.2f %12.2f", d, Math.ceil(d), Math.floor(d)); }Above, we have also set the decimal places i.e. 12.2f is for 2 decimal places.The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Floating-point hexadecimal in Java

Revathi Satya Kondra
Updated on 17-Dec-2024 23:03:20

556 Views

In this article, we use the '%a' format specifier to represent floating-point numbers in their hexadecimal form. This is useful when you need precise control over the representation of floating-point values. For Formatter, import the following package − import java.util.Formatter; Now creating a 'Formatter'object to format the data− Formatter f = new Formatter(); Using the format() method with the %a format specifier to convert a floating-point number to its hexadecimal string.− f.format("%a", 298.45) Example 1: Basic Example In this example, we format the floating-point number '298.45' to its hexadecimal representation using the '%a' format specifier with a 'Formatter' object ... Read More

Format Specifier for Hash Code in Java

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

225 Views

Format specifier for Hash Code us %h.Firstly, create a new object for Formatter and Calendar −Formatter f = new Formatter(); Calendar c = Calendar.getInstance();For hash code −f.format("%h", c)The following is an example that finds the hash code −Example Live Demoimport java.util.Calendar; import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar c = Calendar.getInstance(); System.out.println("Hash Code: "+f.format("%h", c)); } }OutputHash Code: 4b899958

Formatter Specifier for Octal and Hexadecimal in Java

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

199 Views

For Formatter, import the following package −import java.util.Formatter;Now create a Formatter object like this −Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); Formatter f3 = new Formatter();If you want a Format specifier for Octal, use %o −f3.format("Octal values %o %o %o", 15, 55, 78);If you want a Format specifier for Hexadecimal, use %x −f2.format("Hexadecimal values %x %x %x", 24, 98, 110);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1 = new Formatter(); ... Read More

Format Output with Formatter in Java

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

343 Views

For Formatter, import the following package −import java.util.Formatter;Now create a Formatter object −Formatter f1 = new Formatter();Now, we will format the output with Formatter −f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5); ... Read More

Locale-specific formatting in Java

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

158 Views

For locale-specific formatting, firstly import the following packages.import java.util.Calendar; import java.util.Formatter; import java.util.Locale;Create a Formatter and Calendar object −Formatter f = new Formatter(); Calendar c = Calendar.getInstance();We are formatting for different Locales −f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc", c); f.format(Locale.ITALY, "Locale.ITALY: %tc", c);The following is an example −Example Live Demoimport java.util.Calendar; import java.util.Formatter; import java.util.Locale; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar c = Calendar.getInstance(); f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc", c); ... Read More

Get Operating System temporary directory / folder in Java

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

1K+ Views

To get the temporary directory in Java, use the System.getProperty() method. Use theIt’s syntax is −String getProperty(String key)Above, the key is the name of the system property. Since, we want the Java Home Directory name, therefore we will add the key as −java.io.tmpdirThe following is an example −Example Live Demopublic class Demo { public static void main(String []args){ String strTmp = System.getProperty("java.io.tmpdir"); System.out.println("OS current temporary directory: " + strTmp); System.out.println("OS Name: " + System.getProperty("os.name")); ... Read More

Java Program to return a Date set to the last possible millisecond of the minute

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

83 Views

Let us first set the calendar object −Calendar calendar = Calendar.getInstance();Use the getMaximum() method in Java to returns the maximum value for the given calendar field. We will use it to set the minute, second and milliseconds.For seconds −calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));For milliseconds −calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));The following is an example that returns a Date set to the last possible millisecond of the minute −Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar calendar = Calendar.getInstance();       // seconds       calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));       // milliseconds ... Read More

Advertisements