各种数据类型转换,字符串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);
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值