All Date Type Conversions in Java with Examples

All Date Type Conversions in Java with Examples:

This article “All Date Type Conversions in Java with Examples” we are writing to help people to quickly find the data type conversions in java with examples. Few of the below conversions are many times asked in interviews also. So bookmark and read often to store it in your mind.

 

Array to ArrayList convert in Java Example:

 

Here we are just iterating the states array and adding it to the arraylist.

 

Array to ArrayList Conversions Java Program: 


package in.javadomain;java.util.ArrayList;

public class ArrayToArrayList {
public static void main(String[] args) {
ArrayList<String> statesArrayList = new ArrayList<String>();
String[] statesArray = new String[] { "Chennai", "Mumbai", "Delhi", "Bangalore" };
for (String state : statesArray) {
statesArrayList.add(state);
}
System.out.println("Array to ArrayList Done");
System.out.println("Printing from ArrayList: " + statesArrayList);
}
}

 

Output:


Array to ArrayList Done
Printing from ArrayList: [Chennai, Mumbai, Delhi, Bangalore]

 

 

ArrayList to Array convert in Java Example:

Converting ArrayList to Array using toArray method.

 

“ArrayList.toArray(Array)”. You can declare array size based arraylist size.

ArrayList to Array Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;

public class ArrayListToArray {

public static void main(String[] args) {
ArrayList<String> statesArrayList = new ArrayList<String>();
statesArrayList.add("Chennai");
statesArrayList.add("Mumbai");
statesArrayList.add("Delhi");
statesArrayList.add("Bangalore");
String[] s = new String[statesArrayList.size()];
statesArrayList.toArray(s);

System.out.println("ArrayList to Array Conversion Done");
System.out.println("Printing from Array:");
for (String arrayVar : s) {
System.out.println(arrayVar);
}

}
}

 

Output:


ArrayList to Array Conversion Done
Printing from Array:
Chennai
Mumbai
Delhi
Bangalore

 

 

Set to ArrayList Convert in Java Example:

Set can be converted to ArrayList using addAll() method.
Other way, you can just iterate Set and add the values to Arraylist.

Set to ArrayList Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class SetToArrayList {
public static void main(String[] args) {
Set<String> countrySet = new HashSet<String>();
countrySet.add("US");
countrySet.add("UK");
countrySet.add("UAE");
countrySet.add("Newyork");
countrySet.add("PARIS");
countrySet.add("PARIS");
System.out.println("Printing from Set: "+countrySet);

// Converting Set to ArrayList using addAll() collection method.
ArrayList<String> countryList = new ArrayList<String>();
countryList.addAll(countrySet);
System.out.println("Printing from ArrayList: "+countrySet);
}
}

 

Output:


Printing from Set: [UK, UAE, Newyork, PARIS, US]
Printing from ArrayList: [UK, UAE, Newyork, PARIS, US]

 

 

 

ArrayList to Set Convert in Java Example:

ArrayList can also be converted to Set using addAll() method.
Other way, you can just iterate ArrayList and add the values to Set. Remember Set will not allow the duplicates.

 

ArrayList to Set Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class ArrayListToSet {
public static void main(String[] args) {
ArrayList<String> countryList = new ArrayList<String>();
countryList.add("US");
countryList.add("UK");
countryList.add("UAE");
countryList.add("Newyork");
countryList.add("PARIS");
countryList.add("PARIS");
System.out.println("Printing from ArrayList: " + countryList);

// Converting ArrayList to Set using addAll() method.
Set<String> countrySet = new HashSet<String>();
countrySet.addAll(countryList);
System.out.println("Printing from Set: " + countrySet);
}
}

 

Output:


Printing from ArrayList: [US, UK, UAE, Newyork, PARIS, PARIS]
Printing from Set: [UK, UAE, Newyork, PARIS, US]

 

 

HashMap Key to Set/ArrayList Convert in Java Example:

entrySet() can be used to get both key and value.

 

