Java type conversion (String, int, Date, Calendar, etc.)

When I usually do development, I forget it, so it's a reminder. As soon as I can think of it, I will correct it.

・ [Character string ⇔ number](# character string number) -[Character string ⇔ character string](# character string character string) -[Date type ⇔ date type, character string](# date type date type character string)

Character string ⇔ numerical value

String ⇔ int

qiita.java


   //int → String
   int num = 0;
   String intToString = String.valueOf(num);

   //String → int
   String number = "123";
   int stringToInt = Integer.parseInt(number);

String conversion from double type, float type and long type can be done in the same way. For the time being, just throw it in String.valueOf () ...

Supplement: (2020/09/11: correction)

qiita.java


   String number = "123";

   //String → double
   double d = Double.parseDouble(number);

   //String → float
   float f = Float.parseFloat(number);

   //String → long
   long l = Long.parseLong(number);

Character string ⇔ character string

String ⇔ char

qiita.java


   //char → String
   char chr = 'Chi';
   String charToString = String.valueOf(chr);

   //String → char
   String str = "Sutoringu";
   char stringToChar = str.charAt(0);

   //String → char[]
   char[] stringToCharList = str.toCharArray();

   //char[] → String 
   char[] charList = new char[] { '1', 'Sentence', 'Character' };
   String charListToString = new String(charList);

Date type ⇔ date type, character string

Calendar ⇔ Date

qiita.java


   import java.util.Date;
   import java.util.Calendar;

   //Date → Calendar
   Date date = new Date();
   Calendar cal = Calendar.getInstance();
   cal.setTime(date);

   //Calendar → Date
   Calendar cal = Calendar.getInstance();
   Date calendarToDate = cal.getTime();

Date ⇔ String

qiita.java


   import java.text.ParseException;
   import java.text.SimpleDateFormat;
   import java.util.Date;
   
   //Date → String
   Date date = new Date();
   String dateToString = String.valueOf(date);

   //String → Date
   String day = "2020/09/10 21:00:00";
   SimpleDateFormat sdf = new SimpleDateFormat(day);
   try{
      Date stringToDate = sdf.parse(day);
   } catch(ParseException e){
      e.printStackTrace();
   }

It seems that it is not possible to make a String directly from the Calendar type. → I was able to convert it with String.valueOf (), but it was not an easy-to-understand character string. Is it basically Calendar ⇔ Date ⇔ String?

.oO (There are a lot of String.valueOf available ... Thank you)

end

Recommended Posts

Java type conversion (String, int, Date, Calendar, etc.)
Java date data type conversion (Date, Calendar, String)
[Java] Date type conversion
Java type conversion
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Type conversion from java BigDecimal type to String type
Uri → String, String → Uri type conversion
[Java] List type / Array type conversion
Conversion of String, Date, LocalDate
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
Java Primer Series (Type Conversion)
[Java Bronze] Learning memo (interface, static method, type conversion, etc.)
[Java] Correct comparison of String type
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
[Java ~ Variable definition, type conversion ~] Study memo
[Java] Data type / string class cheat sheet
Java study # 3 (type conversion and instruction execution)
Notes on operators using Java ~ String type ~
[Java] Convert Object type null to String type
[Basic knowledge of Java] About type conversion
[Java] Comparison of String type character strings
[Java] Calculation mechanism, operators and type conversion
[Java] How to convert one element of a String type array to an Int type
[Introduction to Java] About type conversion (cast, promotion)
[Java] char type can be cast to int type
[Java] How to use Calendar class and Date class
[JAVA] Stream type
[Java] Enumeration type
Java Optional type
Java double type
Java string processing
Date () and Calendar ()
Java-automatic type conversion
Split string (Java)
[Java] Get the date 10 days later with the Calendar class