Java Articles - Page 303 of 540

How can I display Java date as '12/04/2019'

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

117 Views

To format and display date like this i.e. Day/Month/Year, you need to set the pattern:dd/MM/yyyyAt first, set a LocalDate:LocalDate localDate = LocalDate.now();Now format and display date as '12/04/2019':localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))Exampleimport java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("Date = "+date);       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");       System.out.println("Formatted Date = "+date.format(formatter));    } }OutputDate = 2019-04-12 Formatted Date = 12/04/2019

How to format and display date in Java as '201904'

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

135 Views

To format and display date like this i.e. YearMonth, you need to set the pattern:uuuuMMAt first, set a LocalDate:LocalDate localDate = LocalDate.now();Now format and display date as ‘201904’:localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))Exampleimport java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDate localDate = LocalDate.now();       System.out.println("Date = "+localDate);       System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM")));    } }OutputDate = 2019-04-19 Date (Year and Month) = 201904

Java program to convert millisecond to readable string

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

955 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 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

1K+ 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

445 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 get first Friday in a month

Samual Sam
Updated on 29-Sep-2024 02:47:00

773 Views

In this article, we'll explore how to get the first Friday of any given month in Java. The program will use Java's LocalDate class and TemporalAdjusters to automatically calculate the first Friday based on the input date. We will walk through how this works step by step and see how the code performs this task. Problem Statement Write a program in Java to get the first Friday in a month. Below is a demonstration of the same − Input Current date = 2019-04-01 Output Current date = 2019-04-01 First Friday date = 2019-04-05 Steps to get the first Friday in a ... Read More

Java Program to get day of week for the last day of each month in 2019

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

392 Views

For last day of each month, at first create a List collection for day of week −ListdayOfWeek = new ArrayList();Now, get day of week for the last day of each month in 2019, set the year using withYear() and use lastDayOfMonth() and getDayOfWeek methods −for (Month m: Month.values()) {    DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); }Example Live Demoimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] argv) {       ListdayOfWeek = new ArrayList();       for (Month m: Month.values()) {          DayOfWeek days = ... Read More

Java program to get display name for day of week in different locale

Samual Sam
Updated on 23-Nov-2024 03:47:56

424 Views

In this article, we will learn how to get the display name for a day of the week in different locales using Java. The DayOfWeek class in Java provides methods to work with days of the week, and with the help of getDisplayName(), you can retrieve the name of a day in different formats based on the locale. Java.util.Locale.getDisplayName() Method The java.util.Locale.getDisplayName(Locale inLocale) method returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. Steps to get the display ... Read More

Java Program to get the reverse of the NavigableSet

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

141 Views

We will create a NavigableSet collection and add some elements −NavigableSet set = new TreeSet(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU");Now, use descendingSet() for the reverse of NavigableSet −NavigableSetreverse = set.descendingSet();Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add("ABC");       set.add("DEF");       set.add("GHI");       set.add("JKL");       set.add("MNO");       set.add("PQR");       set.add("STU");       NavigableSetreverse = set.descendingSet();       System.out.println("NavigableSet = " + set);       ... Read More

Advertisements