String转换为int: Integer.parseInt(), Integer.valueOf()
int转换为String: String.valueOf(), Integer.toString()
String转换为long: Long.parseLong()
long转换为String: String.valueOf(), Long.toString()
String转换为float: Float.parseFloat()
float转换为String: String.valueOf(), Float.toString()
String转换为double: Double.parseDouble()
double转换为String: String.valueOf(), Double.toString()
String转换为char: char c = str.charAt("index"
);
char转换为String: String.valueOf(char), Character.toString(char)
String转换为Object: Object obj = str;
Object转换为String: obj.toString(), String.valueOf(obj)
String转换为Date:DateFormat,SimpleDateFormat
Date date=new SimpleDateFormat("dd/MM/yyyy").parse(sDate);
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
Date date=formatter.parse(sDate);
- SimpleDateFormat formatter=new SimpleDateFormat(“dd-MMM-yyyy”);
- SimpleDateFormat formatter=new SimpleDateFormat(“MM dd, yyyy”);
- SimpleDateFormat formatter=new SimpleDateFormat(“E, MMM dd yyyy”);
- SimpleDateFormat formatter=new SimpleDateFormat(“E, MMM dd yyyy HH:mm:ss”);
- SimpleDateFormat formatter=new SimpleDateFormat(“dd-MMM-yyyy HH:mm:ss”);
Date转换为String:DateFormat,SimpleDateFormat
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String strDate = formatter.format(date);
Date转换为Timestamp
Date date = new Date();
Timestamp ts=new Timestamp(date.getTime());
Timestamp转换为Date
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date(ts.getTime());