Showing posts with label udf. Show all posts
Showing posts with label udf. Show all posts

Wednesday, 22 June 2011

String utility : unquoting the string

This is another simple Java string utility method, which can be used to unquote a string value. This method takes care of single and double quotes both along with handling the null string. It returns the string as it is when the string is not quoted.

public static String unquote(String s) {
if (s != null && ((s.startsWith("\"") && s.endsWith("\""))
|| (s.startsWith("'") && s.endsWith("'")))) {

s = s.substring(1, s.length() - 1);
}
return s;
}

Friday, 11 March 2011

Converting Date to string

Consider we create date of today:

        // Get the date today using Calendar object.
        Date today = Calendar.getInstance().getTime();    
        String dateString = date2String(today);

package org.kodejava.example.java.util;

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

public static String date2String(Date today){
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);
}

Wednesday, 2 February 2011

Finding the logarithm of any base in Java

Unfortunately, the only methods in Java that compute the logarithm of a number are java.lang.Math.log and java.lang.Math.log10. The former returns the natural logarithm (base e) of the number and the latter returns the base 10 logarithm of the number.

To calculate the logarithm of any base in Java, we thus have to use the following method:


  1. public double logOfBase(int base, int num) {  
  2.     return Math.log(num) / Math.log(base);  
  3. }  

Why does it work?


Let's say we want to find the base 2 logarithm of 32 (method call would be logOfBase(2, 32)), which is 5.


Now if we divide the base e logarithm (ln) of our number (32) by the base e logarithm of our base (2), we get the answer we wanted:

Wednesday, 22 December 2010

Getting java run time memory statistics

   public static void logJVMStatistics()
    {
        System.out.println("JVM Statistics -- Max : " + Runtime.getRuntime().maxMemory() + "; " +
                            "Free: " + Runtime.getRuntime().freeMemory() + "; " +
                            "Total: " + Runtime.getRuntime().totalMemory());
    }

Sunday, 28 November 2010

How do I convert String to Date object?

The following code shows how we can convert a string representation of date into java.util.Date object.
To convert a string of date we can use the help from java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

public Date StringToDate
{
         DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

       try
        {
            Date today = df.parse("20/12/2005");           
           System.out.println("Today = " + df.format(today));
      } catch (ParseException e)
      {
            System.out.println("Cannot convert to date "+e);
        }
    
}
And here is the result of our code:
Today = 20/12/2005
The example starts by creating an instance of SimpleDateFormat with "dd/MM/yyyy" format which mean that the date string is formatted in day-month-year sequence.
Finally using the parse(String source) method we can get the Date instance. Because parse method can throw java.text.ParseException exception if the supplied date is not in a valid format; we need to catch it.
Here are the list of defined patterns that can be used to format the date taken from the Java class documentation.
Letter Date / Time Component Examples
G Era designator AD
y Year 1996; 96
M Month in year July; Jul; 07
w Week in year 27
W Week in month 2
D Day in year 189
d Day in month 10
F Day of week in month 2
E Day in week Tuesday; Tue
a Am/pm marker PM
H Hour in day (0-23) 0
k Hour in day (1-24) 24
K Hour in am/pm (0-11) 0
h Hour in am/pm (1-12) 12
m Minute in hour 30
s Second in minute 55
S Millisecond 978
z Time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone -0800

Sunday, 1 August 2010

Input functions in java (UDFs)

Inputing strings
public int inputInt() throws IOException
{
    int k=0;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    str = br.readLine();
    k = Integer.parseInt(str);
    return k;
           
}
For inputing integers
public int inputInt() throws IOException
{
    int k=0;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    str = br.readLine();
    k = Integer.parseInt(str);
    return k;
           
}