Now we want only HashMap keys so we can use keySet().

HashMap Key to Set/ArrayList Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class HashMapKeyToSetAndArrayList {
public static void main(String[] args) {
HashMap<String, String> cityHashMap = new HashMap<String, String>();
cityHashMap.put("Karnataka", "Bangaloore");
cityHashMap.put("Tamilnadu", "Chennai");
cityHashMap.put("Maharastra", "Mumbai");
cityHashMap.put("New Delhi", "Delhi");
cityHashMap.put("Tamilnadu", "Chennai");

// HashMap Key to Set
Set<String> mapKeySet = new HashSet<String>();
mapKeySet.addAll(cityHashMap.keySet());
System.out.println("Printing Keys From Set : " + mapKeySet);

ArrayList<String> mapKeyList = new ArrayList<String>();
mapKeyList.addAll(cityHashMap.keySet());
System.out.println("Printing Keys From ArrayList : " + mapKeyList);
}
}

Output:

Printing Keys From Set : [New Delhi, Karnataka, Tamilnadu, Maharastra]
Printing Keys From ArrayList : [New Delhi, Karnataka, Tamilnadu, Maharastra]

Note: If you notice here, the duplicate State Tamilnadu is not exist even in the ArrayList, because hashmap keys are converted to keySet() nothing but set collection.
So duplicates are removed here itself.

 

 

 

HashMap Value to Set/ArrayList Convert in Java Example:

entrySet() can be used to get both key and value.

 

HashMap Value to Set/ArrayList Conversion Java Program:

 


package in.javadomain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapValueToSetAndArrayList {
public static void main(String[] args) {
HashMap<String, String> cityHashMap = new HashMap<String, String>();
cityHashMap.put("Karnataka", "Bangaloore");
cityHashMap.put("Tamilnadu", "Chennai");
cityHashMap.put("Maharastra", "Mumbai");
cityHashMap.put("New Delhi", "Delhi");
cityHashMap.put("Tamilnadu", "Chennai");

// HashMap Value to Set
Set<String> mapKeySet = new HashSet<String>();
for (Entry<String, String> stateCityMap : cityHashMap.entrySet()) {
mapKeySet.add(stateCityMap.getValue());
}
System.out.println("Printing Values From Set : " + mapKeySet);

// HashMap Value to ArrayList
ArrayList<String> mapKeyList = new ArrayList<String>();
for (Entry<String, String> stateCityMap : cityHashMap.entrySet()) {
mapKeyList.add(stateCityMap.getValue());
}
System.out.println("Printing Values From ArrayList : " + mapKeyList);
}
}

 

Output:


Printing Values From Set : [Delhi, Chennai, Bangaloore, Mumbai]
Printing Values From ArrayList : [Delhi, Bangaloore, Chennai, Mumbai]

 

ArrayList to Vector Convert in Java Example:

ArrayList to Vector can be done in 3 ways,

1. using addAll() method
2. directly assigning the value.
Vector<String> mobilesVector = new Vector<String>(mobilesList);
here mobilesList is an arraylist.
3. Iterating and adding the value to Vector.

 

ArrayList to Vector Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;
import java.util.Vector;

public class ArrayListToVector {

public static void main(String[] args) {

ArrayList<String> mobilesList = new ArrayList<String>();
mobilesList.add("Nokia");
mobilesList.add("LG");
mobilesList.add("Samsung");
mobilesList.add("HTC");

System.out.println("Printing from ArrayList : " + mobilesList);
// converting arraylist to vector using addAll() method
Vector<String> mobilesVector = new Vector<String>();
// can be done this way also,
// Vector<String> mobilesVector = new Vector<String>(mobilesList);
mobilesVector.addAll(mobilesList);

System.out.println("Printing from Vector : " + mobilesVector);
}

}

Output:


Printing from ArrayList : [Nokia, LG, Samsung, HTC]
Printing from Vector : [Nokia, LG, Samsung, HTC]

 

 

