包装类和日期类

包装类:
因为基本类型不能参与面向对象的开发(基本对象不能创建对象,也不能调用方法),
所以将基本类型数据封装成一个对象,可以在对象中定义更多的功能和方法去操作该数据(这个封装的对象就是包装类对象)


/*
    基本类型对应的包装类
	基本数据类型 		包装类
	byte			Byte
	short			Short
	int				Integer
	long			Long
	float			Float
	double			Double
	char			Character
	boolean			Boolean
常用操作一:用于基本类型与字符串之间的转换
 */
public class Demo {
    public static void main(String[] args) {
        int a=2;
        String str="12";
        /*

         */
        //以Integer为例,讲解包装类的使用
        /*
        Integer  类中包装了一个原始类型为int的值
        Boolean  类中包装了一个原始类型为boolean的值

        Integer 类构造方法
            public Integer(int value)   根据int值创建Integer对象
            public Integer(String s)    根据String值创建Integer对象
        Boolean 类构造方法
            public Boolean(Boolean b)   根据Boolean值创建Boolean对象
            public Boolean(String s)    根据String值创建Boolean对象

         */
        Integer i1=new Integer(10);
        System.out.println(i1);
        Integer i2=new Integer("123");
        System.out.println(i2);
//        Integer i3=new Integer("znf");
//        System.out.println(i3); 报错  znf不是一个数字

        Boolean b=new Boolean("hh");
        System.out.println(b);//输出false 只要待转不是true,就都是false

        /*
        静态方法获取对应包装类对象Integer
        public static Integer vaLueOf(int i)   根据指定的int值返回一个Integer实例
        public static Integer vaLueOf(String i)   根据指定的String值返回一个Integer实例
         */

        //1
        Integer i3=Integer.valueOf(10);//根据10创建一个Integer实例并返回到i3
        System.out.println(i3);
        //2
        Integer i4=Integer.valueOf("123456");
        System.out.println(i4);

        /*
        静态方法获取对应包装类对象Double
         public static Double vaLueOf(double b)   根据指定的double值返回一个Double实例
         public static Double vaLueOf(String i)   根据指定的String值返回一个Double实例
         */
        Double d1=Double.valueOf(3.14);
        Double d2=Double.valueOf("6.21");
        /*
        结论
        两个构造
        public 包装类名(对应的基本类型 变量名)
        public 包装类名(Sting s)

        两个静态
         public static 包装类名 vaLueOf(对应的基本类型  变量名)
         public static 包装类名 vaLueOf(String s)
         */
    }

}



/*
    int和String类型的互相转换

      String-->int:调用的是int 的包装类中的parseInt(String s)方法
      int----->String :调用的是String的valueOf(int i)

 */
public class Demo01 {
    public static void main(String[] args) {
        /*
        int--->String
        1.直接在int后面加字符串
        2.String 类中有一个静态的方法 public static String valueOf(int i)
         */
        int num=100;
        String s1=num+"";

        String s2=String.valueOf(num);
        System.out.println(s2);


        /*
        String--->int
        1.String-->Integer--->int
        2.String-->int(使用的是包装类的方法)
            Public static int ParseInt(String s)
         */
        //1
       String s3="1234";
       //将String-->Integer
        Integer i1=Integer.valueOf(s3);
        //将Integer-->int
        /*
        Integer 中有一个方法可以获取int类型的值
        public int intValue()
         */
        int a=i1.intValue();

        //2:将Sting-->int
        // Public static int ParseInt(String s)
        int b=Integer.parseInt(s3);
        System.out.println(b);


        /*
        String-->double
         */

        String s7="6.23";
        double d=Double.parseDouble(s7);

        /*
        自动拆装箱
        自动装箱:
        自动装箱:自动把基本数据类型转化为对应的包装类
        自动拆箱:自动把包装类转化为对应的基本数据类型
         */
        Integer i8=600;//自动装箱,把600自动转化转化为Integer对象
        i8=i8+200;//自动拆装箱,i8自动拆箱为600做加法运算,结果为int类型的800,自动装箱为Integer对象

    }
}

