|
| 1 | +package com.baeldung.java.conversion; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | +import java.time.LocalDateTime; |
| 6 | +import java.util.Date; |
| 7 | + |
| 8 | +import com.baeldung.datetime.UseLocalDateTime; |
| 9 | + |
| 10 | +public class StringConversion { |
| 11 | + |
| 12 | + public static int getInt(String str) { |
| 13 | + return Integer.parseInt(str); |
| 14 | + } |
| 15 | + |
| 16 | + public static int getInteger(String str) { |
| 17 | + return Integer.valueOf(str); |
| 18 | + } |
| 19 | + |
| 20 | + public static long getLongPrimitive(String str) { |
| 21 | + return Long.parseLong(str); |
| 22 | + } |
| 23 | + |
| 24 | + public static Long getLong(String str) { |
| 25 | + return Long.valueOf(str); |
| 26 | + } |
| 27 | + |
| 28 | + public static double getDouble(String str) { |
| 29 | + return Double.parseDouble(str); |
| 30 | + } |
| 31 | + |
| 32 | + public static double getDoublePrimitive(String str) { |
| 33 | + return Double.valueOf(str); |
| 34 | + } |
| 35 | + |
| 36 | + public static byte[] getByteArray(String str) { |
| 37 | + return str.getBytes(); |
| 38 | + } |
| 39 | + |
| 40 | + public static char[] getCharArray(String str) { |
| 41 | + return str.toCharArray(); |
| 42 | + } |
| 43 | + |
| 44 | + public static boolean getBooleanPrimitive(String str) { |
| 45 | + return Boolean.parseBoolean(str); |
| 46 | + } |
| 47 | + |
| 48 | + public static boolean getBoolean(String str) { |
| 49 | + return Boolean.valueOf(str); |
| 50 | + } |
| 51 | + |
| 52 | + public static Date getJava6Date(String str, String format) throws ParseException { |
| 53 | + SimpleDateFormat formatter = new SimpleDateFormat(format); |
| 54 | + return formatter.parse(str); |
| 55 | + } |
| 56 | + |
| 57 | + public static LocalDateTime getJava8Date(String str) throws ParseException { |
| 58 | + return new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); |
| 59 | + } |
| 60 | +} |
0 commit comments