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)
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);
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);
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