Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Java[jdk] download, installation and setting path

Java+download+install+setting+path...

Click here to download java installation document



Regular expressions java

Regular+expressions+java...

Java supports regular expressions through the classes in the java.util.regex package in the standard Java library. While there are some differences in advanced features supported by the Java regular expression library compared to PCRE, they both share a large part of the syntax and patterns and expressions can be used in Java and other languages.

In the examples below, be sure to import the following package at the top of the source file if you are trying out the code:

import java.util.regex.*;

Regular expression String patterns

In Java, regular strings can contain special characters (also known as escape sequences) which are characters that are preceeded by a backslash ( \ ) and identify a special piece of text like a newline ( \n ) or a tab character ( \t ). As a result, when writing regular expressions in Java code, you need to escape the backslash in each metacharacter to let the compiler know that it's not an errant escape sequence.

For example, take the pattern "There are \d dogs" . In Java, you would escape the backslash of the digit metacharacter by using the escape sequence \\ (effectively escaping the backslash with itself) to create the pattern "There are \\d dogs" .

This is only necessary when hard-coding patterns into Java code, as strings that are read in from user input or from files are read by character individually and escape sequences are not interpreted. This is a common approach to get around this problem, by either putting the patterns in a Properties or resource file so they can be easier to read and understand.

Other languages like C# or Python support the notion of raw strings, but Java has yet to add this useful feature into the core language.

Matching a string

Working with regular expressions in Java generally involves instantiating a Pattern, and matching it against some text. The simplest way to do this is to call the static method Pattern.matches(), which takes an input string and the regular expression to match it against, and simply returns whether the pattern matches the string.  

Method
boolean isMatch = Pattern.matches(String regex, String inputStr)

However, this does not give you any additional information such as where in the input string the pattern matches, or the groups that matched. So for most purposes, it is both more useful and also more efficient to compile a new Pattern and then use it to create a new Matcher for each input string that you are matching against, which will hold the results of the match.

Methods
Pattern ptrn = Pattern.compile(String regex) Matcher matcher = ptrn.matcher(String inputStr)
Example
// Lets use a regular expression to match a date string. Pattern ptrn = Pattern.compile("([a-zA-Z]+) (\\d+)"); Matcher matcher = ptrn.matcher("June 24"); if (matcher.matches()) { // Indeed, the expression "([a-zA-Z]+) (\d+)" matches the date string // To get the indices of the match, you can read the Matcher object's // start and end values. // This will print [0, 7], since it matches at the beginning and end of the // string System.out.println("Match at index [" + matcher.start() + ", " + matcher.end() + ")"); // To get the fully matched text, you can read the Matcher object's group // This will print "June 24" System.out.println("Match: " + matcher.group()); }

Capturing groups

Capturing groups in a regular expression is as straightforward as matching a string in the example above. After using a Pattern to match an input string, you can just iterate through the extracted groups in the returned Matcher .

Example
// Lets use a regular expression to capture data from a few date strings. String pattern = "([a-zA-Z]+) (\\d+)"; Pattern ptrn = Pattern.compile("([a-zA-Z]+) (\\d+)"); Matcher matcher = ptrn.matcher("June 24, August 9, Dec 12"); // This will print each of the matches and the index in the input string // where the match was found: // June 24 at index [0, 7) // August 9 at index [9, 17) // Dec 12 at index [19, 25) while (matcher.find()) { System.out.println(String.format("Match: %s at index [%d, %d]", matcher.group(), matcher.start(), matcher.end())); } // If we are iterating over the groups in the match again, first reset the // matcher to start at the beginning of the input string. matcher.reset(); // For each match, we can extract the captured information by reading the // captured groups. while (matcher.find()) { // This will print the number of captured groups in this match System.out.println(String.format("%d groups captured", matcher.groupCount())); // This will print the month and day of each match. Remember that the // first group is always the whole matched text, so the month starts at // index 1 instead. System.out.println("Month: " + matcher.group(1) + ", Day: " + matcher.group(2)); // Each group in the match also has a start and end index, which is the // index in the input string that the group was found. System.out.println(String.format("Month found at[%d, %d)", matcher.start(1), matcher.end(1))); }

