How to add one day to a date?(Java)

How to add one day to a date? Given a Date dt you have several possibilities: Solution 1: You can use the Calendar class for that: Date dt = new Date(); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DATE, 1); dt = c.getTime(); Solution 2: You should seriously consider using the Joda-Time library, because of the various shortcomings of the Date class. With Joda-Time you can do the following: Date dt = new Date(); DateTime dtOrg = new DateTime(dt); DateTime dtPlusOne = dtOrg.plusDays(1); Solution 3: With Java 8 you can also use the new JSR 310 API (which is inspired by Joda-Time): LocalDateTime.from(dt.toInstant()).plusDays(1);

Read More

The difference between regular (non-static) and static methods

The difference between regular (non-static) and static methods Java is a Object Oriented Programming(OOP) language, which means we need objects to access methods and variables inside of a class. However this is not always true. While discussing static keyword in java, we learned that static members are class level and can be accessed directly without any instance. In this article we will see the difference between static and non-static methods. Static Method Example class StaticDemo { public static void copyArg(String str1, String str2) { //copies argument 2 to arg1 str2 = str1; System.out.println(“First…

Read More

Java ArrayBlockingQueue

Java ArrayBlockingQueue ArrayBlockingQueue is based on a bounded Java array. Bounded means it will have a fixed size, once created it cannot be resized. This fixed size collection can be understood using this Java List size puzzle. ArrayBlockingQueue uses the “Blocking” type method from the four available types described in the Java BlockingQueue tutorial. Attempt to put into the empty ArrayBlockingQueue and take will be blocking the thread. BlockingQueueProducer.java This is a producer which produces three string values and put() to the BlockingQueue. These three elements are produced between varied intervals. package com.javapapers.java.collections; import java.util.concurrent.BlockingQueue; public class BlockingQueueProducer implements…

Read More

What is the difference between an int variable and a short variable in Java?

What is the difference between an int variable and a short variable in Java? short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters. int: By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8…

Read More

Java Ternary Operator

Java Ternary Operator Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one liner replacement for if-then-else statement and used a lot in java programming. We can use ternary operator to replace switch also as shown in below example. The first operand in java ternary operator should be a boolean or a statement with boolean result. If the first operand is true then java ternary operator returns second operand else it returns third operand. Syntax of java ternary operator is: result = testStatement ?…

Read More

How to synchronize ArrayList in java with example and What is Collections.synchronizedlist ?

How to synchronize ArrayList in java with example and What is Collections.synchronizedlist ? Description: The ArrayList is not synchornized (not thread-safe). Collections.synchronizedList() method helps to get thread safe list. Collections.synchronizedList() method Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. here are two ways to synchronize explicitly: Using Collections.synchronizedList() method Using thread-safe variant of ArrayList: CopyOnWriteArrayList Example 1: Collections.synchronizedList() method for Synchronizing ArrayList In this example we…

Read More