Found 9150 Articles for Object Oriented Programming

Convert String to Long in Java

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

461 Views

To convert String to Long, use the valueOf() method.Let’s say the following is our string.// string String myStr = "5"; System.out.println("String: "+myStr);To convert it to Long, we have used valueOf() −Long longObj = Long.valueOf(myStr);The following is the complete example to convert a String value to Long −Example Live Demopublic class Demo { public static void main(String[] args) { // string String myStr = "5"; System.out.println("String: "+myStr); // long Long longObj = Long.valueOf(myStr); System.out.println("Converted to Long: "+longObj); } }OutputString: 5 Converted to Long: 5

Java program to get the minimum of three long values

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

94 Views

Firstly, let us declare and initialize three long values.long val1 = 88799; long val2 = 98567; long val3 = 98768;Now, find the minimum of three long values using the following condition −// checking for maximum if (val2 < val1) { val1 = val2; } if (val3 < val1) { val1 = val3; }The following is the complete example to get the minimum value −Example Live Demopublic class Demo { public static void main(String[] args) { long val1 = 88799; long ... Read More

Java Program to get the maximum of three long values

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

217 Views

Firstly, let us declare and initialize three long values −long val1 = 98799; long val2 = 98567; long val3 = 98768;Now, find the maximum of three long values using the following condition −// checking for maximum if (val2 > val1) { val1 = val2; } if (val3 > val1) { val1 = val3; }The following is the complete example to get the maximum value −Example Live Demopublic class Demo { public static void main(String[] args) { long val1 = 98799; ... Read More

Formatting Minute in m format in Java

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

369 Views

The “m” format is to format minutes i.e. 1, 2, 3, 4, etc. Here, we will use the following.SimpleDateFormat("m");Let us see an example −// displaying minutes in m format SimpleDateFormat simpleformat = new SimpleDateFormat("m"); String strMinute = simpleformat.format(new Date()); System.out.println("Minutes in m format = "+strMinute);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time ... Read More

Display hour in KK (00-11) format in Java

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

219 Views

The “KK” format in Java Date is used to display hour in 00-11.. Use SimpleDateFormat("KK") to get the same format −// displaying hour in KK format SimpleDateFormat simpleformat = new SimpleDateFormat("KK"); String strHour = simpleformat.format(new Date()); System.out.println("Hour in KK format = "+strHour);Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time ... Read More

Java program to calculate distance light travels

karthikeya Boyini
Updated on 27-Aug-2024 18:48:19

2K+ Views

In this article, we will demonstrate how to calculate the distance light travels in one year using Java. We will use the Demo class and the main method to perform this calculation. Speed of light: 186000Days = 365dist = speed * seconds Problem Statement Write a Java program to calculate the total distance that light travels in a year, given that the speed of light is 186, 000 miles per second. Output Light travels: 5865696000000 miles Steps to calculate distance light travels Following are the steps to calculate the distance light travels − Initialize the Demo class.Define ... Read More

Convert long primitive to Long object in Java

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

9K+ Views

To convert long primitive to Long object, follow the below steps.Let’s say the following is our long primitive.// primitive long val = 45; System.out.println("long primitive: "+val);Now, to convert it to Long object is not a tiresome task. Include the same long value while creating a new Long object −// object Long myObj = new Long(val); System.out.println("Long object: "+myObj);The following is the complete example −Example Live Demopublic class Demo { public static void main(String[] args) { // primitive long val = 45; ... Read More

Convert Long to numeric primitive data types in Java

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

707 Views

Let’s say we have Long object here.Long myObj = new Long("9879");Now, if we want to convert this Long to short primitive data type. For that, use the in-built shortValue() method −// converting to short primitive types short shortObj = myObj.shortValue(); System.out.println(shortObj);In the same way convert Long to another numeric primitive data type int. For that, use the in-built intValue() method −// converting to int primitive types int intObj = myObj.intValue(); System.out.println(intObj);The following is an example wherein we convert Long to numeric primitive types short, int, float, etc −Example Live Demopublic class Demo { public static void main(String[] args) ... Read More

Java Program to subtract long integers and check for overflow

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

787 Views

To check for Long overflow, we need to check the Long.MAX_VALUE with the subtracted long result. Here, Long.MAX_VALUE is the maximum value of Long type in Java.Let us see an example wherein long integers are subtracted and if the result is still more than the Long.MAX_VALUE, then an exception is thrown −The following is an example showing how to check for Long overflow −Example Live Demopublic class Demo { public static void main(String[] args) { long val1 = 70123; long val2 = 10567; ... Read More

SimpleDateFormat('zzzz') in Java

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

862 Views

TimeZone can be formatted in z, Z or zzzz formats.The following is an example that displays how to implement SimpleDateFormat('zzzz') −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s zzzz"); System.out.println("Today's date = "+simpleformat.format(cal.getTime())); // displaying hour ... Read More

Advertisements