Finding and replacing strings

Another common task is to find and replace a part of a string using regular expressions, for example, to replace all instances of an old email domain, or to swap the order of some text. You can do this in Java with the Matcher.replaceAll() and Matcher.replaceFirst() methods. Both these methods first reset the matcher to start at the beginning of the input string up to either the end of the string, or the end of the first match respectively.

The replacement string can contain references to captured groups in the pattern (using the dollar sign $), or just a regular literal string.

Method
String replacedString = matcher.replaceAll(String inputStr)
String replacedString = matcher.replaceFirst(String inputStr)
Example
// Lets try and reverse the order of the day and month in a few date // strings. Notice how the replacement string also contains metacharacters // (the back references to the captured groups) so we use a verbatim // string for that as well. Pattern ptrn = Pattern.compile("([a-zA-Z]+) (\\d+)"); Matcher matcher = ptrn.matcher("June 24, August 9, Dec 12"); // This will reorder the string inline and print: // 24 of June, 9 of August, 12 of Dec // Remember that the first group is always the full matched text, so the // month and day indices start from 1 instead of zero. String replacedString = matcher.replaceAll("$2 of $1"); System.out.println(replacedString);

Pattern Flags

When compiling a Pattern , you will notice that you can pass in additional flags to change how input strings are matched. Most of the available flags are a convenience and can be written into the into the regular expression itself directly, but some can be useful in certain cases.

  • Pattern.CASE_INSENSITIVE makes the pattern case insensitive so that it matches strings of different capitalizations
  • Pattern.MULTILINE is necessary if your input string has newline characters (\n) and allows the start and end metacharacter (^ and $ respectively) to match at the beginning and end of each line instead of at the beginning and end of the whole input string
  • Pattern.DOTALL allows the dot metacharacter (.) to match new line characters as well
  • Pattern.LITERAL makes the pattern literal, in the sense that the escaped characters are matched as-is. For example, the pattern "\d" will match a backslash followed by a 'd' character as opposed to a digit character

Links

For more information about using regular expressions in Java, please visit the following links:

Web application status check example

Click here to download eclipse supported ZIP file



This is app.properties properties file and these properties are used in the application.




ContextRoot1=127.0.0.1:5050
ContextRoot2=127.0.0.1:8080
ContextRoot3=127.0.0.1:7070


This is ServerStatus.java file having the source code to execute business logic.



 

    
package com.cv.java.server.info;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author Chandra Vardhan
 *
 */
public class ServerStatus {

  private static Properties appProperties = new Properties();

  private static List<String> servers = new ArrayList<String>();

  static {
    appProperties = readPropertiesFile();
    setValues(appProperties);
  }

  private static void setValues(final Properties properties) {
    if (properties != null) {
      String contextRoot1 = properties.getProperty("ContextRoot1");
      if (contextRoot1 != null && !contextRoot1.trim().isEmpty()) {
        servers.add("ContextRoot1:" + contextRoot1);
      }
      String contextRoot2 = properties.getProperty("ContextRoot2");
      if (contextRoot2 != null && !contextRoot2.trim().isEmpty()) {
        servers.add("ContextRoot2:" + contextRoot2);
      }
      String contextRoot3 = properties.getProperty("ContextRoot3");
      if (contextRoot3 != null && !contextRoot3.trim().isEmpty()) {
        servers.add("ContextRoot3:" + contextRoot3);
      }
    }

  }

