各种数据类型转换,字符串String转换成整数int等
各种数据类型转换,字符串String转换成整数int等
字符串String转换成整数int
1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
整数 int 转换成字符串 String
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = “” + i;
各种数字类型转换成字符串型:
String s = String.valueOf( value); // 其中 value 为任意一种数字类型。
数字类型与数字类对象之间的转换:
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i );
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
字符串型转换成各种数字类型:
String s = “169”;
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );
时间转换:
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;
/**
-
description: 时间格式
-
author: kevin
-
date: 2017/6/13 19:46
/
public class DateUtils {
/* 日志对象 *//** 年-月-日 时:分:秒 显示格式 */
// 备注:如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式。
public static String DATE_TO_STRING_DETAIAL_PATTERN = “yyyy-MM-dd HH:mm:ss”;/** 年-月-日 显示格式 */
public static String DATE_TO_STRING_SHORT_PATTERN = “yyyy-MM-dd”;private static SimpleDateFormat simpleDateFormat;
/**
- Date类型转为指定格式的String类型
- @param source
- @param pattern
- @return
*/
public static String DateToString(Date source, String pattern) {
simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(source);
}
/**
- 获取现在时间
- @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);