java(九)Math类和Random类,日期类和日期格式化

一、Math类操作数字的,三角函数,最大值,最小值等等

二、Randon类是操作随机数。具体内容看APi

三、日期类获取毫秒数(Date)类

Date date =new Date()
long time =date.getTime()
// 或者   System.currentTimeMillis();
System.out.println(time);

四、操作日期的获取和赋值、添加、减少等等。(Calendar)类

 Calendar calendar = Calendar.getInstance();
 int year = calendar.get(Calendar.YEAR); // 获取年份
 System.out.println(year); // 2017

// 添加add()
calendar.add(Calendar.YEAR,100); // 2117
// 赋值
Date date =new Date();
calendar.set(date);

五、日期格式化和字符串解析(SimpleDateFormat)

  • 格式化
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
 String name = sf.format(new Date());
 System.out.println("name = " + name); // 2017-9-26
  • 字符串解析
   // 格式化日期 DateFormat 下的SimpleDateFormat类 parse()转化为日期; format()格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 定义一个日期转化为毫秒数
        Date time1 = null;
        try {
            time1 = dateFormat.parse("2017-10-10");
        } catch (Exception e) {

        }
        calendar.setTime(time1);
        System.out.println(calendar.get(Calendar.MONTH)); // 9

六、详情操作

http://blog.csdn.net/jediael_lu/article/details/43852043

你可能感兴趣的:(java(九)Math类和Random类,日期类和日期格式化)