- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9318 Articles for Object Oriented Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
To calculate distance light travels, we need to follow the basic formulae to calculate distance.Distance = Speed x TimeHere, the following are the parameters −speed = 186000; days = 365; seconds = days * 24 * 60 * 60;Above, we calculated the time in seconds for an year −days = 365; seconds = days * 24 * 60 * 60;The following is the complete example to calculate distance light travels −Example Live Demopublic class Demo { public static void main(String[] args) { int speed; long days, seconds, dist; speed = 186000; days = 365; seconds = days * 24 * 60 * 60; dist = speed * seconds; System.out.print("Light travels: "+dist + " miles"); } }OutputLight travels: 5865696000000 miles
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
7K+ 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
499 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
630 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
608 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
You can display timezone easily in Java using SimpleDateFormat(“z”).Firstly, to work with SimpleDateFormat class in Java, import the following package −import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“z”) to display timezone −Format f = new SimpleDateFormat(”z”);Now, get the timezone in a string −String strTimeZone = f.format(new Date());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 Calendar cal = Calendar.getInstance(); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
772 Views
You can display AM/PM time marker easily in Java using SimpleDateFormat(“a”).Firstly, to work with SimpleDateFormat class in Java, import the following package.import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“a”) to display AM/PM marker −Format f = new SimpleDateFormat(”a”);Now, get the marker in a string −String strMarker = f.format(new Date());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 Calendar cal = Calendar.getInstance(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
828 Views
To work with SimpleDateFormat class in Java, import the following package.import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“ss”) to display seconds in two-digit.Format f = new SimpleDateFormat(”ss”);Now, get the seconds in a string −String strSeconds = f.format(new Date());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 Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s"); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
209 Views
To work with SimpleDateFormat class in Java, import the following package.import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“s”) to display seconds in one-digit.Format f = new SimpleDateFormat(”s”);Now, get the seconds in a string.String strSeconds = f.format(new Date());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 Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s"); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
199 Views
To work with SimpleDateFormat class in Java, import the following package.import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“hh”) to display hour −Format f = new SimpleDateFormat("hh");Now, get the hour in a string −String strHour = f.format(new Date());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 { Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s"); System.out.println("Today's date = "+simpleformat.format(cal.getTime())); ... Read More