Found 33676 Articles for Programming

Java program to convert millisecond to readable string

Alshifa Hasnain
Updated on 22-Jan-2025 18:36:33

808 Views

In this article, we will learn to convert milliseconds to readable strings in Java. Converting milliseconds into a human-readable format such as hours, minutes, seconds, and milliseconds is a common programming task, especially in time-sensitive applications. Problem Statement The challenge is to convert a given time in milliseconds into a readable string that shows hours, minutes, seconds, and remaining milliseconds. Input long millis = 5000000; Output  Hours = 1 Minutes = 23 Seconds = 20Milliseconds = 0 1 hr(s) 23 min(s) 20 sec(s) 0 ms Approaches to convert millisecond to readable string Following are the two different approaches to converting milliseconds ... Read More

Java Program to convert LocalDateTime to LocalDate and LocalTime

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

4K+ Views

At first set a LocalDateTime:LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime dateTime = LocalDateTime.of(date, time);Now, convert the LocalDateTime to LocalDate and LocalTime:LocalDate localDate = LocalDateTime.now().toLocalDate(); LocalTime localTime = LocalDateTime.now().toLocalTime();Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       LocalTime time = LocalTime.now();       LocalDateTime dateTime = LocalDateTime.of(date, time);       System.out.println("DateTime = "+dateTime);       LocalDate localDate = LocalDateTime.now().toLocalDate();       LocalTime localTime = LocalDateTime.now().toLocalTime();       System.out.println("Date = "+localDate);       System.out.println("Time = ... Read More

Java Program to get Tail Set from TreeSet

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

93 Views

Let us first create a TreeSet and add elements to it:TreeSet treeSet = new TreeSet(); treeSet.add(20); treeSet.add(50); treeSet.add(100); treeSet.add(120); treeSet.add(150); treeSet.add(200); treeSet.add(250); treeSet.add(300); treeSet.add(350); treeSet.add(400);Now, let us get Tail Set from the TreeSet. In the below case, we will get elements above 200:SortedSet set = treeSet.tailSet(200);Exampleimport java.util.SortedSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       TreeSet treeSet = new TreeSet();       treeSet.add(20);       treeSet.add(50);       treeSet.add(100);       treeSet.add(120);       treeSet.add(150);       treeSet.add(200);       treeSet.add(250);       treeSet.add(300); ... Read More

Java Program to get Sub Set from TreeSet

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

113 Views

Let us first create a TreeSet and add elements:TreeSet treeSet = new TreeSet(); treeSet.add(10); treeSet.add(20); treeSet.add(30); treeSet.add(40); treeSet.add(50); treeSet.add(60); treeSet.add(70); treeSet.add(80); treeSet.add(90); treeSet.add(100);Now, let’s say you need to set sub set from 50 to 70, then use the subset() for it:SortedSet sub = treeSet.subSet(50, 70); System.out.println("Sub Set = " + sub);Exampleimport java.util.TreeSet; import java.util.SortedSet; public class Demo {    public static void main(String[] args) {       TreeSet treeSet = new TreeSet();       treeSet.add(10);       treeSet.add(20);       treeSet.add(30);       treeSet.add(40);       treeSet.add(50);       treeSet.add(60);       ... Read More

How to fill array values in Java?

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

3K+ Views

Let us first create an int array −int[] arr = new int[10];Now, fill array values. Here, the numbers get added from the index 2 to 7 −Arrays.fill(arr, 2, 7, 100);Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int[] arr = new int[10];       System.out.println("Array = "+Arrays.toString(arr));       Arrays.fill(arr, 2, 7, 100);       System.out.println("Fill = "+Arrays.toString(arr));    } }OutputArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Fill = [0, 0, 100, 100, 100, 100, 100, 0, 0, 0]

Java Program to fill an array with random numbers

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

3K+ Views

Let us first crate an array −double[] arr = new double[5];Now, create Random class object −Random randNum = new Random();Now, fill the above array with random numbers −for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); }Example Live Demoimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String args[]) {       double[] arr = new double[5];       Random randNum = new Random();       for (int i = 0; i < 5; i++) {          arr[i] = randNum.nextInt();       }       System.out.println("Random numbers = "+Arrays.toString(arr));    } }OutputRandom numbers = [-6.59888981E8, 1.141160731E9, -9.931249E8, 1.335266582E9, 8.27918412E8]

Java program to shift array elements to the left

Alshifa Hasnain
Updated on 07-Aug-2025 18:19:53

994 Views

What Is Left Shifting in an Array? Shifting the array elements to the left by the given number of positions is also known as left rotation of the array. The element at the beginning gets pushed out of the front during the shift, but instead of being deleted, they are re-attached at the end of the array. Let us consider an example for a better understanding: Original Array: After 1 Left Shift: We can see in the above figure that, after the first shift, the element "1" is pushed out and then reattached at the end of the ... Read More

Java Program to create random BigInteger within a given range

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

367 Views

Create a Random class object −Random random = new Random();Now let us create a random BigInteger within the range set below −BigInteger i = new BigInteger(1024, random);Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String[] args) {       Random random = new Random();       BigInteger i = new BigInteger(1024, random);       System.out.println("Random BigInteger = "+i);    } }OutputRandom BigInteger = 172988250696765715389833755481951104504569480256142363412177847577736195982554760981595486734850686498607899498518922990162874103419942352546847073039976287403845518172884710426458735414702299598858093908453406020426533304237452347610248118892069951238970445771615614781904759399589093691142208659284351662649

Java Program to create a BigDecimal from a string type value

Alshifa Hasnain
Updated on 27-Dec-2024 19:15:53

508 Views

In this article, we will learn to create a BigDecimal from a string value in Java. The BigDecimal class in Java is highly versatile and often used for calculations requiring high precision, such as financial transactions or scientific computations. we'll explore two approaches to creating a BigDecimal from a string value, complete with examples. When to Use BigDecimal We can use the BigDecimal in the following conditions − When working with financial calculations requiring precision (e.g., currency operations). When handling scientific computations involving large or highly precise numbers. ... Read More

Java Program to adjust Adjust LocalDate to first Day Of next month with TemporalAdjusters class

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

348 Views

Let us first set a date −LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);Now, adjust LocalDate to first Day of next month −LocalDate day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());Example Live Demoimport java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo {    public static void main(String[] args) {       LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);       System.out.println("Current Date = "+localDate);       System.out.println("Current Month = "+localDate.getMonth());       LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());       System.out.println("First day of month = "+day);       day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());       System.out.println("First day of next month = "+day);    } }OutputCurrent Date ... Read More

Advertisements