
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

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]

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

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

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

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

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

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

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

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

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