Vector to ArrayList Convert in Java Example:

Vector to ArrayList also can be done in 3 ways,

1. using addAll() method
2. directly assigning the value.
ArrayList<String> mobilesList = new ArrayList<String>(mobilesVector);
here mobilesVector is a vector.
3. Iterating and adding the value to ArrayList.

 

Vector to ArrayList Conversion Java Program:


package in.javadomain;

import java.util.ArrayList;
import java.util.Vector;

public class VectorToArrayList {

public static void main(String[] args) {

Vector<String> mobilesVector = new Vector<String>();
mobilesVector.add("Nokia");
mobilesVector.add("LG");
mobilesVector.add("Samsung");
mobilesVector.add("HTC");

System.out.println("Printing from Vector : " + mobilesVector);
// converting Vector to arraylist using addAll() method
ArrayList<String> mobilesList = new ArrayList<String>();

mobilesList.addAll(mobilesVector);

System.out.println("Printing from ArrayList : " + mobilesList);
}

}

Output:


Printing from Vector : [Nokia, LG, Samsung, HTC]
Printing from ArrayList : [Nokia, LG, Samsung, HTC]

 

 

 

Vector to Set Convert in Java Example:

Vector to Set also can be done in 3 ways,

1. using addAll() method
2. directly assigning the value.
Set<String> rvrsSet = new HashSet<String>(rvrsVector);
here rvrsVector is Vector.
3. Iterating and adding the value to Set.

 

Vector to Set Conversion Java Program:


package in.javadomain;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

public class VectorToSet {
public static void main(String[] args) {
Vector<String> rvrsVector = new Vector<String>();
rvrsVector.add("Krishna");
rvrsVector.add("Godavari");
rvrsVector.add("Yamuna");
rvrsVector.add("Ganga");
rvrsVector.add("Kaveri");
System.out.println("Printing Vector : " + rvrsVector);
Set<String> rvrsSet = new HashSet<String>();
rvrsSet.addAll(rvrsVector);
System.out.println("Printing Set : " + rvrsSet);
}
}

 

Output:

Printing Vector : [Krishna, Godavari, Yamuna, Ganga, Kaveri]
Printing Set : [Krishna, Kaveri, Ganga, Yamuna, Godavari]

 

 

 

Set to Vector Convert in Java Example:

Set Vector to also can be done in 3 ways,

1. using addAll() method
2. directly assigning the value.
Vector<String> countryVector = new Vector<String>(countrySet);
here countrySet is Set.
3. Iterating and adding the value to Set.

 

Set to Vector Conversion Java Program:


package in.javadomain;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

public class SetToVector {
public static void main(String[] args) {
Set<String> countrySet = new HashSet<String>();
countrySet.add("US");
countrySet.add("UK");
countrySet.add("UAE");
countrySet.add("Newyork");
countrySet.add("PARIS");
countrySet.add("PARIS");
System.out.println("Printing from Set: " + countrySet);

// Converting Set to ArrayList using addAll() collection method.
Vector<String> countryVector = new Vector<String>();
countryVector.addAll(countrySet);
System.out.println("Printing from Vector : " + countryVector);
}
}

 

Output:


Printing from Set: [UK, UAE, Newyork, PARIS, US]
Printing from Vector : [UK, UAE, Newyork, PARIS, US]

 

 

Integer to String Convert in Java Example:

 

Since integer is a wrapper class, you can use toString() directly to convert integer to string.

 

Integer to String Conversion Java Program:


package in.javadomain;

public class IntegerToString {

public static void main(String[] args) {
Integer a = 10;
String str = a.toString();

System.out.println("Integer : "+a);
System.out.println("String[Integer to String] : "+str);
}
}

 

Output:


Integer : 10
String[Integer to String] : 10

 

 

 

int to String Convert in Java Example:

 

int is a primitive data type, so you can not convert it to String using toString() directly.