  /**
   * 
   */
  public ServerStatus() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    ServerStatus serverStatus = new ServerStatus();
    for (String server : servers) {
      String info = serverStatus.statusCheck(server);
      System.out.println(info);
    }
  }

  private String statusCheck(String server) {
    String[] ipPorts = server.split(":");
    String contextRoot = ipPorts[0];
    String ipAdd = ipPorts[1];
    int port = Integer.parseInt(ipPorts[2]);
    boolean status = isServerUp(ipAdd, port);
    String info = "";
    if (status) {
      info = contextRoot + " application is running at IPAddress : " + ipAdd + " and port : " + port;
    } else {
      info = contextRoot + " application is NOT available at IPAddress : " + ipAdd + " and port : " + port;;
    }
    return info;
  }

  public boolean isServerUp(final String ipAddress, final int port) {
    boolean isUp = false;
    try {
      Socket socket = new Socket(ipAddress, port);
      // Server is up
      isUp = true;
      socket.close();
    } catch (IOException e) {
      // Server is down
      isUp = false;
    }
    return isUp;
  }

  private static Properties readPropertiesFile() {
    Properties props = new Properties();
    try {
      System.out.println("Reading properties file : app.properties!!!");
      props.load(ServerStatus.class.getClassLoader().getResourceAsStream("app.properties"));
    } catch (Exception e) {
      System.err.println("Problem while reading properties file!!!");
    }
    return props;
  }
  
}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.java.server.info</groupId> <artifactId>WebApplicationStatusCheck</artifactId> <version>1.0</version> </project>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Timer task example

Click here to download eclipse supported ZIP file



This is TimerTaskForAnHour.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every sixty minutes once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForAnHour {

  private static final long EVERY_60_MINUTES = 1000L * 60L * 60L;

  /**
   * 
   */
  public TimerTaskForAnHour() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every sixty 60 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every thirty minutes...
    timer.schedule(hourlyTask, 0l, EVERY_60_MINUTES);

  }

}


This is TimerTaskForFiveMinutes.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every five minutes once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForFiveMinutes {

  private static final long EVERY_5_MINUTES = 1000L * 60L * 5L;

  /**
   * 
   */
  public TimerTaskForFiveMinutes() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every five 5 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every five minutes...
    timer.schedule(hourlyTask, 0l, EVERY_5_MINUTES);

  }

}


This is TimerTaskForHalfAnHour.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every thirty minutes once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForHalfAnHour {

  private static final long EVERY_30_MINUTES = 1000L * 60L * 30L;

  /**
   * 
   */
  public TimerTaskForHalfAnHour() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every thirty 30 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every thirty minutes...
    timer.schedule(hourlyTask, 0l, EVERY_30_MINUTES);

  }

}


This is TimerTaskForOneDay.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every one day once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForOneDay {

  private static final long EVERY_24_HOURS = 1000L * 60L * 60L * 24L;

  /**
   * 
   */
  public TimerTaskForOneDay() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one day : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every thirty
    // minutes...
    timer.schedule(hourlyTask, 0l, EVERY_24_HOURS);

  }

}


This is TimerTaskForOneMinute.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every one minute once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForOneMinute {

  private static final long EVERY_1_MINUTE = 1000L * 60L * 1L;

  /**
   * 
   */
  public TimerTaskForOneMinute() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one 1 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every one minute...
    timer.schedule(hourlyTask, 0l, EVERY_1_MINUTE);

  }

}


This is TimerTaskForOneWeeK.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every one week once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForOneWeeK {

  private static final long EVERY_ONE_WEEK = 1000L * 60L * 60L*24L*7L;

  /**
   * 
   */
  public TimerTaskForOneWeeK() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one week : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every one week
    timer.schedule(hourlyTask, 0l, EVERY_ONE_WEEK);

  }

}




This is TimerTaskForParticularDay.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every sixty minutes once.
 */
package com.cv.java.task;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForParticularDay {

  private static final long EVERY_ONE_WEEK = 1000L * 60L * 60L * 24L * 7L;

  /**
   * 
   */
  public TimerTaskForParticularDay() {
  }

  public static void main(String[] args) {
    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    date.set(Calendar.HOUR, 12);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 0);
    // Schedule to run every Sunday in midnight
    TimerTask weeklyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every SUNDAY 12.00 AM : : " + new Date());
      }
    };

    timer.schedule(weeklyTask, date.getTime(), EVERY_ONE_WEEK);
  }// Main method ends
}// MainApplication ends




