Java Time Difference

Dec 24, 2017 2 min.

Time difference can be calculated by subtracting the milliseconds. And then milliseconds can be converted into different Units using TimeUnit. e.g. TimeUnit.MILLISECONDS.toSeconds(1000) will give 1 because 1 second equivalent to 1000 millisecons. Following sample gives a example of finding difference between two dates and converting them into different units import java.util.Date; import java.util.concurrent.TimeUnit; public class TimeDifference { public static void main(String[] args) { Date date1 = new Date(System.currentTimeMillis() - (24 * 60 * 60 * 1000)); Date date2 = new Date(); long timeDifferenceInDays = TimeUnit.

JAXRS Rest Client

Dec 20, 2017 1 min.

Option 1 : Using interface proxy import javax.ws.rs.FormParam; import javax.ws.rs.HeaderParam; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import org.apache.cxf.jaxrs.client.JAXRSClientFactory; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; public class RestClient { public static void main(String[] args) { final MyApiClient client = JAXRSClientFactory.create("<http://localhost:8080/api>", MyApiClient.class); final HTTPClientPolicy clientPolicy = WebClient.getConfig(client).getHttpConduit().getClient(); clientPolicy.setConnectionTimeout(15 * 1000); clientPolicy.setReceiveTimeout(15 * 1000); final Response response = client.hello("your-authorization-token", "javawiki"); System.out.println("Response Status: " + response.getStatus()); } } @Path("/testapi") public interface MyApiClient { @PUT @Path("/hello") Response hello(@HeaderParam("AUTHORIZATION") String authorization, @FormParam("name") String name); } Option 2: Direct client import javax.

Java get hostname and IP address

Dec 19, 2017 1 min.

Use java.net.InetAddress to get hostname or IP Address To get local hostname or IP import java.net.InetAddress; public class LocalIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.getLocalHost(); // Get Hostname System.out.println(inetAddress.getHostName()); // Get IP Address System.out.println(inetAddress.getHostAddress()); } } $javac LocalIPGetter.java $java LocalIPGetter mytestpc 172.17.0.2 To get remote hostname or IP import java.net.InetAddress; public class RemoteIPGetter { public static void main(String[] args) throws Exception { InetAddress inetAddress = InetAddress.

MySQL MariaDB Set utf8mb4 as default charset

Nov 22, 2017 2 min.

By default charset is set to to latin1 and utf8. This can be checked by executing the following command MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%'; +--------------------------+-------------------+ | Variable_name | Value | +--------------------------+-------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | collation_connection | utf8_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +--------------------------+-------------------+ To set the charset as utf8mb4 do the following steps:

Install Mariadb in Aws Ec2

Nov 22, 2017 1 min.

Setup MariaDB Execute the following commands: curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash sudo apt install mariadb-server During this setup, it will ask for root password. Enter and keep a note of it. Verify Installation Verify correct version installed by executing mysql --version Login as root to check the installation mysql -u root -p Enable Network access To access the database from outside localhost comment the bind-address in in /etc/mysql/my.

Git Squash

Nov 10, 2017 1 min.

Option 1 to squash last two commits git rebase -i HEAD~2 This will open editor where you can choose which commit to keep and which to squash. Also you have option to rewrite messages. Option 2 Reset and commit again: git reset --soft HEAD~2 git commit -m "New message for the combined commit"public class Test { public static void main(String[] args) { System.out.println("hello"); } } <section id="main"> <div> <h1 id="title">{{ .

TomEE datasource password encryption

Nov 9, 2017 1 min.

Execute the following command to get the cipher: cd <<tomcat_home>>/bin tomee.sh cipher <<orginal password>> e.g. tomee.sh cipher Passw0rd This will give the cipher something like below xMH5uM1V9vQzVUv5LG7YLA== Use this cipher in the resources.xml with the prefix ‘cipher:Static3DES:’ as given below <resources> <Resource id="jdbc/myDS" type="javax.sql.DataSource"> password = cipher:Static3DES:xMH5uM1V9vQzVUv5LG7YLA== </Resource> </resources> alternatively you can give as below <resources> <Resource id="jdbc/myDS" type="javax.sql.DataSource"> password = xMH5uM1V9vQzVUv5LG7YLA== PasswordCipher = Static3DES </Resource> </resources>

Ubuntu Netbeans.desktop file

Nov 8, 2017 1 min.

File: /usr/share/applications/netbeans.desktop [Desktop Entry] Encoding=UTF-8 Name=NetBeans IDE 8.2 Comment=The Smarter Way to Code Exec=/apps/netbeans-8.2/bin/netbeans Icon=/apps/netbeans-8.2/nb/netbeans.png Categories=Application;Development;Java;IDE Version=1.0 Type=Application Terminal=0 StartupNotify=true StartupWMClass=NetBeans IDE 8.

Google Sign-in Developer Error

May 27, 2017 1 min.

Resolving google sign-in error: Status{statusCode=DEVELOPER_ERROR, resolution=null} Follow these steps: Go to play console: https://play.google.com/apps/publish/ Go to “Release Management” -> “App signing” Copy the SHA1 from “App signing certificate” -> “SHA-1 certificate fingerprint” Go to Firebase console: https://console.firebase.google.com Select project -> Project Settings -> Select App Click “Add Fingerprint” Paste the SHA1 copied in the previous steps.

Avoid NullPointerException on Primitive Wrapper Objects

Feb 22, 2017 5 min.

Using the primitive wrapper objects like Boolean, Integer or Long in operators may throw NullPointerException, if the value is null, because the primitive wrapper objects auto unboxed to primitive values when you use java operators like ‘==’ ‘>’ ‘<’, etc. e.g. private Boolean isDone() { return null; } private Long getMyLongValue() { return null; } public void someMethod() { // Throw NPE if (isDone()) { } // Throw NPE if (getMyLongValue() > 10) { } } This will throw NullPointerException if the method ‘isDone()’ or ‘getMyLongValue()’ returns null.