Spring Profiles example

Spring @Profile allow developers to register beans by condition. For example, register beans based on what operating system (Windows, *nix) your application is running, or load a database properties file based on the application running in development, test, staging or production environment. In this tutorial, we will show you aContinue 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 TimeZoneExample.java 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


Spring Caching and Ehcache example

In this tutorial, we will show you how to enable data caching in a Spring application, and integrate with the popular Ehcache framework. Tools used Ehcache 2.9 Spring 4.1.4.RELEASE Logback 1.0.13 Maven 3 / Gradle 2 JDK 1.7 Eclipse 4.4 Note Spring supports caching since version 3.1 Spring cache hasContinue Reading


Ehcache hello world example

In this tutorial, we will show you two examples to help you getting started with Ehcache. Tools used : Ehcache 2.9 Maven 3 Gradle 2 JDK 1.7 P.S Ehcache required JDK 1.5 or above. 1. Project Directory Structure 2. Hello World Assume this is a Maven project : pom.xml <dependency>Continue Reading


Logback – Duplicate log messages

Review a simple Java application and log a message via Logback. App.java package com.mkyong.test;   import org.slf4j.Logger; import org.slf4j.LoggerFactory;   public class App {   private static final Logger log = LoggerFactory.getLogger(App.class);   public static void main(String[] args) { log.debug("Testing"); } } P.S Tested with Logback 1.1.2 1. Problem AContinue Reading


Ehcache Logging example

Ehcache is using SLF4j logging, to log stuff, put a slf4j implementation in the project classpath, in this example, we use logback. Tools used : Ehcache 2.9 Maven 3 logback 1.0.13 1. Project Directory Structure 2. Project Dependencies pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> 3.Continue Reading


Apache Ant Tutorial

Apache Ant tutorial with full example, including Ant installation, create JAR file, WAR file, working with dependency with Ivy tools , running jUnit and TestNG test and etc


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

testng tutorials

Ant and TestNG Task example

In this tutorial, we will show you how to run a TestNG test in Ant build. 1. Run by Classes build.xml <taskdef name="testng" classname="org.testng.TestNGAntTask"> <classpath location="lib/testng-6.8.14.jar" /> </taskdef>   <target name="testng" depends="compile">   <!– Assume test.path contains the project library dependencies –> <testng classpathref="test.path" outputDir="${report.dir}" haltOnFailure="true">   <!– Extra projectContinue Reading


Maven – Get source code for Jar

In Maven, you can get source code for project dependencies in most IDEs like this : $ mvn dependency:sources $ mvn dependency:resolve -Dclassifier=javadoc Get source code for Jar Get javadoc for Jar, normally, developers don’t provide this. 1. Eclipse IDE In Eclipse IDE, it’s better to use the maven eclipseContinue Reading


Ant and jUnit Task example

In this tutorial, we will show you how to run a junit test in Ant build. 1. Run a unit test build.xml <target name="junit" depends="compile">   <junit printsummary="yes" haltonfailure="no">   <!– Project classpath, must include junit.jar –> <classpath refid="test.path" />   <!– test class –> <classpath location="${test.classes.dir}" />   <testContinue Reading


Must include junit.jar if not in Ant’s own classpath

Declares a junit task in Ant like this build.xml <!– Run jUnit –> <target name="junit" depends="resolve">   <junit printsummary="yes" haltonfailure="no">   <classpath refid="test.path" /> <classpath location="${build.dir}" />   <test name="com.mkyong.test.TestMessage" haltonfailure="no" todir="${report.dir}" outfile="result"> <formatter type="plain" /> <formatter type="xml" /> </test>   </junit> </target> Run ant junit, but hits the followingContinue 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


How to install Apache Ant on Windows

To install Apache Ant on Windows, you just need to download the Ant’s zip file, and Unzip it, and configure the ANT_HOME Windows environment variables. Tools Used : JDK 1.7 Apache Ant 1.9.4 Windows 8.1 1. JAVA_HOME Make sure JDK is installed, and JAVA_HOME is configured as Windows environment variable.Continue Reading

Page 1 of 10012345...102030...Last »