So we must use its wrapper class[Integer] to convert int to string.

 

int to String Conversion Java Program:


package in.javadomain;

public class IntToString {

public static void main(String[] args) {
int a = 10;
String str = Integer.toString(a);

System.out.println("int : "+a);
System.out.println("String[int to String] : "+str);
}
}

 

Output:


int : 10
String[int to String] : 10

 

 

Float to String Convert in Java Example:

 

Float is a Wrapper class, so toString() can be used to convert Float to String.

 

 

Float to String Conversion Java Program:


package in.javadomain;

public class FloatToString {

public static void main(String[] args) {
Float a = 6.18f;
String str = a.toString();

System.out.println("Float : "+a);
System.out.println("String[Float to String] : "+str);
}
}

 

Output:


Float : 6.18
String[Float to String] : 6.18

 

 

float to String Convert in Java Example:

 

float is a primitive data type, so you can not convert it to String using toString() directly.

So we must use its wrapper class[Float] to convert float to string or String.valueOf(f).

 

float to String Conversion Java Program:


package in.javadomain;

public class floatToString {

public static void main(String[] args) {
float a = 6.18f;
String str = Float.toString(a);

System.out.println("float : "+a);
System.out.println("String[float to String] : "+str);
}
}

Output:


float : 6.18
String[float to String] : 6.18

 

 

 

Double to String Convert in Java Example:

 

Double is a Wrapper class, so toString() can be used to convert Double to String.

 

Double to String Conversion Java Program:


package in.javadomain;

public class DoubleToString {

public static void main(String[] args) {
Double d = 11.0873;
String str = d.toString();

System.out.println("Double : "+d);
System.out.println("String[Double to String] : "+str);
}
}

Output:


Double : 11.0873
String[Double to String] : 11.0873

 

 

double to String Convert in Java Example:

 

double is a primitive data type, so you can not convert it to String using toString() directly.

 

So we must use its wrapper class[Double] to convert double to string or String.valueOf(d).

 

double to String Conversion Java Program:


package in.javadomain;

public class doubleToString {

public static void main(String[] args) {
double d = 11.0873;
String str = Double.toString(d);

System.out.println("double : "+d);
System.out.println("String[double to String] : "+str);
}
}

 
Output:


double : 11.0873
String[double to String] : 11.0873

 

char to String Convert in Java Example:

 

char can be converted to string using String valueOf() method (or) Character toString() method.

 

char to String Conversion Java Program:


package in.javadomain;

public class charToString {

public static void main(String[] args) {
char c = 'c';
String str = String.valueOf(c);

// or
String str1 = Character.toString(c);

System.out.println("char : "+c);
System.out.println("String[char to String using valueOf] : "+str);
System.out.println("String[char to String using Character] : "+str1);
}
}

 

Output:


char : c
String[char to String using valueOf] : c
String[char to String using Character] : c

 

 

Date to String Convert in Java Example:

 

Date can be converted to string using toString() method.

 

Date to String Conversion Java Program:


package in.javadomain;

import java.util.Date;

public class DateToString {

public static void main(String[] args) {
Date d = new Date();
System.out.println("Date : " + d);
String s = d.toString();
System.out.println("String[Date to String] : " + s);
}

}

 

Output:

Date : Thu Apr 21 23:09:38 IST 2016
String[Date to String] : Thu Apr 21 23:09:38 IST 2016

 

 

Integer to Long Convert in Java Example:

 

Integer can be converted to Long longValue() method. Remember longValue() method exist only for wrapper class. So you can not use the same to convert from int to long.

 

Integer to Long Conversion Java Program:


package in.javadomain;

public class IntegerToLong {

public static void main(String[] args) {
Integer i = 10;
Long l = i.longValue();
System.out.println("Integer : " + i);
System.out.println("Long[Integer to Long] : " + l);
}

}

 

Output:


Integer : 10
Long[Integer to Long] : 10

 

 

