Tag : java


Java – Convert Date to Calendar example

In Java, you can use calendar.setTime(date) to convert a Date object to a Calendar object. Calendar calendar = Calendar.getInstance(); Date newDate = calendar.setTime(date); A Full example DateAndCalendar.java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;   public class DateAndCalendar { public static void main(String[] argv) throws ParseException {   //1.Continue Reading


Java – Convert String to Enum object

Review below Java enum example, how to convert a String like ‘arin’ to WhoisRIR.ARIN Enum object? 1. Problem WhoisRIR rir = //convert ‘arin’ to enum WhoisRIR.ARIN ? WhoisRIR.java package com.mkyong.whois.utils;   public enum WhoisRIR {   ARIN("whois.arin.net"), RIPE("whois.ripe.net"), APNIC("whois.apnic.net"), AFRINIC("whois.afrinic.net"), LACNIC("whois.lacnic.net"), JPNIC("whois.nic.ad.jp"), KRNIC("whois.nic.or.kr"), CNNIC("ipwhois.cnnic.cn"), //should query apnic UNKNOWN("");   privateContinue Reading


Java : Return a random item from a List

Normally, we are using the following ways to generate a random number in Java. 1. ThreadLocalRandom (JDK 1.7) //Generate number between 0-9 int index = ThreadLocalRandom.current().nextInt(10); 2. Random() //Generate number between 0-9 Random random = new Random(); int index = random.nextInt(10); 3. Math.random() //Generate number between 0-9 int index =Continue Reading


Java – Read a file from resources folder

In this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets : ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("file/test.xml").getFile()); 1.Continue Reading


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 – 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 : <-> 4294967295 <-> 3232235778 1. IP Address to Decimal We show you two ways to convert an IP address to a decimal number NormalContinue Reading


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


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


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


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


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 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


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


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

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