Java - Removing all characters but letters and numbers

/**
 * Removing all characters but letters and numbers.
 * @author Xuan Ngo
 *
 */
public class StrippingUnwantedChars
{
 
  public static void main(String[] args)
  {
    String s = "Good Morning, today is 1900-12-31.";
 
    /**
     * Replace characters that are NOT lowercase letters(a-z), uppercase 
     *  letters(A-Z) or numbers(0-9) with empty string.
     */
    s = s.replaceAll("[^a-zA-Z0-9]", "");
 
    System.out.println(s); // Output: GoodMorningtodayis19001231
  }
 
}

Java - Create 1 log file per day

Add the followings in your Log4j configuration/properties file:

log4j.rootLogger=info, R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=filename
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS zzz} %5p %c{1}:%L - %m%n

The filename of the log file will look like this:

filename
filename.2010-01-21.log
filename.2010-01-22.log
filename.2010-01-23.log

Java - Measure time elapsed

/**
 * Measure time elapsed.
 * @author Xuan Ngo
 *
 */
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
 
public class Chronometer 
{
  private Calendar m_oStart = null;
  private Calendar m_oEnd = null;
 
 
  public Chronometer(){}
 
  public void start()
  {
    this.m_oStart = Calendar.getInstance();
  }
  public void stop()
  {
    this.m_oEnd = Calendar.getInstance();
  }
 
  /**
   * @return Return the elapsed time measured in milliseconds.
   */
  public final long getDiffInMillis()
  {

Java - log4j

http://ideoplex.com/id/386/controlling-hibernate-output-with-log4j

http://www.laliluna.de/articles/log4j-tutorial.html

http://juliusdavies.ca/logging.html

http://www.vaannila.com/log4j/log4j-configuration.html

import org.apache.log4j.Logger;  
 
public class YourClass
{  
  private static final Logger log = Logger.getLogger(YourClass.class)  
  // Some people recommend against making that line static, but Logger.getLogger() is synchronized, so non-static can degrade performance.
 
  public void someMethod() 
  {  
    log.info("Your info message");  

Eclipse - Change the default text encoding in Eclipse

In Eclipse, go to Window->Preferences->General->Workspace and select UTF-8 as the Text file encoding.

Java - Find a linear function that represents a set of data points

/**
 * Simple Linear Regression: Find a linear function that represents a set of data points.
 * @author Xuan Ngo 
 */
import java.util.ArrayList;
import static java.lang.Math.pow;
import java.lang.RuntimeException;
 
public class SimpleLinearRegression
{
  private ArrayList<Double> m_aX = new ArrayList<Double>();
  private ArrayList<Double> m_aY = new ArrayList<Double>();
 
  private ArrayList<Double> m_aXX = null;
  private ArrayList<Double> m_aXY = null;
 
  private double m_dSumOfXs = 0;
  private double m_dSumOfYs = 0;

Drupal - Food for thought: Why this query is slower than the other 1?

Queries ran in MySQL:

-- Slow query: 3.6 seconds
SELECT COUNT(*) FROM sessions WHERE uid = 0 
  AND unix_timestamp() timestamp < (60*15)
 
-- Fast query: 30 milliseconds
SELECT COUNT(*) FROM sessions WHERE uid = 0 
  AND timestamp > (unix_timestamp() (60*15))

Why the first query is slower than the last one?

Drupal - Mistakenly put drupal/ in sites/all/modules

When updating drupal and other modules at the same time, I sometimes mistakenly put drupal-6.x folder in sites/all/modules folder. Later on, I discovered my mistake and naturally, my first reflex would be to delete the drupal-6.x folder. Consequently, my website crashed and I couldn't log in. However, if I put back the drupal-6.x folder, then it is working fine.

To remove drupal-6.x folder from sites/all/modules folder, I did the followings:

Java - Convert an octal value to its corresponding ASCII value

/**
 * Convert an octal value to its corresponding ASCII value.
 * Convert from octal(base 8)->decimal(base 10)->character.
 * @author Xuan Ngo
 */
public class OctalToAscii
{
  public static void main(String[] args)
  {
    // Note: octal is base 8.
    String sOctal = "355";
 
    // Convert Octal(base8) to decimal(base 10).
    Integer iOctal = Integer.parseInt(sOctal, 8);
    System.out.println(iOctal); // Output: 237
 
    // Cast decimal to its corresponding ASCII value.
    char cOctal = (char)iOctal.intValue();

Java - Usages of regular expression in String.replaceAll()

/**
 * Examples showing the usages of regular expression in String.replaceAll().
 * @author Xuan Ngo
 */
public class Regex
{
  public static void main(String[] args) 
  {
    String s = "";
 
    /*
     * Remove substring that ends with ,???$
     * Show how to escape and use $.
     */
    s = "53 001,88 $";
    s = s.replaceAll(",...\\$$", "\\$");
    System.out.println(s); // Output: 53 001$
 
    /*
     * Replace a substring regardless of its characters' case(Case Insensitive).
     */
    s = "CaSE Insensitive";

Syndicate content