Showing posts with label english. Show all posts
Showing posts with label english. Show all posts

Tuesday, May 20, 2008

Video : GData Java Client API Demo

Here is the video showing a demo on using CellDemo class from Google Data (GData) Java client API bundled.


With this video I intended to express 3 things :
  1. How we execute the sample in Eclipse environment
  2. The running class will create a HTTPS channel
  3. Execution result is immediately visible to us
You can watch the video here.

Saturday, May 17, 2008

Eclipse com.sun.mirror.apt.* Problem

When I'm working with Google GData Java client API in Eclipse environment, I found a numerous problems and errors. The problems were found in package com.google.gdata.data.apt which uses a number of classes from com.sun.mirror.apt package, actually a standard package of recent JDK bundled. And Eclipse recognize the package and all the classes as undefined ones.

Well, I have already followed the GData "Get Started" instructions and added all libraries needed. After googling for almost an hour - which is kinda waste of time.- finally I found out an answer. And it had to do with my understanding of Eclipse inclusion of JRE and JDK library.. surprising enough given that it is too basic !

I always assume that "Java library" in Eclipse workspace is a JDK one if we have it installed. But I was wrong, since Eclipse only include JRE library - not the JDK part.

So, what I'm going to do is to add tools.jar - a jar that has com.sun.mirror.apt.* classes - from JDK distribution into my Eclipse project.

To add it as external jar, here are the steps :
  • Navigate to Project=>Properties menu
  • Click on Java Build Path in left side navigation
  • Click at Libraries tag
  • Click on Add External JARs
  • Navigate into your JDK library folder, for example C:\Program Files\Java\jdk1.5.0_15\lib
  • Choose tools.jar and click Open button
  • Click OK
  • Done
Figure 1 : JRE jars and the added JDK's tools.jar


So for those who also develop Java application using GData Java API with Eclipse, I write this article in a hope that it can be a time savior for you facing the same problem.

Thank You for Visiting,

Friday, March 7, 2008

Using Regular Expression in Java

Regular expression (regex) is a very powerful construct to manipulate text. Originated its popularity from PERL language, it is now supported by almost every popular programming language including Java.

So, how do we use it in Java ?

It's easy, just take a look at following tips.

Matching a text pattern within a String object

To match a text pattern, we can use matches() method from String object. Its syntax definition as follow :

boolean java.lang.String.matches(String regex)

So, we see that matches() method take a pattern - which is a String object - and returns boolean value of a matching condition.

Example 1:

if(textToBeMatched.matches("[a-z A-Z]+"))
System.out.println("Text match with a series of alphabet and space character only pattern");


Example 2:

public class RegexTest {

public static void testPhoneNumber(String phoneNumber)
{
String phoneNumberPattern = "^\\+{0,1}[\\d]+[-\\d]+\\d$";

if(phoneNumber.matches(phoneNumberPattern))
System.out.println(phoneNumber + " is a correct phone number !");
else
System.out.println(phoneNumber + " is a wrong phone number !");
}
public static void main(String[] args)
{
testPhoneNumber("+6221-3011-9353"); //outputs a correct phone number
testPhoneNumber("-6221-3011-9353"); //outputs a wrong phone number
}
}


Replacing text that match a pattern

To replace a text matching a pattern we need to use two java.util classes, i.e : Pattern and Matcher.

First, we initialize the pattern with compile() static method of Pattern class. With which also create a Pattern object. We then feed a source text to the matcher() method which then create a Matcher object. The last thing for us to do is to manipulate the text within the object, so we replace text with Matcher's replaceAll() method.



Example:

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

public class RegexReplacementTest {

public static String censoredPhoneNumber(String phoneNumber)
{
String phonePattern = "(\\+{0,1}[\\d]+[-\\d]+\\d).*";

Pattern pattern = Pattern.compile(phonePattern);
Matcher matcher = pattern.matcher(phoneNumber);

return matcher.replaceAll("*censored*");
}

public static void main(String[] args)
{
String phoneNo = "My phone number is +6221-3011-9353";

System.out.println(censoredPhoneNumber(phoneNo));

}
}

Conclusion

This blog's article show how we use regular expression in two ways :
  1. to match a text pattern using String's matches() method.
  2. to replace string which match a text pattern with two helper classes, Pattern and Matcher.

Hope this article can help you to resolve text manipulation problem that you may have encountered.

Any comments to improve this article is greatly welcomed. Post your comment here or mail to feris@phi-integration.com.


Monday, March 3, 2008

Working with Date and Time in Java

I recalled when I started to learn Java, one of the burden I'm facing with is how to deal with date and time properly.

So to help anyone with the same problem, in this article I list down a number of "shortcut tips" on dealing with them.

1. Returning timestamp
Date now = new Date();

long nowLong = now.getTime(); //returning a timestamp
System.out.println("Value is " + nowLong);

2. Formatting date
SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
Date now = new Date();
long nowLong = now.getTime(); //returning a timestamp

System.out.println(df.format(now));

3. Assigning date value with a formatted text literal
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

try {
Date date = (Date) sdf.parse("12/31/2007");
System.out.println("Current date is : " + sdf.format(date));
} catch (ParseException e) {
System.out.print("Illegal date !");
}

4. Comparing date with compareTo() method of Date object
//With compareTo() method we should have a return of value -1, 0, and 1.
//Syntax : date1.compareTo(date2);
//Return value
//   -1 : date1 is earlier than date2
//    0 : date1 is having exactly same value with date2
//    1 : date2 is later than date2

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

try {
Date date1 = (Date) sdf.parse("12/31/2007");
Date date2 = (Date) sdf.parse("12/31/2008");
Date date3 = (Date) sdf.parse("12/31/2008");

System.out.println(date1.compareTo(date2));
System.out.println(date2.compareTo(date1));
System.out.println(date2.compareTo(date3));
} catch (ParseException e) {
System.out.print("Illegal date !");
}

5. Assigning date with GregorianCalendar class
//Get current date and time
Calendar cal = new GregorianCalendar();
System.out.println(cal);

//Set date with int value
Calendar newdate = new GregorianCalendar(2008, Calendar.MARCH, 1);
System.out.println(newdate);

//Convert into Date object
Date date = newdate.getTime();     
System.out.println(date);

//Print with a MM/dd/yyyy format
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("New date is : " + sdf.format(date));

Online Resources:
1. http://www.javaworld.com/jw-12-2000/jw-1229-dates.html
2. http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html
3. http://forum.java.sun.com/thread.jspa?threadID=552315&messageID=2701323
4. http://forum.java.sun.com/thread.jspa?threadID=539302&range=1&start=2&forumID=31
5. http://www.exampledepot.com/