int to long Convert in Java Example:

 

int values can be assigned directly to long variables without any external conversions. Because long variables can store the values which int is holding. So no need of
any external conversions

 

int to long Convert Java Program:


package in.javadomain;

public class intTolong {

public static void main(String[] args) {
int i = 10;
long l = i;
System.out.println("int : " + i);
System.out.println("long[int to long] : " + l);
}

}

Output:


int : 10
long[int to long] : 10

 

float to double Convert in Java Example:

 

float values can be assigned directly to double variables without any external conversions. Because double variables can hold/store the values which float is holding.
So no need of any external conversions.

 

float to double Convert Java Program:


package in.javadomain;

public class floatTodouble {

public static void main(String[] args) {
float f = 10.26f;
double d = f;
System.out.println("float : " + f);
System.out.println("double[float to double] : " + d);
}

}

 

Output:


float : 10.26
double[float to double] : 10.260000228881836

 

 

Float to Double Convert in Java Example:

 

Float wrapper class values can be converted Double wrapper class values using doublevalue() method.

Float to Double Conversion in Java Example:

package in.javadomain;

public class FloatToDouble {

public static void main(String[] args) {
Float f = 10.26f;
Double d = f.doubleValue();
System.out.println("Float : " + f);
System.out.println("Double[Float to Double] : " + d);
}

}

 

Output:


Float : 10.26
Double[Float to Double] : 10.260000228881836

 

 

Integer/int to BigDecimal Convert in Java Example:

 

BigDecimal takes int/long/double/string and converts directly as a big decimal number.

 

Integer/int to BigDecimal Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class IntegerToBigDecimal {

public static void main(String[] args) {
Integer i = 666;
// also works if
// int i = 666;
BigDecimal bd = new BigDecimal(String.valueOf(i));
System.out.println("int/Integer : " + i);
System.out.println("BigDecimal[int/Integer to BigDecimal] : " + bd);
}

}

 

Output:


int/Integer : 666
BigDecimal[int/Integer to BigDecimal] : 666

 

 

Long/long to BigDecimal Convert in Java Example:

 

BigDecimal takes int/long/double/string and converts directly as a big decimal number.

 

Long/long to BigDecimal Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class LongToBigDecimal {

public static void main(String[] args) {
long l = 1126L;
// (or) Long l = 1126L;
BigDecimal bd = new BigDecimal(String.valueOf(l));
System.out.println("long/Long : " + l);
System.out.println("BigDecimal[long/Long to BigDecimal] : " + bd);
}

}

 

Output:


long/Long : 1126
BigDecimal[long/Long to BigDecimal] : 1126

 

 

BigDecimal to int Convert in Java Example:

 

BigDecimal can be converted to int using Integer wrapper class valueOf() method.

 

valueOf() method takes only either int/string. so intValue() method used to pass the value to valueOf() method.

 

BigDecimal to int Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class BigDecimalToInt {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal(10);
// Converting bigdecimal to Int
int i =Integer.valueOf(bd.intValue());
System.out.println("BigDecimal : "+bd);
System.out.println("int[BigDecimal to int] : "+i);

}
}

 

Output:


BigDecimal : 10
int[BigDecimal to int] : 10

 

 

BigDecimal to Integer Convert in Java Example:

 

BigDecimal can be converted to Integer using Integer wrapper class valueOf() method.

 

valueOf() method takes only either int/string.

 

so intValue() method used to pass the required int value to valueOf() method.

 

BigDecimal to Integer Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class BigDecimalToInteger {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal(10);
Integer i =Integer.valueOf(bd.intValue());
System.out.println("BigDecimal : "+bd);
System.out.println("Integer[BigDecimal to Integer] : "+i);

}
}

 

Output:


BigDecimal : 10
Integer[BigDecimal to Integer] : 10

 

 

BigDecimal to Long/long Convert in Java Example:

 

BigDecimal can be converted to Long/long using Long wrapper class valueOf() method.

 