import java.util.Date;
/*
    日期类型
        Date类:代表一个特定的时间,精确到毫秒;从起始时间到现在的毫秒值
            计算机有一个起始时间,1970年1月1日 00:00:00 美国时间
        Date构造
            public Date();创建一个Date对象,并初始化当前时间
            public Date(long date);创建一个Date对象,并初始化从起始时间到指定时间的毫秒值
 */
public class DateDemo01 {
    public static void main(String[] args) {
        //1
        Date date=new Date();
        System.out.println(date);
        //2
        long l=3600000;//一个小时的毫秒值:1000*60*60
        Date date1=new Date(l);//东八区+8小时  1970年1月1日 0:0:0开始
        System.out.println(date1);//输出 Thu Jan 01 09:00:00 CST 1970

	/*
       Date 常用方法
       getYear():弃用,存在千年虫的问题

       long   getTime():获取日期对象从1970年1月1日0:0:0到现在的毫秒值
       void   setTime(Long time) 设置时间为给定的毫秒值


 */
	Date date=new Date();
        /*
        千年虫的问题:可以理解成计算机的一个bug
                计算机用于表示年份使用的是两位十进制表示的,
                如果设置跨世纪处理问题,就会出现千年虫的问题
         */
//        System.out.println(date.getYear()+1900);
//        System.out.println(date.getMonth()+1);

        //获取date的时间1970年到现在时间的毫秒值
        System.out.println(date.getTime());
        //获取date设置为毫秒值3600000,现在date为1970年1月1日09时0分0秒
        date.setTime(3600000);
    }
}

SimpLeDateFormat

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/*
    SimpleDateFormat类
    格式化和解析时间
    构造方法
        public SimpleDateFormat();构造一个SimpleDateFormat对象,使用默认的日期格式
        public SimpleDateFormat(String pattern);使用给定的格式,格式化日期
    常用方法
        格式化(Date转为String)
        public final String format(Date date)
        解析(String转为Date)
        public Date parse(String source)
 */
public class SimpLeDateFormat {
    public static void main(String[] args) throws ParseException {
        //调用SimpleDateFormat无参构造
        SimpleDateFormat sdf1=new SimpleDateFormat();
        //调用SimpleDateFormat的format方法并传入Date对象
        String sd=sdf1.format(new Date());
        System.out.println(sd);
        /*
        * y 年
        * M 月
        * W 月份中的周数
        * d 月份中的天数
        * F 月份中的星期
        * E 星期中的天数
        * H 一个天中的小时数
        * m 分钟
        * s 秒
        *
        *
        * */
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss E");
        String sdf3=sdf2.format(new Date());
        System.out.println(sdf3);

        SimpleDateFormat sdf4=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String sdf="2022/03/17 14:22:22";
        Date d=sdf4.parse(sdf);//sdf4和sdf格式要对应不然解析不了
        System.out.println(sdf2.format(d));

    }
}

Calendar


import java.util.Calendar;
import java.util.GregorianCalendar;


/*
    Calendar类
        提供操作日历字段的方法
        1.是一个抽象类,如果创建的对象需要通过其子类对象来创建
            Calendar cal=new GregorianCalendar();
        2.使用静态方法获取对象
            Calendar cal2=Calendar.getInstance();

        常用方法:
        public int get(int field) 返回给定日历字段的值
 */
public class CalendarDemo01 {
    public static void main(String[] args) {
        
        //1
        Calendar cal=new GregorianCalendar();
        //2
        Calendar cal2=Calendar.getInstance();
        //获取年份
        System.out.println(cal2.get(Calendar.YEAR));//输出年份,该Calendar.YEAR用于表示年份
        System.out.println(cal2.get(1));
        //获取月份(在月份的基础上+1才是实际月份)
        System.out.println(cal2.get(Calendar.MARCH)+1);
        //获取天
        System.out.println(cal2.get(Calendar.DATE));
        System.out.println(cal2.get(Calendar.DAY_OF_MONTH));

        //获取周几
        System.out.println(cal2.get(Calendar.DAY_OF_WEEK)-1);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值