This is TimerTaskForTenMinutes.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every ten minutes once.
 */
package com.cv.java.task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class TimerTaskForTenMinutes {

  private static final long EVERY_10_MINUTES = 1000L * 60L * 10L;

  /**
   * 
   */
  public TimerTaskForTenMinutes() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every ten 10 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every ten minutes...
    timer.schedule(hourlyTask, 0l, EVERY_10_MINUTES);

  }

}




This is EveryFiveMinutesTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every five minutes once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class EveryFiveMinutesTaskStartAtSpecific {

  private static final long EVERY_5_MINUTES = 1000L * 60L * 5L;

  /**
   * 
   */
  public EveryFiveMinutesTaskStartAtSpecific() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 10);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 0);
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every five 5 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every five minutes...
    timer.schedule(hourlyTask, date.getTime(), EVERY_5_MINUTES);

  }

}


This is EveryHalfAnHourTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every thirty minutes once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class EveryHalfAnHourTaskStartAtSpecific {

  private static final long EVERY_30_MINUTES = 1000L * 60L * 30L;

  /**
   * 
   */
  public EveryHalfAnHourTaskStartAtSpecific() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 2);
    date.set(Calendar.MINUTE, 20);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 1);
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every thirty 30 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every thirty
    // minutes...
    timer.schedule(hourlyTask, date.getTime(), EVERY_30_MINUTES);

  }

}


This is EveryHourTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every sixty minutes once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class EveryHourTaskStartAtSpecific {

  private static final long EVERY_ONE_HOUR = 1000L * 60L * 60L;

  /**
   * 
   */
  public EveryHourTaskStartAtSpecific() {
  }

  public static void main(String[] args) {
    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 2);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 1);
    // Schedule to run every hour
    TimerTask hourlyTask = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one hour : : " + new Date());
      }
    };

    timer.schedule(hourlyTask, date.getTime(), EVERY_ONE_HOUR);
  }// Main method ends
}// MainApplication ends


This is EveryOneDayTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every one day once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class EveryOneDayTaskStartAtSpecific {

  private static final long EVERY_24_HOURS = 1000L * 60L * 60L * 24L;

  /**
   * 
   */
  public EveryOneDayTaskStartAtSpecific() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 2);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 1);
    TimerTask daily = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one day : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every thirty
    // minutes...
    timer.schedule(daily, date.getTime(), EVERY_24_HOURS);

  }

}


This is EveryOneMinuteTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every one minute once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class EveryOneMinuteTaskStartAtSpecific {

  private static final long EVERY_1_MINUTE = 1000L * 60L * 1L;

  /**
   * 
   */
  public EveryOneMinuteTaskStartAtSpecific() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 2);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 1);
    TimerTask everyMinute = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every one 1 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every one minute...
    timer.schedule(everyMinute, date.getTime(), EVERY_1_MINUTE);

  }

}


This is EveryTenMinutesTaskStartAtSpecific.java file having the source code to execute business logic.



 

    
/**
 * This is an example program and it will execute for every ten minutes once.
 */
package com.cv.java.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Chandra Vardhan
 *
 */
public class EveryTenMinutesTaskStartAtSpecific {

  private static final long EVERY_10_MINUTES = 1000L * 60L * 10L;

  /**
   * 
   */
  public EveryTenMinutesTaskStartAtSpecific() {
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR, 2);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, 1);
    TimerTask tenMinute = new TimerTask() {
      @Override
      public void run() {
        System.out.println("Will run for every ten 10 minutes : : " + new Date());
      }
    };

    // schedule the task to run starting now and then every ten minutes...
    timer.schedule(tenMinute, date.getTime(), EVERY_10_MINUTES);

  }

}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.java.timer</groupId> <artifactId>TimerTask</artifactId> <version>1.0</version> </project>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n