valueOf() method takes long value. so longValue() method used to pass the required long value to valueOf() method.

BigDecimal to Long/long Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class BigDecimalToLongAndlong {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal(10);
long l = Long.valueOf(bd.longValue());
Long l1 = Long.valueOf(bd.longValue());
System.out.println("BigDecimal : " + bd);
System.out.println("Long[BigDecimal to Long] : " + l);
System.out.println("long[BigDecimal to long] : " + l1);

}
}

 

Output:


BigDecimal : 10
Long[BigDecimal to Long] : 10
long[BigDecimal to long] : 10

 

BigDecimal to Float/float Convert in Java Example:

 

BigDecimal can be converted to Float/float using Float wrapper class valueOf() method.

 

valueOf() method takes float value. so floatValue() method used to pass the required float value to valueOf() method.

BigDecimal to Float/float Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class BigDecimalToFloatAndfloat {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal(10);
float f = Float.valueOf(bd.floatValue());
Float f1 = Float.valueOf(bd.floatValue());
System.out.println("BigDecimal : " + bd);
System.out.println("float[BigDecimal to float] : " + f);
System.out.println("Float[BigDecimal to Float] : " + f1);

}
}

 

Output:

BigDecimal : 10
float[BigDecimal to float] : 10.0
Float[BigDecimal to Float] : 10.0

 

 

BigDecimal to Double/double Convert in Java Example:

 

BigDecimal can be converted to Double/double using Double wrapper class valueOf() method.

 

valueOf() method takes double value. so doubleValue() method used to pass the required double value to valueOf() method.

BigDecimal to Double/double Conversion Java Program:

package in.javadomain;

import java.math.BigDecimal;

public class BigDecimalToDoubleAnddouble {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal(10);
double d = Double.valueOf(bd.doubleValue());
Double d1 = Double.valueOf(bd.doubleValue());
System.out.println("BigDecimal : " + bd);
System.out.println("double[BigDecimal to double] : " + d);
System.out.println("Double[BigDecimal to Double] : " + d1);
}
}

Output:


BigDecimal : 10
double[BigDecimal to double] : 10.0
Double[BigDecimal to Double] : 10.0

 

 

Long/long to Double/double Convert in Java Example:

 

Long can be converted to double/Double using doubleValue() method.

 

But long should be converted to Long wrapper class before converting to double/Double. Then we can use the same doubleValue() method to convert long to double/Double.

 

Long/long to Double/double Conversion Java Program:


package in.javadomain;

public class LongToDouble {
public static void main(String[] args) {
Long l = new Long(123456789011L);
long l1 = 123456789011L;

// Converting Long/long to double
double d = l.doubleValue();
double d1 = Long.valueOf(l1).doubleValue();

// Converting Long/long to Double
Double d2 = l.doubleValue();
Double d3 = Long.valueOf(l1).doubleValue();

System.out.println("Long : " + l);
System.out.println("long : " + l1);
System.out.println("double[Long to double] : " + d);
System.out.println("double[long to double] : " + d1);
System.out.println("Double[Long to double] : " + d2);
System.out.println("Double[long to double] : " + d3);
}
}

 
Output:


Long : 123456789011
long : 123456789011
double[Long to double] : 1.23456789011E11
double[long to double] : 1.23456789011E11
Double[Long to double] : 1.23456789011E11
Double[long to double] : 1.23456789011E11

 

 

Integer/int to Double/double Convert in Java Example:

 

Integer can be converted to double/Double using doubleValue() method.

 

B

ut int should be converted to Integer wrapper class before converting to double/Double, because doubleValue() exist only in Integer wrapper class. Then we can use the
same doubleValue() method to convert int to double/Double.

 

Integer/int to Double/double Conversion Java Program:


package in.javadomain;

