It can be checked if the duration is negative or not using the isNegative() method in the Duration class in Java. This method requires no parameters. Also, it returns true if the duration is negative and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.time.Duration; public class Demo { public static void main(String[] args) { Duration d = Duration.ofSeconds(5); boolean flag = d.isNegative(); System.out.println("The duration is: " + d); if(flag) System.out.println("The above duration is negative"); else ... Read More
The IntStream min() method in the Java IntStream class is used to get the minimum element from the stream. It returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty.The syntax is as followsOptionalInt min()Here, OptionalInt is a container object which may or may not contain an int value.Create an IntStream and add some elementsIntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);Now, get the minimum element of the streamOptionalInt res = intStream.min();The following is an example to implement IntStream min() method in Java. The isPresent() method of the OptionalInt ... Read More
You can achieve this with the help of an aggregate framework. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700871e9c5dd6f1f78296") } > db.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77008f1e9c5dd6f1f78297") } > db.countGroupByDemo.insertOne({"StudentId":20, "StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700971e9c5dd6f1f78298") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700a21e9c5dd6f1f78299") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700aa1e9c5dd6f1f7829a") } ... Read More
In order to display the indexes of a collection, you can use getIndexes(). The syntax is as follows −db.yourCollectionName.getIndexes();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.indexDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f7c4f2f684a30fbdfd599") } > db.indexDemo.insertOne({"StudentName":"Mike", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f7c552f684a30fbdfd59a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.indexDemo.insertOne({"StudentName":"Carol", "StudentAge":20});The following is the output −{ "acknowledged" : true, "insertedId" : ObjectId("5c8f7c5e2f684a30fbdfd59b") ... Read More
The CachedRowSet is the base implementation of disconnected row sets. It connects to the data source, reads data from it, disconnects with the data source and the processes the retrieved data, reconnects to the data source and writes the modifications.Creating a CachedRowSetYou can create a Cached RowSet object using the createCachedRowSet() method of the RowSetFactory.You can create a RowSetFactory object using the newfactory() method of the RowSetProvider method.Create a CachedRowSet object using the above-mentioned methods as shown below −//Creating the RowSet object RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet rowSet = factory.createCachedRowSet();Connecting to the data sourceAfter creating a RowSet object you need ... Read More
Search for a text in MongoDBs Double Nested Array with the help of dot(.) notation. Let us first create a collection. Following is the query to create a collection with documents> db.doubleNestedArrayDemo.insertOne( ... { ... "StudentId" : "1000", ... "StudentName" : "Larry", ... "StudentDetails" : [ ... { ... "ProjectName" : "Online Banking", ... "ProjectDetails" : [ ... { ... ... Read More
To create custom DateTime formatter, use DateTimeFormatter. Let us first see for Time −DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter();For Date −dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("/") .appendValue(ChronoField.MONTH_OF_YEAR) .appendLiteral("/") .appendValue(ChronoField.DAY_OF_MONTH) .toFormatter();Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; public class Demo { public static void main(String[] args) { DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter(); System.out.println("Time = "+dtFormat.format(LocalDateTime.now())); dtFormat = new DateTimeFormatterBuilder() ... Read More
Let us first create a collection with documents −> db.likeDemo.insertOne({"Name":"John", Age:32}); { "acknowledged" : true, "insertedId" : ObjectId("5cb84984623186894665ae41") } > db.likeDemo.insertOne({"Name":"Chris", Age:25}); { "acknowledged" : true, "insertedId" : ObjectId("5cb84991623186894665ae42") } > db.likeDemo.insertOne({"Name":"Carol", Age:22}); { "acknowledged" : true, "insertedId" : ObjectId("5cb849a1623186894665ae43") } > db.likeDemo.insertOne({"Name":"Johnny", Age:22}); { "acknowledged" : true, "insertedId" : ObjectId("5cb849b2623186894665ae44") } > db.likeDemo.insertOne({"Name":"James", Age:27}); { "acknowledged" : true, "insertedId" : ObjectId("5cb849bb623186894665ae45") }Following is the query to display all documents from the collection with the help of find() method −> db.likeDemo.find().pretty();This will produce the following output −{ "_id" : ... Read More
You can use ORDER BY ASC to order timestamp values in ascending order with TIMESTAMP() method.The following is the syntax using TIMESTAMP() −SELECT timestamp( yourTimestampColumnName ) as anyAliasName From yourTableName order by 1 ASCTo understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table Timestamp_TableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> yourTimestamp timestamp -> ); Query OK, 0 rows affected (0.83 sec)Now you can insert some records in the table using insert ... Read More
Before getting into example, we should know what Intent service is in android. Intent Service is going to do back ground operation asynchronously. When user call startService() from activity , it doesn't create instance for each request. It going to stop service after done some action in service class or else we need to stop service using stopSelf().This example demonstrate about How to start intent service for every 10 sec’s.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 ... Read More