Showing posts with label datetostring. Show all posts
Showing posts with label datetostring. Show all posts

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); }

}
}