public class IntToDouble {
public static void main(String[] args) {
Integer i = 18;
int i1 = 18;

double d = i.intValue();
double d1 = Integer.valueOf(i1).intValue();

Double d2 = i.doubleValue();
Double d3 = Long.valueOf(i1).doubleValue();

System.out.println("Integer : " + i);
System.out.println("int : " + i1);
System.out.println("double[Integer to double] : " + d);
System.out.println("double[int to double] : " + d1);
System.out.println("Double[Integer to double] : " + d2);
System.out.println("Double[int to double] : " + d3);
}
}

Output:


Integer : 18
int : 18
double[Integer to double] : 18.0
double[int to double] : 18.0
Double[Integer to double] : 18.0
Double[int to double] : 18.0

 

Double/double to Long/long Convert in Java Example:

 

Double can be converted to Long/long using longValue() method.

 

But double primitive data type should be converted to Double wrapper class before converting to long/Long, because longValue() exist only in Long wrapper class. Then
we can use the same longValue() method to convert double to Long/long.

 

Double/double to Long/long Conversion Java Program:


package in.javadomain;

public class DoubleToLong {
public static void main(String[] args) {
double d = 18.11;
Double d1 = 18.781;

long l = Double.valueOf(d).longValue();
long l1 = d1.longValue();

Long l2 = Double.valueOf(d).longValue();
Long l3 = d1.longValue();

System.out.println("double : " + d);
System.out.println("Double : " + d1);
System.out.println("long[double to long] : " + l);
System.out.println("long[Double to long] : " + l1);
System.out.println("Long[double to Long] : " + l2);
System.out.println("Long[Double to Long] : " + l3);
}
}

Output:

double : 18.11
Double : 18.781
long[double to long] : 18
long[Double to long] : 18
Long[double to Long] : 18
Long[Double to Long] : 18

 

 

Double/double to Integer/int Convert in Java Example:

 

Double can be converted to Integer/int using intValue() method.

 

But double primitive data type should be converted to Double wrapper class before converting to int/Integer, because intValue() exist only in Integer wrapper class.
Then we can use the same intValue() method to convert double to Integer/int.

 

Double/double to Integer/int Conversion Java Program:


package in.javadomain;

public class DoubleToInteger {
public static void main(String[] args) {
double d = 18.11;
Double d1 = 18.781;

int i = Double.valueOf(d).intValue();
int i1 = d1.intValue();

Integer i2 = Double.valueOf(d).intValue();
Integer i3 = d1.intValue();

System.out.println("double : " + d);
System.out.println("Double : " + d1);
System.out.println("int[double to int] : " + i);
System.out.println("int[Double to int] : " + i1);
System.out.println("Integer[double to Integer] : " + i2);
System.out.println("Integer[Double to Integer] : " + i3);
}
}

 

Output:


double : 18.11
Double : 18.781
int[double to int] : 18
int[Double to int] : 18
Integer[double to Integer] : 18
Integer[Double to Integer] : 18

 

 

Double/double to Float/float Convert in Java Example:

 

Double can be converted to Float/float using floatValue() method.

 

But double primitive data type should be converted to Float wrapper class before converting to float/Float, because floatValue() exist only in Float wrapper class.
Then we can use the same floatValue() method to convert double to Float/float.

 

Double/double to Float/float Conversion Java Program:


package in.javadomain;

public class DoubleToFloat {
public static void main(String[] args) {
double d = 18.1112982;
Double d1 = 18.781129812;

float f = Double.valueOf(d).floatValue();
float f1 = d1.floatValue();

Float f2 = Double.valueOf(d).floatValue();
Float f3 = d1.floatValue();

System.out.println("double : " + d);
System.out.println("Double : " + d1);
System.out.println("float[double to float] : " + f);
System.out.println("float[Double to float] : " + f1);
System.out.println("Float[double to Float] : " + f2);
System.out.println("Float[Double to Float] : " + f3);
}
}

 

Output:


double : 18.1112982
Double : 18.781129812
float[double to float] : 18.111298
float[Double to float] : 18.78113
Float[double to Float] : 18.111298
Float[Double to Float] : 18.78113

 

