常用类-BigInteger类、BigDecimal类、Date/DateFormat类、Calendar类

1:BigInteger(理解)
(1)针对大整数的运算
(2)构造方法
A:BigInteger(String s)

/*
 * BigInteger:可以让超过Integer范围内的数据进行运算
 * 
 * 构造方法:
 * BigInteger(String val) 
 */
public class BigIntegerDemo {
    public static void main(String[] args) {
        // 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。
        // Integer i = new Integer(100);
        // System.out.println(i);
        // // System.out.println(Integer.MAX_VALUE);
        // Integer ii = new Integer("2147483647");
        // System.out.println(ii);
        // // NumberFormatException
        // Integer iii = new Integer("2147483648");
        // System.out.println(iii);

        // 通过大整数来创建对象
        BigInteger bi = new BigInteger("2147483648");
        System.out.println("bi:" + bi);
    }
}
(3)成员方法(自己补齐)
    A:加
    B:减
    C:乘
    D:除
    E:商和余数
/*
 * public BigInteger add(BigInteger val):加
 * public BigInteger subtract(BigInteger val):减
 * public BigInteger multiply(BigInteger val):乘
 * public BigInteger divide(BigInteger val):除
 * public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
 */
public class BigIntegerDemo {
    public static void main(String[] args) {
        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("50");

        // public BigInteger add(BigInteger val):加
        System.out.println("add:" + bi1.add(bi2));
        // public BigInteger subtract(BigInteger val):加
        System.out.println("subtract:" + bi1.subtract(bi2));
        // public BigInteger multiply(BigInteger val):加
        System.out.println("multiply:" + bi1.multiply(bi2));
        // public BigInteger divide(BigInteger val):加
        System.out.println("divide:" + bi1.divide(bi2));

        // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
        BigInteger[] bis = bi1.divideAndRemainder(bi2);
        System.out.println("商:" + bis[0]);
        System.out.println("余数:" + bis[1]);
    }
}

2:BigDecimal(理解)
(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)
(2)构造方法
A:BigDecimal(String s)
(3)成员方法:
A:加
B:减
C:乘
D:除
E:自己保留小数几位

/*
 * 构造方法:
 *      public BigDecimal(String val)
 * 
 * public BigDecimal add(BigDecimal augend)
 * public BigDecimal subtract(BigDecimal subtrahend)
 * public BigDecimal multiply(BigDecimal multiplicand)
 * public BigDecimal divide(BigDecimal divisor)
 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取
 */
