Found 7442 Articles for Java

Java Program to get the value stored in a byte as an unsigned integer

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

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

Java program to generate n distinct random numbers

Alshifa Hasnain
Updated on 22-Jan-2025 15:48:12

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

Java Program to create Stream from a String/Byte Array

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

How to sort an array with customized Comparator in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

Java Program to get duration between two time instants

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

Java Program to create LocalDateTime from Clock

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

Java Program to format LocalTimeDate as BASIC_ISO_DATE format

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

Java Program to format LocalDateTime as ISO_WEEK_DATE format

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

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

How to format Java LocalDateTime as ISO_DATE_TIME format

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

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

Java Program to format date as Apr 14 2019 01:35 PM IST

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

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

Advertisements