Found 4337 Articles for Java 8

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

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

654 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

146 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 adjust LocalDate to last Day of Month with TemporalAdjusters class

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

2K+ Views

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

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

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

570 Views

At first, get the current date:LocalDate listDays = LocalDate.now();Now, get the date for all the days of the current week:Arrays.asList(DayOfWeek.values()).stream().map(listDays::with).collect(toList()));Exampleimport java.time.DayOfWeek; import java.time.LocalDate; import java.util.Arrays; import static java.util.stream.Collectors.toList; public class Demo {    public static void main(String[] args) {       LocalDate listDays = LocalDate.now();       System.out.println("All the dates for the days in the week =          "+Arrays.asList(DayOfWeek.values()).stream().map(listDays::with).collect(toList()));    } }OutputAll the dates for the days in the week = [2019-04-15, 2019-04-16, 2019-04-17, 2019-04-18, 2019-04-19, 2019-04-20, 2019-04-21]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 30-Jul-2019 22:30:25

439 Views

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.At first, we have set the value to 0 and then considered a while loop. Since 0 and 1 aren’t prime numbers, we will be generating prime numbers after that:int val = 0; System.out.println("Prime Numbers..."); while (true) {    // conditions }Under while, we have set the following two condition. On increment, when the value of val become greater than 1 i.e.2, ... Read More

Java Program to generate random number with restrictions

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

751 Views

To generate random number with resctrictions, here we are taking an example of a phone number from India with the country code 91.First, we have set the first 2 numbers i.e. the country code. The variables are declared for each digit. We have also fixed the first digit of the number as 9 after country code 91:Random num = new Random(); int num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11; num0 = 9; num1 = 1; num2 = 9;Now, for rest of the numbers, use the nextInt() of the Random. The parameter consists of the bound ... Read More

Java Program to convert mathematical string to int

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

247 Views

To evaluate mathematical string to int, 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, use put() to set a key/value pair in the state of the ScriptEngine:scriptEngine.put("one", 10); scriptEngine.put("two", 50); scriptEngine.put("three", 40);Now, here is the mathematical string. Use eval to evaluate:String strExp = "(one + two - three) == 20"; Object evalExp = scriptEngine.eval(strExp);Exampleimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Demo {    public static void main(String[] args) {       ScriptEngineManager ... Read More

How to convert Long array list to long array in Java?

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

3K+ Views

Firstly, declare a Long array list and add some elements to it:ArrayList < Long > arrList = new ArrayList < Long > (); arrList.add(100000 L); arrList.add(200000 L); arrList.add(300000 L); arrList.add(400000 L); arrList.add(500000 L);Now, set the same size for the newly created long array:final long[] arr = new long[arrList.size()]; int index = 0;Each and every element of the Long array list is assigned to the long array:for (final Long value : arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class Demo {    public static void main(String[] args) {       ArrayList

Java Program to get the beginning and end date of the week

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

1K+ Views

At first, set a date:LocalDate date = LocalDate.of(2019, 4, 16);Now, get the date for the beginning of the week:LocalDate start = date; while (start.getDayOfWeek() != DayOfWeek.MONDAY) {    start = start.minusDays(1); }Now, get the date for the end of the week:LocalDate end = date; while (end.getDayOfWeek() != DayOfWeek.SUNDAY) {    end = end.plusDays(1); }Exampleimport java.time.DayOfWeek; import java.time.LocalDate; public class Demo {    public static void main(String[] argv) {       LocalDate date = LocalDate.of(2019, 4, 16);       System.out.println("Date = " + date);       LocalDate start = date;       while (start.getDayOfWeek() != DayOfWeek.MONDAY) { ... Read More

Advertisements