All posts in category : java


Java – Get number of available processors

A code snippet to show you how to get the number of available processors / cores / CPUs in your environment. int processors = Runtime.getRuntime().availableProcessors(); System.out.println(processors); Output 8 P.S Tested with Intel(R) Core(TM) i7-4770 CPU @3.40GHz


Jsoup – Check Redirect URL

In this article, we will show you how to use Jsoup to check if an URL is going to redirect. 1. URL Redirection Normally, a redirect URL will return an HTTP code of 301 or 307, and the target URL will be existed in the response header “location” field. ReviewContinue Reading


Java – Convert date and time between timezone

In this tutorial, we will show you 3 examples (Date, Calendar and Joda Time) to convert a date and time to different time zones. This example will convert date and time from (GMT+8:00) Asia/Singapore – Singapore Time Date : 22-1-2015 10:15:55 AM to (GMT-5:00) America/New_York – Eastern Standard Time DateContinue Reading


Java – Display list of TimeZone with GMT

This Java example shows you how to display a list of TimeZone with GMT in front. P.S Tested with JDK 1.7 package com.mkyong.test;   import java.util.TimeZone; import java.util.concurrent.TimeUnit;   public class TimeZoneExample {   public static void main(String[] args) {   String[] ids = TimeZone.getAvailableIDs(); for (String id :Continue Reading


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


Convert DateTime to Date, but TimeZone is missing?

A code snippet to use Joda Time to convert a java.util.Date to different timezone : //java.util.Date : 22-1-2015 10:15:55 AM //System TimeZone : Asia/Singapore   //Convert java.util.Date to America/New_York TimeZone DateTime dt = new DateTime(date); DateTimeZone dtZone = DateTimeZone.forID("America/New_York"); DateTime dtus = dt.withZone(dtZone); //21-1-2015 09:15:55 PM – Correct!   //ConvertContinue 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 ? package com.mkyong.whois.utils;   public enum WhoisRIR {   ARIN(""), RIPE(""), APNIC(""), AFRINIC(""), LACNIC(""), JPNIC(""), KRNIC(""), CNNIC(""), //should query apnic UNKNOWN("");   privateContinue Reading


Java – Cron job to run a jar file

Quartz is good, but often times we just need a simple scheduler system to run a jar file periodically. On *unix system, you can use the build-in cron to schedule a scheduler job easily. In this example, we will show you how to create a cron job on *nix toContinue 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


jsoup : Send search query to Google

This example shows you how to use jsoup to send a search query to Google. Document doc = Jsoup .connect(""); .userAgent("Mozilla/5.0") .timeout(5000).get(); Unusual traffic from your computer network Don’t use this example to spam Google, you will get above message from Google, read this Google answer. 1. jsoup example ExampleContinue 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 : Unsupported major.minor version 51.0

1. Problem Run a “jar” on Debian 7.5, and hits the following error messages : $ java -jar ripecrawler.jar Exception in thread "main" java.lang.UnsupportedClassVersionError: com/mkyong/whois/job/RipeCrawlJob : Unsupported major.minor version 51.0   at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass( at at at$000( at$ at Method) at atContinue Reading


servlet-api-2.5.jar – jar not loaded

Deployed a “war” file on Tomcat, and hits following error messages : Jul 17, 2014 7:59:55 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(D:\apache-tomcat-7.0.53\webapps\hc\WEB-INF\lib\servlet-api-2.5.jar) – jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class Tools used : JDK1.7 Maven 3 Tomcat 7 1. Reason The Tomcat’s container comes with ownContinue 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   //… Reading


Find out your Java heap memory size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm. Heap sizes Initial heap size of 1/64 of physical memory up to 1Gbyte Maximum heapContinue Reading

Page 1 of 1712345...10...Last »