regex for percentage with two decimal places in java

public class Validate {

private static Pattern pattern;

private static Matcher matcher;

private static final String PERCENTAGE_PATTERN =”[0-9]{1,13}(\\.[0-9]+)?”;

public static boolean validatePercentage(final Double percentage) {

pattern = Pattern.compile(PERCENTAGE_PATTERN);

matcher = pattern.matcher(percentage.toString());

return matcher.matches();
}

}

Advertisements

How to add years with Date object in java

add() and roll() are used to add or substract values to a Calendar object.

You specify which Calendar field is to be affected by the operation (Calendar.YEAR, Calendar.MONTH, Calendar.DATE).

add() adds or substracts values to the specified Calendar field, the next larger field is modified when the result makes the Calendar “rolls over”.

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Test {
  public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c1 = Calendar.getInstance();
    c1.set(1999, 0 , 20); // 1999 jan 20
    System.out.println("Date is : " + sdf.format(c1.getTime()));
    c1.add(Calendar.DATE,20);   // or  Calendar.DAY_OF_MONTH which is a synonym
    System.out.println("Date + 20 days is : " + sdf.format(c1.getTime()));
    /*
     * output :
     * Date is : 1999-01-20
     * Date + 20 days is : 1999-02-09
     */
 }
}

To substract, simply use a negative argument.

roll() does the same thing except you specify if you want to roll up (add 1) or roll down (substract 1) to the specified Calendar field. The operation only affects the specified field while add() adjusts other Calendar fields.

See the following example, roll() makes january rolls to december in the same year while add() substract the YEAR field for the correct result.

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Test {
  public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c1 = Calendar.getInstance();
    c1.set(1999, 0 , 20); // 1999 jan 20
    System.out.println("Date is : " + sdf.format(c1.getTime()));
    c1.roll(Calendar.MONTH, false); // roll down, substract 1 month
    System.out.println ("Date roll down 1 month : "
       + sdf.format(c1.getTime()));

    c1.set(1999, 0 , 20); // 1999 jan 20
    System.out.println("Date is : " + sdf.format(c1.getTime()));
    c1.add(Calendar.MONTH, -1); // substract 1 month
    System.out.println
      ("Date minus 1 month : "
          + sdf.format(c1.getTime()));
    /*
     * output :
     * Date is : 1999-01-20
     * Date roll down 1 month : 1999-12-20
     * Date is : 1999-01-20
     * Date minus 1 month : 1998-12-20
     */
 }
}

Email address validation in java

Here’s a Java example to show you how to use regex to validate email address.

EmailValidator.java
package com.anp.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

	private Pattern pattern;
	private Matcher matcher;

	private static final String EMAIL_PATTERN =
		"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
		+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


	/**
	 * Validate hex with regular expression
	 *
	 * @param hex
	 *            hex for validation
	 * @return true valid hex, false invalid hex
	 */
	public static boolean validate(final String hex) {
                pattern = Pattern.compile(EMAIL_PATTERN);
		matcher = pattern.matcher(hex);
		return matcher.matches();

	}
}