Java基础第四天--常用API

常用API

基本类型包装类概述

将基本数据类型封装成对象的好处可以在对象中定义更多的功能方法操作该数据

常用的操作之一:用于基本数据类型与字符串之间的转换

基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Integer类的概述和使用

Integer:包装一个对象中的原始类型int的值

方法名 说明
public Integer (int value) 根据int值创建Integer对象(过时)
public Integer (String s) 根据String值创建Integer对象(过时)
public static Integer valueOf(int i) 返回表示指定的int值的Integer实例
public static Integer valueOf(String s) 返回一个保存指定值的Integer对象String

int和String的相互转换

基本类型包装类的最常见操作是:用于基本类型和字符串之间的相互转换

1.int转换为String

public static String valueOf(int i):返回int参数的字符串表示形式。该方法是String类中的方法

2.String转换为int

public static int parseInt(String s):将字符串解析为int类型。该方法是Integer类中的方法

    /*
    int和String的相互转换
    */
    public class IntegerDemo {
        public static void main(String[] args) {
            //int---String
            int number = 100;
            //方式1
            String s1 = "" + number;
            System.out.println(s1);
            //方式2
            String s2 = String.valueOf(number);
            System.out.println(s2);
            System.out.println("========");

            //String---int
            String 2 = "100";
            //方式1
            //String---Integer---int
            Integer i =Integer.valueOf(s);
            int x = i.intValue();
            System.out.println(x);
            //方式2
            int y = Integer.parseInt(s);
            System.out.println(y);
        }
    }

案例:字符串中数据排序

需求:有一个字符串"91 27 46 38 50",请写程序实现最终输出结果是"27 38 46 50 91"

    public class IntegerTest {
        public static void main(String[] args) {
            //定义一个字符串
            String s = "91 27 46 38 50";
            
            //把字符串中的数据存储到一个int类型的数组中
            String[] strArray = s.split(" ");
    //        for (int i = 0; i < strArray.length; i++) {
    //        System.out.println(strArray[i]);
    //        }
            
            //定义一个int数组,把String[]数组中的每一个元素存储到int数组中
            int[] arr = new int[strArray.length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = Integer.parseInt(strArray[i]);
            }
    //        for (int i = 0; i < arr.length; i++) {
    //            System.out.println(arr[i]);
    //        }
            
            //对int数组进行排序
            Arrays.sort(arr);
            
            //把排序后的int数组中的元素进行拼接得到一个字符串,这里拼接采用StringBuilder来实现
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < arr.length; i++) {
                if(i == arr.length - 1) {
                    sb.append(arr[i]);
                }else {
                    sb.append(arr[i]).append(" ");
                }
            }
            String result = sb.toString();
            
            //输出结果
            System.out.println("result:" + result);
            //result:27 38 46 50 91
        }
    }

自动装箱和拆箱

  • 装箱:把基本数据类型转换为对应的包装类类型
  • 拆箱:把包装类类型转换为对应的基本数据类型

    注意:在使用包装类类型的时候,如果做操作,最好先判断是否为null,推荐的是,只要是对象,在使用前就必须进行不为null的判断
    public class IntegerDemo {
        public static void main(String[] args) {
            //装箱:把基本数据类型转换为对应的包装类类型
            Integer.valueOf(100);
            Integer ii = 100;//Integer.valueOf(100);
            
            //拆箱:把包装类类型转换为对应的基本数据类型
            //ii += 200;
    //        ii = ii.intValue() + 200;
            ii += 200;
            System.out.println(ii);
            
            Integer iii = null;
            if(iii != null) {
                iii += 300;
            }       
        }
    }

日期类

Date类概述和构造方法

Date代表了一个特定的时间,精确到毫秒

方法名 说明
public Date() 分配一个Date对象,并初始化,以使它代表它被分割的时间,精确到毫秒
public Date(long date) 分配一个Date对象,并将其初始化为表示从标准化时间起指定的毫秒值

Date类的常用方法

方法名 说明
public long getTime() 获取的是日期对象从1970年1月1日00:00:00到现在的毫秒值
Public void setTime(long time) 设置时间,给的是毫秒值

SimpleDateFormat类概述

1.SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式和解析日期。

2.日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从'A'到'Z'以及从'a'到'z'引号的字母被解释为时间日期或时间字符串的组件的模式字母

3.常用的模式字母以及对应关系如下:

  • y        年
  • M       月
  • d        日
  • H        时
  • m       分
  • s         秒

SimpleDateFormat的构造方法

方法名 说明
public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式

SimpleDateFormat格式化和解析日期

1.格式化(从Date到String)

public final String format(Date date):将日期格式化成日期/时间字符串

2.解析(从String到Date)

public Date parse(String source):从给定字符串的开始解析文本以生成日期

Calendar的常用方法

方法名 说明
public int get(int field) 返回给定日历字段的值
public abstract void add(int field,int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段
public final void set(int year,int mouth,int date) 设置当前日历的年月日

猜你喜欢

转载自www.cnblogs.com/energy-xjq/p/fourth_day.html