The difference between two LocalDateTime objects can be obtained using the until() method in the LocalDateTime class in Java. This method requires two parameters i.e. the end date for the LocalDateTime object and the Temporal unit. Also, it returns the difference between two LocalDateTime objects in the Temporal unit specified.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; import java.time.temporal.*; public class Demo { public static void main(String[] args) { LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30"); LocalDateTime ldt2 = LocalDateTime.parse("2019-02-19T12:21:30"); System.out.println("The first LocalDateTime is: " + ldt1); ... Read More
The equals() method of the AbstractList class is used to compare the specified object with this list for equality. The value TRUE is returned if both the lists are same i.e the same size and elements.The syntax is as followspublic boolean equals(Object ob)Here, ob is the object is to be compared for equality. To work with the AbstractList class, import the following packageimport java.util.AbstractList;The following is an example to implement equals() method of the AbstractlList class in JavaExample Live Demoimport java.util.ArrayList; import java.util.AbstractList; public class Demo { public static void main(String[] args) { AbstractList myList = new ... Read More
To get the element with a max id, you can use the find() method. To understand the above concept, let us create a collection with the document. The query is as follows −> db.getElementWithMaxIdDemo.insertOne({"Name":"John", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbce480f10143d8431e1c") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Larry", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcec80f10143d8431e1d") } > db.getElementWithMaxIdDemo.insertOne({"Name":"David", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcf580f10143d8431e1e") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Chris", "Age":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcfe80f10143d8431e1f") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Robert", "Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbd0880f10143d8431e20") }Display all documents from ... Read More
There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. First way is using a stringstream to read words separated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. For example, >Example Code Live Demo#include #include #include #include using namespace std; int main() { string str("Hello from the dark side"); ... Read More
In some situations, we should open you tube application from our application to show some specific video. This example demonstrate about How android YouTube app Play Video from Intent.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view. When you click on text view, it will open YouTube application.Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; ... Read More
To select rows with more than 2 decimal places, use SUBSTR() function from MySQL. Let us first create a table −mysql> create table selectRows2DecimalPlacesDemo -> ( -> Amount varchar(100) -> ); Query OK, 0 rows affected (0.73 sec)Following is the query to insert records in the table using insert command −mysql> insert into selectRows2DecimalPlacesDemo values('234.5678'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRows2DecimalPlacesDemo values('19.50'); Query OK, 1 row affected (0.19 sec) mysql> insert into selectRows2DecimalPlacesDemo values('23.456'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRows2DecimalPlacesDemo values('12.123'); Query OK, 1 row affected (0.14 ... Read More
To format and display date like this i.e. YearMonth, you need to set the pattern:uuuuMMAt first, set a LocalDate:LocalDate localDate = LocalDate.now();Now format and display date as ‘201904’:localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))Exampleimport java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("Date = "+localDate); System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))); } }OutputDate = 2019-04-19 Date (Year and Month) = 201904
You can use aggregate function AVG() for this. Let us first create a table −mysql> create table DemoTable ( Value1 int, Value2 int, Value3 int ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 20, 30); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(13, 15, 18); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(21, 31, 41); Query OK, 1 row affected (0.21 sec)Display records from the table using select command −mysql> select *from DemoTable;This will produce the following ... Read More
You need to use subquery with select statement, one select for inner and one for outer. The inner select will return rows and outer will order by ascending order. The syntax is as follows:SELECT *FROM ( SELECT *FROM yourTableName ORDER BY yourColumnName1 DESC LIMIT 9 ) AS anyAliasName ORDER BY yourColumnName2;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table OrderByAfterLimit -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserName varchar(20), -> UserAge int, -> PRIMARY KEY(Id) -> ); Query OK, ... Read More
A new DoubleBuffer can be allocated using the method allocate() in the class java.nio.DoubleBuffer. This method requires a single parameter i.e. the capacity of the buffer. It returns the new DoubleBuffer that is allocated. If the capacity provided is negative, then the IllegalArgumentException is thrown.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { DoubleBuffer buffer = DoubleBuffer.allocate(5); buffer.put(4.5D); buffer.put(1.2D); ... Read More