Tag : java

DNS

Java – Get nameservers of a website

In this tutorial, we will show you how to get the nameservers of a website using Java + dig command. 1. Using Dig Command 1.1 On Linux, we can use dig command to query a DNS lookup of a website, for example : $ dig any mkyong.com   //… mkyong.com.Continue Reading

java-ip-to-decimal

Java – Convert IP address to Decimal Number

In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vice versa. For examples : 255.255.255.255 <-> 4294967295 192.168.1.2 <-> 3232235778 1. IP Address to Decimal We show you two ways to convert an IP address to a decimal number NormalContinue Reading

lshiftLeft

Java and “& 0xFF” example

Before you understand what is & 0xFF, make sure you know following stuffs : Bitwise AND operator, link. Converts hex to/from binary, and decimal to/from binary. In short, & 0xFF is used to make sure you always get the last 8 bits. Let’s see an example to convert an IPContinue Reading

Geolocation

Java – find location using Ip Address

In this example, we show you how to find a location (country, city, latitude, longitude) using an IP address. 1. GeoLite Database The MaxMind provides a free GeoLite database (IP Address to Location). Get a free GeoLite free Databases – here Get a GeoIP client Java APIs – here StartContinue Reading

java_logo_100

How to join two Lists in Java

In this article, we show you 2 examples to join two lists in Java. JDK – List.addAll() Apache Common – ListUtils.union() 1. List.addAll() example Just combine two lists with List.addAll(). JoinListsExample.java package com.mkyong.example;   import java.util.ArrayList; import java.util.List;   public class JoinListsExample {   public static void main(String[] args) {Continue Reading

java_logo_100

How to convert String to Date – Java

In this tutorial, we will show you how to convert String to Date. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. Refer table below for some of the common date and time patterns used in java.text.SimpleDateFormat. Letter Description ExamplesContinue Reading

java_logo_100

Convert String with commas to Long – Java

A short guide to show you how to convert a String with commas to a long type. 1. For a normal String, you can use Long.valueOf to convert it directly. String bigNumber = "1234567899"; long result = Long.valueOf(bigNumber); 2. For a String with commas, you can use java.text.NumberFormat to convertContinue Reading

ascii-art-java

ASCII Art Java example

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text. P.S This example is credit to this post and this post. ASCIIArt.java package com.mkyong;   import java.awt.Font; import java.awt.Graphics; importContinue Reading

cvs-file

How to read and parse CSV file in Java

A comma-separated values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma “,”). For example : US,United States MY,Malaysia In this tutorial, we show you how to read, parse and print out the values from a csvContinue Reading

world-country

Display a list of countries in Java

In this article, we show you how to use the Locale class to play around the list of countries. P.S Tested with JDK 1.6 1. List of Countries The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166. ListCountry.java package com.webmitta.model;   import java.util.Locale;  Continue Reading

default thumbnail

How to get client Ip Address in Java

Normally, you can use servletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application. String ipAddress = request.getRemoteAddr(); But, if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IPContinue Reading

time-date-different-in-Java

How to calculate date and time difference in Java

In this tutorial, we show you 2 examples to calculate date / time difference in Java : Manual time calculation. Joda time library. 1. Manual time calculation Converts Date in milliseconds (ms) and calculate the differences between two dates, with following rules : 1000 milliseconds = 1 second 60 secondsContinue Reading

google pagerank

How to get Google PageRank (PR) in Java

In this example, we will show you how to get Google PageRank (PR) in Java. To request a PageRank for “mkyong.com”, you just need to send following HTTP request : http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&ch=6236440745 &ie=UTF-8&oe=UTF-8&features=Rank&q=info:mkyong.com P.S Above URL is used by Google toolbar plugin. The tricky part is following hashing value : ch=6236440745Continue Reading

search-file

Search directories recursively for file in Java

Here’s an example to show you how to search a file named “post.php” from directory “/Users/mkyong/websites” and all its subdirectories recursively. FileSearch.java package com.mkyong;   import java.io.File; import java.util.ArrayList; import java.util.List;   public class FileSearch {   private String fileNameToSearch; private List<String> result = new ArrayList<String>();   public String getFileNameToSearch()Continue Reading

java_logo_100

How to count duplicated items in Java List

A Java example to show you how to count the total number of duplicated entries in a List, using Collections.frequency and Map. CountDuplicatedList.java package com.mkyong;   import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap;   public class CountDuplicatedList {   public staticContinue Reading

Page 1 of 1512345...10...Last »