---
title: "Java Conversion"  
description: "If you want to convert string to float,double,int,date and vice- versa then this article are very useful for that"  
author: "Anupam Mishra"  
published: 2015-12-10  
updated: 2019-11-27  
canonical: https://www.mindstick.com/articles/11943/java-conversion  
category: "java"  
tags: ["java", "j2ee"]  
reading_time: 2 minutes  

---

# Java Conversion

##### 1. String to Integer:

In java, if we are [converting string](https://www.mindstick.com/forum/23195/converting-string-to-int-in-java) to [integer](https://www.mindstick.com/forum/160726/roman-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](https://www.mindstick.com/interview/34427/explain-the-output-in-angular):**

50100

150

##### 2. Integer to String:

In java, if we are converting Integer to String then using, Integer. [toString](https://www.mindstick.com/interview/2259/what-is-the-difference-between-tostring-convert-tostring)(int) or String.valueOf (int) [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best).

```
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](https://www.mindstick.com/interview/34357/what-is-the-difference-between-decimal-and-float):**

In java, if we are converting String to Float then using, Float.parseFloat (String s) [method](https://www.mindstick.com/forum/166/webservice-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](https://www.mindstick.com/forum/159139/convert-string-to-date-in-oracle-sql):**

We can [convert](https://www.mindstick.com/blog/698/convert-text-document-to-pdf-file) 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

---

Original Source: https://www.mindstick.com/articles/11943/java-conversion

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
