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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容