public class BigDecimalDemo {
    public static void main(String[] args) {
        // System.out.println(0.09 + 0.01);
        // System.out.println(1.0 - 0.32);
        // System.out.println(1.015 * 100);
        // System.out.println(1.301 / 100);

        BigDecimal bd1 = new BigDecimal("0.09");
        BigDecimal bd2 = new BigDecimal("0.01");
        System.out.println("add:" + bd1.add(bd2));
        System.out.println("-------------------");

        BigDecimal bd3 = new BigDecimal("1.0");
        BigDecimal bd4 = new BigDecimal("0.32");
        System.out.println("subtract:" + bd3.subtract(bd4));
        System.out.println("-------------------");

        BigDecimal bd5 = new BigDecimal("1.015");
        BigDecimal bd6 = new BigDecimal("100");
        System.out.println("multiply:" + bd5.multiply(bd6));
        System.out.println("-------------------");

        BigDecimal bd7 = new BigDecimal("1.301");
        BigDecimal bd8 = new BigDecimal("100");
        System.out.println("divide:" + bd7.divide(bd8));
        System.out.println("divide:"
                + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
        System.out.println("divide:"
                + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
    }
}

3:Date/DateFormat(掌握)
(1)Date是日期类,可以精确到毫秒。
A:构造方法
Date()
Date(long time)

/*
 * Date:表示特定的瞬间,精确到毫秒。 
 * 
 * 构造方法:
 *      Date():根据当前的默认毫秒值创建日期对象
 *      Date(long date):根据给定的毫秒值创建日期对象
 */
public class DateDemo {
    public static void main(String[] args) {
        // 创建对象
        Date d = new Date();
        System.out.println("d:" + d);

        // 创建对象
        // long time = System.currentTimeMillis();
        long time = 1000 * 60 * 60; // 1小时
        Date d2 = new Date(time);
        System.out.println("d2:" + d2);
    }
}
    B:成员方法
        getTime()
        setTime(long time)
/*
 * public long getTime():获取时间,以毫秒为单位
 * public void setTime(long time):设置时间
 * 
 * 从Date得到一个毫秒值
 *      getTime()
 * 把一个毫秒值转换为Date
 *      构造方法
 *      setTime(long time)
 */
public class DateDemo {
    public static void main(String[] args) {
        // 创建对象
        Date d = new Date();

        // 获取时间
        long time = d.getTime();
        System.out.println(time);
        // System.out.println(System.currentTimeMillis());

        System.out.println("d:" + d);
        // 设置时间
        d.setTime(1000);
        System.out.println("d:" + d);
    }
}
    C:日期和毫秒值的相互转换
    案例:你来到这个世界多少天了?
/*
 * 算一下你来到这个世界多少天?
 * 
 * 分析:
 *      A:键盘录入你的出生的年月日
 *      B:把该字符串转换为一个日期
 *      C:通过该日期得到一个毫秒值
 *      D:获取当前时间的毫秒值
 *      E:用D-C得到一个毫秒值
 *      F:把E的毫秒值转换为年
 *          /1000/60/60/24
 */
public class MyYearOldDemo {
    public static void main(String[] args) throws ParseException {
        // 键盘录入你的出生的年月日
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的出生年月日:");
        String line = sc.nextLine();

        // 把该字符串转换为一个日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(line);

        // 通过该日期得到一个毫秒值
        long myTime = d.getTime();

        // 获取当前时间的毫秒值
        long nowTime = System.currentTimeMillis();

        // 用D-C得到一个毫秒值
        long time = nowTime - myTime;

        // 把E的毫秒值转换为年
        long day = time / 1000 / 60 / 60 / 24;

        System.out.println("你来到这个世界:" + day + "天");
    }
}
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
    A:SimpleDateFormat(String pattern) 给定模式
        yyyy-MM-dd HH:mm:ss
/*
 * Date  --  String(格式化)
 *      public final String format(Date date)
 * 
 * String -- Date(解析)
 *      public Date parse(String source)
 * 
 * DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
 * 
 * SimpleDateFormat的构造方法:
 *      SimpleDateFormat():默认模式
 *      SimpleDateFormat(String pattern):给定的模式
 *          这个模式字符串该如何写呢?
 *          通过查看API,我们就找到了对应的模式
 *          年 y
 *          月 M 
 *          日 d
 *          时 H
 *          分 m
 *          秒 s
 * 
 *          2014年12月12日 12:12:12
 */
public class DateFormatDemo {
    public static void main(String[] args) throws ParseException {
        // Date -- String
        // 创建日期对象
        Date d = new Date();
        // 创建格式化对象
        // SimpleDateFormat sdf = new SimpleDateFormat();
        // 给定模式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        // public final String format(Date date)
        String s = sdf.format(d);
        System.out.println(s);


        //String -- Date
        String str = "2008-08-08 12:12:12";
        //在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dd = sdf2.parse(str);
        System.out.println(dd);
    }
}
    B:日期和字符串的转换
        a:Date -- String
            format()

        b:String -- Date
            parse()
    C:案例:
        制作了一个针对日期操作的工具类。
/**
 * 这是日期和字符串相互转换的工具类
 * 
 * @author 
 */
public class DateUtil {
    private DateUtil() {
    }

    /**
     * 这个方法的作用就是把日期转成一个字符串
     * 
     * @param d
     *            被转换的日期对象
     * @param format
     *            传递过来的要被转换的格式
     * @return 格式化后的字符串
     */
    public static String dateToString(Date d, String format) {
        // SimpleDateFormat sdf = new SimpleDateFormat(format);
        // return sdf.format(d);
        return new SimpleDateFormat(format).format(d);
    }

    /**
     * 这个方法的作用就是把一个字符串解析成一个日期对象
     * 
     * @param s
     *            被解析的字符串
     * @param format
     *            传递过来的要被转换的格式
     * @return 解析后的日期对象
     * @throws ParseException
     */
    public static Date stringToDate(String s, String format)
            throws ParseException {
        return new SimpleDateFormat(format).parse(s);
    }
}

4:Calendar(掌握)
(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。

/*
 * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
 * 
 * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
 */
public class CalendarDemo {
    public static void main(String[] args) {
        // 其日历字段已由当前日期和时间初始化:
        Calendar rightNow = Calendar.getInstance(); // 子类对象

        // 获取年
        int year = rightNow.get(Calendar.YEAR);
        // 获取月
        int month = rightNow.get(Calendar.MONTH);
        // 获取日
        int date = rightNow.get(Calendar.DATE);

        System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    }
}

/*
 * abstract class Person { public static Person getPerson() { return new
 * Student(); } }
 * 
 * class Student extends Person {
 * 
 * }
 */
(2)如何得到一个日历对象呢?
    Calendar rightNow = Calendar.getInstance();
    本质返回的是子类对象
(3)成员方法
    A:根据日历字段得到对应的值
    B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
    C:设置日历对象的年月日
/*
 * public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
 * public final void set(int year,int month,int date):设置当前日历的年月日
 */
public class CalendarDemo {
    public static void main(String[] args) {
        // 获取当前的日历时间
        Calendar c = Calendar.getInstance();

        // 获取年
        int year = c.get(Calendar.YEAR);
        // 获取月
        int month = c.get(Calendar.MONTH);
        // 获取日
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + (month + 1) + "月" + date + "日");

        // // 三年前的今天
        // c.add(Calendar.YEAR, -3);
        // // 获取年
        // year = c.get(Calendar.YEAR);
        // // 获取月
        // month = c.get(Calendar.MONTH);
        // // 获取日
        // date = c.get(Calendar.DATE);
        // System.out.println(year + "年" + (month + 1) + "月" + date + "日");

        // 5年后的10天前
        c.add(Calendar.YEAR, 5);
        c.add(Calendar.DATE, -10);
        // 获取年
        year = c.get(Calendar.YEAR);
        // 获取月
        month = c.get(Calendar.MONTH);
        // 获取日
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + (month + 1) + "月" + date + "日");
        System.out.println("--------------");

        c.set(2011, 11, 11);
        // 获取年
        year = c.get(Calendar.YEAR);
        // 获取月
        month = c.get(Calendar.MONTH);
        // 获取日
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    }
}
(4)案例:
    计算任意一年的2月份有多少天?
/*
 * 获取任意一年的二月有多少天
 * 
 * 分析:
 *      A:键盘录入任意的年份
 *      B:设置日历对象的年月日
 *          年就是A输入的数据
 *          月是2
 *          日是1
 *      C:把时间往前推一天,就是2月的最后一天
 *      D:获取这一天输出即可
 */
public class CalendarTest {
    public static void main(String[] args) {
        // 键盘录入任意的年份
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = sc.nextInt();

        // 设置日历对象的年月日
        Calendar c = Calendar.getInstance();
        c.set(year, 2, 1); // 其实是这一年的3月1日
        // 把时间往前推一天,就是2月的最后一天
        c.add(Calendar.DATE, -1);

        // 获取这一天输出即可
        System.out.println(c.get(Calendar.DATE));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值