English
Follow Us Online
Twitter    YouTube(TM)
Break out of for loop
Convert String to int
Convert int to String
String to Date object
Convert Double to int
Convert String to int
Convert int to String
String to Date object
Convert Double to int
Convert double to int
Call a method
Call method in Class
Call a Class
Call Super Class method
Check if a file exists
Check String is Number
Declare a variable
Declare a String
Declare an Array
Declare constants
Define enum
Define a method
Exit program
Exit a for loop
Exit a method
Format date
Format double
Find current directory
Get current date
Get user input
Get Class name
Java How to A B C D E F G H I J K L M N O P Q R S T U V W Y W Z
More How to C1 C2
Java How to convert String to int
Related Links Convert String to int Convert int to String Convert String to Date object Next Page >>>
Steps:
  1. Call the parseInt method on Integer class passing the String to be converted.
/**
 * Example showing how to convert String to int
 * 
 * @author www.f5java.com
 * @return : void
 */
public void convertStringToInt() {
	String age = "25";
	// 1. Parse String as int
	int personAge = Integer.parseInt(age);
	System.out.println("Person's age is " + personAge);
}
Java How to convert int to String
Java How to cast int to String
Notes: You cannot cast int to String instead see how to convert int value to String
Steps:
  1. Create a new String object passing a new Integer object with age as parameter and call toString() method as required by the String constructor.
/**
 * Example showing how to convert int to String
 * 
 * @author www.f5java.com
 * @return : void
 */
public void convertIntToString() {
	int age = 25;
	// 1. Convert from int to String
	String personAge = new String(new Integer(age).toString());
	System.out.println("Person's age is " + personAge);
}
Java How to convert String to Date object
Steps:
  1. Create an instance of SimpleDateFormat with date format as parameter.
  2. Call parse method on DateFormat passing String date.
  3. Call format method to format the date.
/**
 * Example method converts the String date into Date Object
 * 
 * @author www.f5java.com
 * @return : void
 */
public void convertDateToString() {
	String bday = "01/01/1980";
	// 1. Create an instance of SimpleDateFormat passing the
	// date format String i.e. dd/MM/yyyy in our case as parameter 
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
	Date birthDay = null;
	try {
		// 2. Parse the String as date using DateFormat
		birthDay = df.parse(bday);
	} catch (ParseException e) { 
		// Cannot parse String into Date, hence print the exception message
		System.out.println("String cannot be parsed into date" + e.getMessage());
	}
	// Format date as dd/MM/yyyy
	System.out.println("Birth day is " + df.format(birthDay));
}