Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Monday, 20 April 2020

Java Convert LocalDate to Date

'java.util.Date' represent date + time + timezone whereas 'java.time.LocalDate' represent only date portion.

Below snippet converts the Date to LocalDate.

Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
LocalDate localDate = zonedDateTime.toLocalDate();

App.java
package com.sample.app;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class App {

	public static void main(String args[]) throws InterruptedException {

		Date date = new Date();
		Instant instant = date.toInstant();
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zonedDateTime = instant.atZone(zoneId);
		LocalDate localDate = zonedDateTime.toLocalDate();

		System.out.println("localDate : " + localDate);

	}

}

Output
localDate : 2020-03-07



You may like
Previous                                                    Next                                                    Home

Monday, 6 April 2020

Convert List to a Map

Approach 1: By traversing each element of the list.
Map<Integer, Employee> map1 = new HashMap<>();
for (Employee emp : emps) {
         map1.put(emp.getId(), emp);
}

Approach 2: Using stream() method
Map<Integer, Employee> map2 = emps.stream()
                  .collect(Collectors.toMap(Employee::getId, emp -> emp, (empObj1, empObj2) -> {
                           return empObj1;
                  }));

Approach 3: Using parallelStream() method
Map<Integer, Employee> map3 = emps.parallelStream()
                  .collect(Collectors.toMap(Employee::getId, item -> item, (empObj1, empObj2) -> {
                           return empObj1;
                  }));

Find the below working application.

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.sample.app.model.Employee;

public class App {

 private static void printMap(Map<Integer, Employee> map) {
  Set<Integer> set = map.keySet();

  for (int key : set) {
   System.out.println(map.get(key));
  }

  System.out.println("-----------------------------");

 }

 public static void main(String args[]) {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Narasimha", "Rao");
  Employee emp3 = new Employee(3, "Ram", "Pothineni");
  Employee emp4 = new Employee(4, "Jaidepp", "Geera");
  Employee emp5 = new Employee(5, "Joel", "Chelli");

  List<Employee> emps = Arrays.asList(emp1, emp2, emp3, emp4, emp5);

  Map<Integer, Employee> map1 = new HashMap<>();
  for (Employee emp : emps) {
   map1.put(emp.getId(), emp);
  }

  Map<Integer, Employee> map2 = emps.stream()
    .collect(Collectors.toMap(Employee::getId, emp -> emp, (empObj1, empObj2) -> {
     return empObj1;
    }));

  Map<Integer, Employee> map3 = emps.parallelStream()
    .collect(Collectors.toMap(Employee::getId, item -> item, (empObj1, empObj2) -> {
     return empObj1;
    }));

  printMap(map1);
  printMap(map2);
  printMap(map3);
 }

}

Output

Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------


You may like

Wednesday, 1 April 2020

Convert string to float

Approach 1: Using Float.parseFloat method
float f1 = Float.parseFloat(str);

Approach 2: Using 'Float.valueOf' method
float f2 = Float.valueOf(str).floatValue();

App.java
package com.sample.app;

public class App {

    public static void main(String args[]) {
        String str = "1.2345";

        float f1 = Float.parseFloat(str);
        float f2 = Float.valueOf(str).floatValue();

        System.out.println("f1: " + f1);
        System.out.println("f2: " + f2);
    }

}

Output
f1: 1.2345
f2: 1.2345

You may like

Convert Float to a string

Approach 1: Use toString() method of Float class.
String str1 = floatVar.toString();
String str2 = Float.toString(floatVar);

Approach 2: Using FloatingDecimal.toJavaFormatString.
String str3 = FloatingDecimal.toJavaFormatString(floatVar);

Approach 3: Using String.valueOf method.
String str4 = String.valueOf(floatVar);

Approach 4: Using DecimalFormat.
String str5 = new DecimalFormat("#.00").format(floatVar);

Find the below working application.

App.java
package com.sample.app;

import java.text.DecimalFormat;

import sun.misc.FloatingDecimal;

public class App {

    public static void main(String args[]) {
        Float floatVar = 1.2345f;

        String str1 = floatVar.toString();
        String str2 = Float.toString(floatVar);
        String str3 = FloatingDecimal.toJavaFormatString(floatVar);
        String str4 = String.valueOf(floatVar);
        String str5 = new DecimalFormat("#.00").format(floatVar);
        
        System.out.println("str1 : " + str1);
        System.out.println("str2 : " + str2);
        System.out.println("str3 : " + str3);
        System.out.println("str4 : " + str4);
        System.out.println("str5 : " + str5);
    }

}

Output

str1 : 1.2345
str2 : 1.2345
str3 : 1.2345
str4 : 1.2345
str5 : 1.23

You may like

Tuesday, 31 March 2020

Java: Convert int to Long

Approach 1: By explicit casting.
Long result1 = (long) i;

Approach 2: Using 'Long.valueOf' method.
Long result2 = Long.valueOf(i);

Approach 3: Using Integer.longValue().
Integer intObj = i;
Long result3 = intObj.longValue();

Approach 4: Using Long constructor.
Long result4 = new Long(i);

App.java
package com.sample.app;

public class App {

  public static void main(String args[]) {
    int i = 10;

    Long result1 = (long) i;

    Long result2 = Long.valueOf(i);

    Integer intObj = i;
    Long result3 = intObj.longValue();

    Long result4 = new Long(i);

    System.out.println("result1 : " + result1);
    System.out.println("result2 : " + result2);
    System.out.println("result3 : " + result3);
    System.out.println("result4 : " + result4);

  }

}

Output

result1 : 10
result2 : 10
result3 : 10
result4 : 10


You may like

Convert Object to int

Approach 1: Using explicit casting.
int i = (int) obj;

Approach 2: Using intValue() method.
i = ((Integer) obj).intValue();

Approach 3: Using parseInt() method.
i = Integer.parseInt(obj.toString());

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {

  Object obj = 10;

  int i = (int) obj;
  System.out.println("i = " + i);

  i = ((Integer) obj).intValue();
  System.out.println("i = " + i);
  
  i = Integer.parseInt(obj.toString());
  System.out.println("i = " + i);
 }

}

Output

i = 10
i = 10
i = 10



You may like