articles

Home / DeveloperSection / Articles / Java Conversion

Java Conversion

Java Conversion

Anupam Mishra 4755 10-Dec-2015
1. String to Integer:

In java, if we are converting string to integer then using, 

Integer.parseInt(String s) method.
public class StringToIntEx{
public static void main(String args[]){
String s="50";
int i=Integer.parseInt(s);
System.out.println(s+100);
System.out.println(i+100);
}}

Output: 

50100

150

2. Integer to String:  

In java, if we are converting Integer to String then using, Integer. toString(int) or String.valueOf (int) methods. 

public class IntegerToStrEx{  

public static void main(String args[]){ 
int n=100;
String s=Integer.toString(n);
String ss=String,valueOf(n);  
System.out.println(s+100);
System.out.println(ss+100);
System.out.println(n+100);
 
}} 

Output: 

50100

50100

150

3. String to Long:

 In java, if we are converting String to Long then using, Long.parseLong (String s).  

public class StringToLongEx{  

public static void main(String args[]){ 
String s="8005366665"; 
long l=Long.parseLong(s); 
System.out.println(l); 
}} 

Output: 

8005366665

4. Long to String:   

In java, if we are converting Long to String then using, Long. toString (Long) or String.valueOf(Long) methods.

public class LongToStringEx{  

public static void main(String args[]){
long l="8005366665";
String s=Long.toString(l);
String ss=String.valueOf(l);
System.out.println(s+100);
System.out.println(ss+100);
}}

Output:

8005366665

8005366665

5. String to Float:

In java, if we are converting String to Float then using, Float.parseFloat (String s) method.

public class StringToFloatEx{  

public static void main(String args[]){
String s="50.6";
float f=Float.parseFloat(s);
System.out.println(f);
}}

Output:

50.6  

6. String to Double:

In java, if we are converting String to Double then using, Double.parseDouble (String s). 

public class StringToDoubleEx{  

public static void main(String args[]){ 
String s="50";  
double d=Double.parseDouble(s);  
System.out.println(d); 
}} 

Output: 

50

7. String to Date: 

We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes. We have also known about DateFormat and SimpleDateFormat classes previously. 

import java.text.SimpleDateFormat;  

import java.util.Date;
public class StringToDateEx {
public static void main(String[] args)throws Exception {

String D="10/12/2015";
    Date D1=new SimpleDateFormat("dd/MM/yyyy").parse(D);
    System.out.println(D+"\t"+D1);
}
}
Output: 

10/12/2015 Thu Dec 10 07:32:15

IST 2015


java java  j2ee 
Updated 27-Nov-2019

Leave Comment

Comments

Liked By