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

JAutodocs for auto comments

Hi today I had seen one useful eclipse plugin for generating comments into your source code automatically, today I am writing an introduction about the same. Lets try to utilize this plugin...

JAutodoc is a very useful eclipse plugin which helps in generating javadoc style comments very easily. Apart from adding the template for javadoc style comments for the class/method & attributes it is smart enough to add the description also based on the signatures. This can cover a substantial part of description creation effort for the methods like the setters/getters etc which do not need much human skills to generate the descriptions.

For more information on how to install and use please visit the official site for JAutoDocs at http://jautodoc.sourceforge.net/

Hope you will find it useful.

How to convert Byte[] array to String in Java

In some cases, we have to convert String variable into a Byte array format, for example, JCE encryption. However how do we convert a Byte[] array to String afterward?

Simple toString() function like following code is not working property. It will not display the original text but byte value.

String s = bytes.toString();

In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.

public class TestByte
{
public static void main(String[] argv) {

String example = "This is an example";
byte[] bytes = example.getBytes();

System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());

String s = new String(bytes);
System.out.println("Text Decryted : " + s);
}
}

Simple, basic but much needed things in java

It is often needed to convert one type of objects into another type in java. Below I have mentioned about these common types as they form basic foundation in java.

* How to convert String to boolean?

String listing = "true";
boolean theValue = Boolean.parseBoolean(listing);

* How to convert boolean to string?

public class BooleanToString {
public static void main(String[] args){
boolean b = true;
String s = new Boolean(b).toString();
System.out.println("String is:"+ s);
}
}

* How to converting string to date?

public class StringToDate {
public static void main(String[] args) {
try {
String str_date="11-June-07";
SimpleDateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date );
}
catch (ParseException e)
{
System.out.println("Exception :"+e);
}
}
}

* How to convert date to string?

import java.util.*;
import java.text.*;
public class DateToString {
public static void main(String[] args) {
try {
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse("11-June-07");
String s = formatter.format(date);
System.out.println("Today is " + s);
} catch (ParseException e)
{System.out.println("Exception :"+e); }

}
}