Found 7442 Articles for Java

Java program to create a TreeSet with custom Comparator

Alshifa Hasnain
Updated on 11-Mar-2025 19:33:32

379 Views

In this article, we will learn to create a TreeSet with a custom Comparator in Java. TreeSet is a set that stores elements in sorted order. By default, elements are sorted in natural ordering (numbers in ascending order). Understanding TreeSet and Comparators Following are some key points for TressSet and Comparators − A TreeSet is a part of the Java Collections Framework and implements the NavigableSet interface. It automatically sorts elements as they are added. By default, it sorts elements using their natural ordering (determined by the ... Read More

Java Program to convert Instant to LocalDateTime

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

575 Views

Let’s say you need to convert Instant to LocalDateTime with IST with timezone:Create an Instant:Instant instant = new Date().toInstant();Now, convert Instant to LocalDateTime:LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("IST")));Exampleimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       Instant instant = new Date().toInstant();       LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("IST")));       System.out.println("Date (IST) = " + date);       date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("PST")));       System.out.println("Date (PST) = " + date);       date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("EST")));       System.out.println("Date (EST) = " ... Read More

Java Program to adjust LocalDate to next Tuesday with TemporalAdjusters class

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

149 Views

At first, set a LocalDate:LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 2);Now, adjust the LocalDate to next Tuesday using next() method:LocalDate date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));Exampleimport java.time.DayOfWeek; import 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.FEBRUARY, 2);       System.out.println("Current Date = "+localDate);       System.out.println("Current Month = "+localDate.getMonth());       LocalDate date = localDate.with(TemporalAdjusters.firstDayOfMonth());       System.out.println("First day of month = "+date);       date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));       System.out.println("Next Tuesday date = "+date);    } }OutputCurrent Date = 2019-02-02 Current ... Read More

Java Program to adjust LocalDate to last Day of Year with TemporalAdjusters class

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

821 Views

Let us first set a date:LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 11);Now, adjust LocalDate to last Day of year:LocalDate day = localDate.with(TemporalAdjusters.lastDayOfYear());Exampleimport 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.FEBRUARY, 11);       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.lastDayOfMonth());       System.out.println("Last day of month = "+day);       day = localDate.with(TemporalAdjusters.lastDayOfYear());       ... Read More

Java Program to convert HashSet to Enumeration

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

241 Views

Let’s say the following is our HashSet:HashSet set = new HashSet(); set.add("P"); set.add("Q"); set.add("R"); set.add("S"); set.add("T"); set.add("U"); set.add("V"); set.add("W"); set.add("X"); set.add("Z");Now convert the above HashSet to Enumeration:Enumeration enumeration = Collections.enumeration(set); while (enumeration.hasMoreElements())    System.out.println(enumeration.nextElement());Exampleimport java.util.Collections; import java.util.Enumeration; import java.util.HashSet; public class Demo {    public static void main(String[] args) {       HashSet set = new HashSet();       set.add("P");       set.add("Q");       set.add("R");       set.add("S");       set.add("T");       set.add("U");       set.add("V");       set.add("W");       set.add("X");       set.add("Z");   ... Read More

Java program to compare two sets

Shriansh Kumar
Updated on 12-Apr-2025 16:31:57

2K+ Views

The given task is to write Java programs that compare two sets and check if they are equal or not. Here, Set is an interface of the Java Collection Framework that extends the Collection interface and stores unique elements. Set depicts the features of a mathematical set. Hence, it allows all those operations that we can perform on a mathematical set, such as union, comparison, intersection, etc. Since Set is an interface, we can't use its functionalities directly. For this purpose, we need a TreeSet class that implements the Set Interface and can access its methods. How to Compare Two Sets in ... Read More

Java program to adjust LocalDate to last day of month with TemporalAdjusters class

Krantik Chavan
Updated on 08-Aug-2024 17:38:19

3K+ Views

In this article, we will learn the LocalDate, Month, and TemporalAdjusters classes from java.time package. These classes are essential for handling date and time operations in Java. LocalDate represents a date without time, Month is an enumeration of the twelve months, and TemporalAdjusters provides utility methods for common date adjustments. We will learn how to set a specific date and adjust it to find the first and last days of the month and the first day of the next month. Steps to adjust LocalDate to last day of month with TemporalAdjusters class Following are the steps ... Read More

Java program to get date for all the days of the current week

Krantik Chavan
Updated on 18-Oct-2024 11:57:36

1K+ Views

In this article, we will learn to get date for all the days of the current week using Java. We will use the java.time package to get the current date and calculate the dates for the rest of the week based on the current day.  Steps to get date for all the days of the current week Following are the steps to get date for all the days of the current week − First we will import the DayOfWeek and LocalDate from java.time and Arrays class from java.util. We will get the current ... Read More

Java Program to parse a mathematical expression and operators

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

2K+ Views

At first, we have set the mathematical expressions:String one = "10+15*20-5/5"; String two = "3+5-6"; String three = "9+2*(6-3+7)";To parse mathematical expression, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.For scripting, use the ScriptEngineManager class for the engine:ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("Nashorn");Now for JavaScript code from string, use eval i.e. execute the script. Here, we are parsing mathematical expressions set above:Object expResult1 = scriptEngine.eval(one); Object expResult2 = scriptEngine.eval(two); Object expResult3 = scriptEngine.eval(three);Exampleimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Demo {    public static void main(String[] args) throws ... Read More

Java program to get the prime numbers with BigInteger type

Nancy Den
Updated on 04-Nov-2024 18:44:41

609 Views

In this article, we will learn how to generate prime numbers using the BigInteger class in Java. A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Here, we have the BigInteger type, which has operations for modular arithmetic, GCD calculation, primality testing, prime generation, etc. Steps to get the prime numbers with BigInteger type Following are the steps to get the prime numbers with BigInteger type − First, we will import the BigInteger from the java.math package. ... Read More

Advertisements