
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

281 Views
Let us first create signed byte −byte signedVal = -100;Now convert a byte to an unsigned integer −int unsignedVal = Byte.toUnsignedInt(signedVal);Examplepublic class Demo { public static void main(String[] args) { byte signedVal = -100; int unsignedVal = Byte.toUnsignedInt(signedVal); System.out.println("Signed value (byte) = " + signedVal); System.out.println("Unsigned value (byte) = " + unsignedVal); } }OutputSigned value (byte) = -100 Unsigned value (byte) = 156

981 Views
In this article, we will learn to generate n distinct random numbers within a specified range, ensuring that no number is repeated in Java. Generating random numbers is a common problem in various applications such as simulations, games, and testing. Problem Statement In this problem, we are given a number n, and we need to generate n unique random numbers within a defined range, ensuring that no number is repeated. The range can be specified by the user, or it can be a default range based on the problem's requirements. Input int n = 10; Output [4, 6, 9, 1, ... Read More

593 Views
Create an input stream and set the string:DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));The getBytes() method is used to convert a string into sequence of bytes and returns an array of bytes.Now return one single input byte:(char) inputStream.readByte()Exampleimport java.io.ByteArrayInputStream; import java.io.DataInputStream; public class Demo { public static void main(String[] args) throws Exception { DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes())); System.out.print((char) inputStream.readByte()); System.out.print((char) inputStream.readByte()); inputStream.close(); } }OutputPq

2K+ Views
Let’s say the following is our string array and we need to sort it:String[] str = { "Tom", "Jack", "Harry", "Zen", "Tim", "David" };Within the sort() method, create a customized comparator to sort the above string. Here, two strings are compared with each other and the process goes on:Arrays.sort(str, new Comparator < String > () { public int compare(String one, String two) { int val = two.length() - one.length(); if (val == 0) val = one.compareToIgnoreCase(two); return val; } });Exampleimport java.util.Arrays; import java.util.Comparator; public class Demo ... Read More

303 Views
Create two time instants:Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50);Use between() to get the duration between two time instants:long resMilli = Duration.between(time1, time2).toMillis();Exampleimport java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50); long resMilli = Duration.between(time1, time2).toMillis(); System.out.println("Duration between two time intervals = "+resMilli); } }OutputDuration between two time intervals = 50000

484 Views
At first, set the Clock:Clock clock = Clock.systemUTC();Now, create LocalDateTime:LocalDateTime dateTime = LocalDateTime.now(clock);Exampleimport java.time.Clock; import java.time.LocalDateTime; public class Demo { public static void main(String[] args) { Clock clock = Clock.systemUTC(); System.out.println("Clock = "+Clock.systemDefaultZone()); LocalDateTime dateTime = LocalDateTime.now(clock); System.out.println("LocalDateTime = "+dateTime); } }OutputClock = SystemClock[Asia/Calcutta] LocalDateTime = 2019-04-19T09:29:50.605820900

504 Views
At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10);Now, format the datetime as BASIC_ISO_DATE format:String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);Exampleimport java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 6, 20, 10); System.out.println("DateTime = "+dateTime); String str = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); System.out.println("Formatted date = "+str); } }OutputDateTime = 2019-09-06T20:10 Formatted date = 20190906

182 Views
At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20);Now, format the datetime as ISO_WEEK_DATE format:String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);Exampleimport java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20); System.out.println("DateTime = "+dateTime); String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE); System.out.println("Formatted date = "+str); } }OutputDateTime = 2019-09-09T10:20 Formatted date = 2019-W37-1

4K+ Views
At first, set the date:LocalDateTime dateTime = LocalDateTime.of(2019, Month.JULY, 9, 10, 20);Now, format the datetime as ISO_DATE_TIME format:String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);Exampleimport java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.of(2019, Month.JULY, 9, 10, 20); System.out.println("DateTime = "+dateTime); String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME); System.out.println("Formatted date = "+str); } }OutputDateTime = 2019-07-09T10:20 Formatted date = 2019-07-09T10:20:00

120 Views
To format and display datetime, you need to use DateTimeFormatter and use the pattern as:DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");Above, the z is the timezone:MMM dd yyyy hh:mm a zNow, use the following for zone:ZonedDateTime dateTime = ZonedDateTime.now();Exampleimport java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] argv) { DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z"); ZonedDateTime dateTime = ZonedDateTime.now(); String res = dateTime.format(dtFormat); System.out.printf("Date = %s %n", res); } }OutputDate = Apr 14 2019 01:35 PM IST