(JAVA):常见的时间方法、日期方法、随机数方法,基础性的知识,必须要知晓。

目录

一、卍卐 系统当前时间

1、卍 currentTimeMillis() 方法:

2、卐 NanoTime() 方法:

3、卐 注意: 

 二、☠☠日期操作类 

(1)☠Date类:

(2)☠Calendar类:

(3)☠DateFormat类:

(4)☠代码示例: 

三、✉✉随机数 

(1)✉ Random类:

(2)✉代码示例:


 

一、卍​​​​​​​卐 系统当前时间

1、卍 currentTimeMillis() 方法:

(1)功能:

        获取系统当前时间,单位:毫秒

(2)代码:

public class time {
    public static void main(String[] args) {

        long start = System.currentTimeMillis();//获取系统当前时间

        for (int i = 0; i < 10000; i++) {
            String str = "" + i;
        }

        long end = System.currentTimeMillis();//获取系统当前时间

        System.out.println("程序耗时:" + (end - start) + "毫秒!");

    }
}

2、卐 NanoTime() 方法:

 (1)功能:

        获取系统当前时间,单位:纳秒

(2)代码:

public class time {
    public static void main(String[] args) {

        long start = System.nanoTime();//获取系统当前时间

        for (int i = 0; i < 10000; i++) {
            String str = "" + i;
        }

        long end = System.nanoTime();//获取系统当前时间

        System.out.println("程序耗时:" + (end - start) + "纳秒!");
    }
}

3、卐 注意: 

        一般使用currentTimeMillis()就可以满足需要了,这是当前时间对元年的毫秒时间偏移量,是一个long类型的数值。但当时间需要更加精确的时候,我们可以使用纳秒来计算即nanoTime(),纳秒计算同样是对元年的纳秒时间偏移量,它会返回一个long类型的数值。


 二、☠☠日期操作类 

(1)Date类:

        Date类对象用来表示时间和日期,该类提供了一系列操作时间和日期各组成部分的方法。Date类中使用最多的是获取系统当前的日期和时间。如:Date date = new Date();这句代码是使用当前系统时间创建日期对象。

(2)Calendar类:

        Calendar类也是用来操作日期和时间的类,它可以看作是Date类的加强版。Calendar提供了一组方法,允许把一个以毫秒为单位的时间转换成年、月、日、小时、分、秒。可以把Calendar当做是万年历,默认显示的是当前时间,当然也可以查看其他时间。

         Calendar类是抽象类,可以通过静态方法getInstance()获得Calender类的对象,其实这个获得对象的是它的子类的对象。

        ▶▶该类提供的一些方法,如下:

        ▷  int get(int field):返回给定日历字段的值。

        ▷  YEAR:指示年

        ▷  MONTH:指示月

        ▷  DAY_OF_MONTH:指示一个月中的某天

        ▷  DAY_OF_WEEK:指示一个星期中的某天

(3)DateFormat类:

        此类是格式化日期时间的类,它在Java.text 包下,是一个抽象类,提供了多种格式化和解析时间的方法。格式化是指将日期转换为文本,解析是指将文本转化成日期格式。使用比较多的是它的子类SimpleDateFormat,SimpleDateFormat类是一个以与语言环境有关的方式来格式化和解析日期的具体类,如:“yyyy-MM-dd HH:mm:ss” 就是指定的一种日期和时间的格式。

(4)代码示例: 

        1、Calendar类的示例:

public class case1 {
    public static void main(String[] args) {

        Calendar time = Calendar.getInstance();

        System.out.print("今天是" + time.get(Calendar.YEAR) + "年"); //输出年
        System.out.print( (time.get(Calendar.MONTH)+1) + "月");     //输出月
        System.out.print(time.get(Calendar.DAY_OF_MONTH) + "日");   //输出日


        System.out.println();
        //输出星期
        System.out.println("今天是星期" + (time.get(Calendar.DAY_OF_WEEK)-1));
    }
}

         2、DateFormat类的示例:

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

public class csae2 {
    public static void main(String[] args) {

        Date date = new Date(); //获取当前时间

        SimpleDateFormat mat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化

        System.out.println("当前时间为:" + mat.format(date)); //输出时间

    }
}

三、✉✉随机数 

(1)✉ Random类:

        1、常见语法:

        int nextInt()

        ▶ int nextInt(int n)

        ▷ 前者返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值

        ▷ 后者返回下一个伪随机数,它是取自此随机数生成器序列的、在0(包括)和指定值              n(不包括)之间均匀分布的 int 值

(2)✉代码示例:

import java.util.Random;

public class case1 {
    public static void main(String[] args) {

        //创建一个Random对象
        Random random = new Random();

        //生成随机数
        for (int i = 0; i < 10; i++) {

            int num = random.nextInt(10);

            System.out.println("第" + (i + 1) + "个随机数是" + num);
        }

    }
}
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇

)">
下一篇>>