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
Java Articles - Page 381 of 745
354 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
4K+ 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
2K+ Views
In this article, we'll explore how to determine the start and end dates of a week using Java. Specifically, we'll write a program that takes a given date and calculates the Monday and Sunday of that week. Problem Statement Write a Java program to find the beginning (Monday) and end (Sunday) dates of the week for a given date. Input Date = 2019-04-16 Output Start of the Week = 2019-04-15End of the Week = 2019-04-21 Steps to calculate week's start and end dates Following are the steps to get the beginning and end date of the week − ... Read More
1K+ Views
Let us first set two dates:LocalDate date1 = LocalDate.of(2019, 4, 16); LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);Now, count the dates between both the above dates using between():int numDays = Period.between(date1, date2).getDays();Exampleimport java.time.LocalDate; import java.time.Month; import java.time.Period; public class Demo { public static void main(String[] argv) { LocalDate date1 = LocalDate.of(2019, 4, 16); LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04); int numDays = Period.between(date1, date2).getDays(); System.out.println("Number of days between two dates = "+numDays); } }OutputNumber of days between two dates = 18
2K+ Views
At first, display the current date:LocalDate date = LocalDate.now();Now, get the day of week from the above Date (current date):DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));On the basis of the above result, use SWITCH to check for the current day. If the day is SATURDAY/SUNDAY, then it’s Weekend.Exampleimport java.time.DayOfWeek; import java.time.temporal.ChronoField; import java.time.LocalDate; public class Demo { public static void main(String[] argv) { LocalDate date = LocalDate.now(); DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK)); switch (day) { case SATURDAY: System.out.println("Weekend - Saturday"); ... Read More
1K+ Views
Set the two Java dates:LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29);Now, get the difference between two dates with Period class between() method:Period p = Period.between(date1, date2);Now, get the years, month and days:p.getYears() p.getMonths() p.getDays()Exampleimport java.time.LocalDate; import java.time.Period; public class Demo { public static void main(String[] args) { LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29); System.out.println("Date 1 = "+date1); System.out.println("Date 2 = "+date2); Period p = Period.between(date1, date2); System.out.println("Period = "+p); ... Read More
683 Views
In this program, we will learn to add a specific period to a date using Java’s LocalDate class. By using Java's Period class, we can specify an amount of time (such as months and days) and then add this period to a LocalDate. The program demonstrates how to set a period and apply it to a given date to get a new, updated date. Steps to add Period to LocalDate Following are the steps to add Period to LocalDate − Import LocalDate and Period from java.time package to work with dates and periods. ... Read More
274 Views
Let us first set an Instant:Instant now = Instant.ofEpochMilli(184142540078l);Let us now minus seconds from Instant:Instant resSeconds = now.minusSeconds(50);Let us now minus nanoseconds from Instant:Instant resNanoSeconds = now.minusNanos(10000);Exampleimport java.time.Instant; public class Demo { public static void main(String[] args) { Instant now = Instant.ofEpochMilli(184142540078l); System.out.println(now); Instant resSeconds = now.minusSeconds(50); System.out.println("After subtracting seconds = "+resSeconds); Instant resNanoSeconds = now.minusNanos(10000); System.out.println("After subtracting nanoseconds = "+resNanoSeconds); } }Output1975-11-02T06:42:20.078Z After subtracting seconds = 1975-11-02T06:41:30.078Z After subtracting nanoseconds = 1975-11-02T06:42:20.077990Z
3K+ Views
The following are the two Instant timestamps:Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935);Get the Duration between both the Instant:Duration res = Duration.between(one, two);Now, get the seconds between the two timestamps:long seconds = res.getSeconds();Now, get the minutes between the two timestamps:long minutes = res.abs().toMinutes();Exampleimport java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935); Duration res = Duration.between(one, two); System.out.println(res); long seconds = res.getSeconds(); System.out.println("Seconds between Durations = "+seconds); ... Read More
21K+ Views
Let us first set two Instants with ofEpochSeconds() method using seconds from the epoch of 1970-01- 01T00:00:00Z.Now get the duration between the above two Instant:Duration res = Duration.between(one, two);Exampleimport java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant one = Instant.ofEpochSecond(1845836728); Instant two = Instant.ofEpochSecond(1845866935); Duration res = Duration.between(one, two); System.out.println(res); } }OutputPT8H23M27S