Double/double to BigDecimal Convert in Java Example:

 

Double can be converted to BigDecimal using BigDecimal.valueOf() method.

 

 

Double/double to BigDecimal Conversion Java Program:


package in.javadomain;

import java.math.BigDecimal;

public class DoubleToBigDecimal {
public static void main(String[] args) {
double d = 18.1112982;
Double d1 = 18.781129812;

BigDecimal bd1 = BigDecimal.valueOf(d);
BigDecimal bd2 = BigDecimal.valueOf(d1);

System.out.println("double : " + d);
System.out.println("Double : " + d1);
System.out.println("BigDecimal[double to BigDecimal] : " + bd1);
System.out.println("BigDecimal[Double to BigDecimal] : " + bd2);
}
}

 

Output:


double : 18.1112982
Double : 18.781129812
BigDecimal[double to BigDecimal] : 18.1112982
BigDecimal[Double to BigDecimal] : 18.781129812

 

String to Integer/int Convert in Java Example:

 

String can be converted to Integer/int using Integer.parseInt() method.

 

String to Integer/int Conversion Java Program:


package in.javadomain;

public class StringToInteger {

public static void main(String[] args) {
String s = "15";
Integer a = Integer.parseInt(s);
System.out.println("String : " + s);
System.out.println("Integer[String to Integer] : " + a);
}

}

 

Output:


String : 15
Integer[String to Integer] : 15

 

 

String to char/Character Convert in Java Example:

 

String can be converted to char/Character using charAt() method.

String to char/Character Java Program:


package in.javadomain;

public class StringToChar {

public static void main(String[] args) {
String s = "Java";
char c = s.charAt(0);
Character c1 = s.charAt(0);
System.out.println("String : " + s);
System.out.println("Char[String to Char] - Printing only First character : " + c);
System.out.println("Character[String to Character] - Printing only First character : " + c1);
}

}

 

Output:


String : Java
Char[String to Char] - Printing only First character : J
Character[String to Character] - Printing only First character : J

 

 

String to Float/float Convert in Java Example:

 

String can be converted to Float/float using Float.valueOf() method.

String to Float/float Java Program:


package in.javadomain;

public class StringToFloat {

public static void main(String[] args) {
String s = "5.081988090238232023092";
Float f = Float.valueOf(s);
float f1 = Float.valueOf(s);
System.out.println("String : " + s);
System.out.println("Float[String to Float] : " + f);
System.out.println("float[String to float] : " + f1);
}

}

 

Output:


String : 5.081988090238232023092
Float[String to Float] : 5.081988
float[String to float] : 5.081988

 

String to Double/double Convert in Java Example:

 

String can be converted to Double/double using Double.valueOf() method.

String to Double/double Java Program:


package in.javadomain;

public class StringToDouble {

public static void main(String[] args) {
String s = "11111985";
Double d = Double.valueOf(s);
double d1 = Double.valueOf(s);

System.out.println("String : " + s);
System.out.println("Double[String to Double] : " + d);
System.out.println("double[String to double] : " + d1);
}

}

 

Output:


String : 11111985
Double[String to Double] : 1.1111985E7
double[String to double] : 1.1111985E7

 

 

String to Date Convert in Java Example:

 

String can be converted to Date using SimpleDateFormat parse methods. You can also define the date output formats in the simpledateformat.

String to Date Java Program:

package in.javadomain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

public static void main(String[] args) throws ParseException {
String s = "11-01-1991";
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Date d = sdf.parse(s);

System.out.println("String : " + s);
System.out.println("Date[String to Date] : " + d);
}

}

Output:


String : 11-01-1991
Date[String to Date] : Fri Jan 11 00:01:00 IST 1991

 

 

Hope I have given all the required conversions in java. If you feel anything is missing feel free to mention in the comments area to include.

607 total views, 2 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in