zoukankan      html  css  js  c++  java
  • Java学习笔记-常用类

    日期相关类

    • 日期类:Date
    • 格式化日期类:SimpleDateFormat
    • 获取当前时间的总毫秒数,静态方法:System.currentTimeMillis()

    示例:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateTest01 {
        public static void main(String[] args) throws Exception{
            //日期类无参构造:Date d = new Date();
            //功能:获取系统当前的时间(精确到毫秒)
            Date nowTime = new Date();
            System.out.println(nowTime);
    
    
            //java.text包下,日期输出格式定制类:SimpleDateFormat
            //yyyy-年   MM-月  dd-日  HH-时  mm-分  ss-秒  SSS毫秒
            //注意:在日期格式中,除了y M d H m s S这些字符不能随便写,剩下的随意
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //实例方法:format(Date Time) ,日期输出格式化
            String nowTime1 = sdf.format(nowTime);
            System.out.println(nowTime1);
    
    
            //日期字符串String如何转换成Date类型?
            String time = "2008,08,08 08:08:8";
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy,MM,dd HH:mm:ss");
            //实例方法:parse(String strTime)
            Date dateTime = sdf2.parse(time);
            System.out.println(dateTime);
    
    
            //日期类有参构造:Date d = new Date(long date)
            //功能:从从起始时间过了date毫秒后的时间
            //long currentTimeMillis(): 获得 1970.1.1 00:00:00 000到当前系统时间的总毫秒数
            Date yestedayTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
            System.out.println(sdf.format(yestedayTime));
        }
    }
    

    格式化输出数值类型

    • 使用 java.text包下DecimalFormat类

    示例程序:

    import java.text.DecimalFormat;
    /*
    # 代表任意数字
    , 代表千分位
    . 代表小数点
    0 代表不够时补0
    示例:##,###.0000  表示加入千分位,小数位不够时补0
    */
    public class DecimalFormatTest01 {
        public static void main(String[] args) {
            DecimalFormat df = new DecimalFormat("##,###.0000");
            String s = df.format(1232.23);
            System.out.println(s);   //输出:1,232.2300
        }
    }
    

    高精度 (了解)

    • java.math.BigDecimal
    • BigDecimal属于精度极高的大数据,属于java对象
    • 专门用于财务软件

    示例程序

    import java.math.BigDecimal;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
            BigDecimal v1 = new BigDecimal(100);
            BigDecimal v2 = new BigDecimal(200);
            BigDecimal v3 =v1.add(v2);
            System.out.println(v3);  //输出:300
        }
    }
    

    生成随机数

    • java.util.Random

    示例程序

    import java.util.Random;
    
    public class RandomTest01 {
        public static void main(String[] args) {
            //创建随机数对象
            Random random = new Random();
    
            //随机产生一个int类型范围内的随机数
            int num1 = random.nextInt();
    
            //产生0~100之间的随机数
            int  num2 = random.nextInt(101);
            System.out.println(num2);
        }
    }
    
  • 相关阅读:
    Python列表List增删改查、拷贝
    面向对象之继承——python篇
    面向对象——python篇
    异常——python基础篇
    函数进阶
    文件操作——python基础篇
    函数(一)——python基础篇
    字典——python基础篇
    特有的字符串格式化函数format——python篇
    webdriver对应谷歌版本下载地址
  • 原文地址:https://www.cnblogs.com/zy200128/p/13020190.html
Copyright © 2011-2022 走看看