Found 7442 Articles for Java

Java Program to generate random numbers with no duplicates

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

3K+ Views

For random numbers in Java, create a Random class object −Random randNum = new Random();Now, create a HashSet to get only the unique elements i.e. no duplicates −Setset = new LinkedHashSet();Generate random numbers with Random class nextInt −while (set.size() < 5) {    set.add(randNum.nextInt(5)+1); }Exampleimport java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class Demo {    public static void main(final String[] args) throws Exception {       Random randNum = new Random();       Setset = new LinkedHashSet();       while (set.size() < 5) {          set.add(randNum.nextInt(5)+1);       }       System.out.println("Random numbers with no duplicates = "+set);    } }OutputRandom numbers with no duplicates = [2, 4, 1, 3, 5]

Java program to shuffle an array using list

Alshifa Hasnain
Updated on 07-Aug-2025 17:33:30

272 Views

In this article, we will learn how to shuffle an array using a list in Java. To do so, we will be using: Using Collections.shuffle() Method Using Fisher-Yates shuffle Using Java Streams Using Collections.shuffle() Method Shuffling an array means randomly rearranging its elements. The Java Collections framework provides the Collections.shuffle() Method, which shuffles a list. Since this method shuffles the list randomly, the order of the resultant elements is different each time we use it. Example Following is the program to shuffle an array using a ... Read More

Java Program to convert java.util.Date to any local date in certain timezone

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

215 Views

First, set the Date and ZoneId −Date date = new Date(); ZoneId zone = ZoneId.systemDefault();Now convert the java.util.date to localdate −date.toInstant().atZone(zone).toLocalDate() date.toInstant().atZone(zone).toLocalTime() date.toInstant().atZone(zone).getHour() date.toInstant().atZone(zone).getMinute() date.toInstant().atZone(zone).getSecond()Exampleimport java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       Date date = new Date();       ZoneId zone = ZoneId.systemDefault();       System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());       System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());       System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());       System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());       System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());    } }OutputLocalDate = 2019-04-18 LocalTime= 23:25:09.708 Hour = 23 Minute = 25 Seconds = 9Read More

How to get number of quarters between two dates in Java

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

625 Views

Let’s say we have the following two dates −LocalDate.of(2019, 3, 20); LocalDate.of(2019, 10, 25);To get the number of quarters between the above two dates, use the QUARTER_YEARS −IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20),LocalDate.of(2019, 10, 25));Exampleimport java.time.LocalDate; import java.time.temporal.IsoFields; public class Demo {    public static void main(String[] args) {       long quarters =          IsoFields.QUARTER_YEARS.between(LocalDate.of(2019, 3, 20), LocalDate.of(2019, 10, 25));       System.out.println("Quarters between the two dates = " + quarters);    } }OutputQuarters between the two dates = 2

How do I discover the Quarter of a given Date in Java?

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

2K+ Views

Let us first get the current date −LocalDate currentDate = LocalDate.now();Now, use the Calendar class and set the locale −Calendar cal = Calendar.getInstance(Locale.US);Now, get the month −int month = cal.get(Calendar.MONTH);Find the Quarter −int quarter = (month / 3) + 1;Exampleimport java.time.LocalDate; import java.util.Calendar; import java.util.Locale; public class Demo {    public static void main(String[] args) {       LocalDate currentDate = LocalDate.now();       System.out.println("Current Date = "+currentDate);       Calendar cal = Calendar.getInstance(Locale.US);       int month = cal.get(Calendar.MONTH);       int quarter = (month / 3) + 1;       System.out.println("Quarter = "+quarter);    } }OutputCurrent Date = 2019-04-12 Quarter = 2

Java Program to get Temporal Queries precision

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

148 Views

To get Temporal Queries precision, use the TemporalQuery interface with the precision() method of the TemporalQueries −TemporalQueryprecision = TemporalQueries.precision(); Get the precision for LocalDate: LocalDate.now().query(precision) Get the precision for LocalTime: LocalTime.now().query(precision) Get the precision for YearMonth: YearMonth.now().query(precision) Get the precision for Year: Year.now().query(precision)Exampleimport java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Year; import java.time.YearMonth; import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; public class Demo {    public static void main(String[] args) {       TemporalQueryprecision = TemporalQueries.precision();       System.out.println("TemporalQueries precision...");       System.out.println(LocalDate.now().query(precision));       System.out.println(LocalTime.now().query(precision));       System.out.println(LocalDateTime.now().query(precision));       System.out.println(YearMonth.now().query(precision));       System.out.println(Year.now().query(precision)); ... Read More

Java program to get the seconds since the beginning of the Java epoch

Samual Sam
Updated on 21-Oct-2024 17:41:50

850 Views

In this article, we will learn to get the seconds since the beginning of the Java epoch. To get the seconds since the beginning of epochs, you need to use Instant. The method here used is ofEpochSecond() method. The epoch is the number of seconds that have elapsed since 00::00:00 Thursday, 1 January 1970. Get the seconds with ChronoUnit.SECONDS − long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS); Steps to retrieve seconds since Epoch Following are the steps to retrieve seconds since Epoch − First, we will import the Instant and ChronoUnit classes from the java.time and java.time.temporal packages. ... Read More

Java Program to get day of week as string

Shriansh Kumar
Updated on 16-Aug-2024 07:28:17

3K+ Views

Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat. In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date. Using LocalDate Class In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day ... Read More

Java Program to get timezone id strings

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

225 Views

Let us get the timezone id strings for timezone America. For this, use the get.AvailableZoneIds() method −ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America")) .forEach(System.out::println);With that, we have used the forEach to display all the timezones in America −America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon . . .Exampleimport java.time.ZoneId; public class Demo {    public static void main(String[] args) {       System.out.println("TimeZones in the US");       ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America"))       .forEach(System.out::println);    } }OutputTimeZones in the US America/Cuiaba America/Marigot America/El_Salvador America/Guatemala America/Belize America/Panama America/Managua America/Indiana/Petersburg America/Chicago America/Tegucigalpa America/Eirunepe America/Miquelon America/Argentina/Catamarca America/Grand_Turk America/Argentina/Cordoba America/Araguaina America/Argentina/Salta America/Montevideo America/Manaus ... Read More

Java program to get milliseconds between dates

karthikeya Boyini
Updated on 05-Sep-2024 11:22:45

2K+ Views

In this article, we will learn to get milliseconds between dates in Java. We will be using the LocalDateTime class from java.time package and ChronoUnit.MILLIS of java.time.temporal package. ChronoUnit is an enum that is part of Java date and time API which represents a unit of time that is days, hours, minutes etc. Here, the MILLIS is a unit that represents the concept of a millisecond. Problem Statement Write a program in Jave to get milliseconds between dates. Below is the representation − Input Date One = 2024-09-04T05:40:19.817038951Date Two = 2019-04-10T11:20 Output Milliseconds between two dates = -170533219817 Steps to get milliseconds between dates ... Read More

Advertisements