Java object-oriented (IX)

Random commonly used class of

Random class java.util package is located, it is mainly used for generating pseudo-random number

Random class seeds origins as the random number algorithm, it calculates and generates a pseudo-random number, which is independent of the random number generation section

When creating Random instance, if no number of seeds, will be to the current time as a seed number to generate a pseudo-random number is calculated

Random seed with identical, at the same frequency and generates a pseudo random number is identical

Constructor

// 创建一个新的随机数生成器。 
Random() 

// 使用单个 long 种子创建一个新的随机数生成器。 
Random(long seed) 

// 相同种子生成的随机数是一样的

Common method

// 返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值
int nextInt() 

// 返回一个伪随机数,它是取自此随机数生成器序列的、在 [0,n) 之间均匀分布的 int 值。
int nextInt(int n) 

// 返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值 
double nextDouble() 

// 返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值
float nextFloat() 

// 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值
boolean nextBoolean() 

// 生成随机字节并将其置于用户提供的 byte 数组中
void nextBytes(byte[] bytes) 

// 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值   
long nextLong() 

// 使用单个 long 种子设置此随机数生成器的种子
void setSeed(long seed) 

Example 1:

Random r = new Random();
System.out.println(r.nextDouble());
System.out.println(r.nextBoolean());
System.out.println("-> " + r.nextInt());

System.out.println("---------------");

// 相同的种子,生成的随机数是一样
Random r2 = new Random(10);
Random r3 = new Random(10);
System.out.println("-> " + r2.nextInt()); // -1157793070
System.out.println("-> " + r3.nextInt()); // -1157793070

// 生成34到179之间的随机数 : 34 + [0 145)
System.out.println(r.nextInt(145) + 34);

Examples 2: UUID

// UUID:通用唯一识别符
// 在一台机器 上生成 的数字
// 当前的时间,跟当前电脑网卡 生成一段字符
String uuid = UUID.randomUUID().toString();
System.out.println(uuid);

Example 3: Verification Code

// 生成验证码
// 5位的随机数 UUID生成的是16进制
String res = UUID.randomUUID().toString();
System.out.println(res);
res = res.substring(0, 5);
System.out.println(res);
System.out.println("--------------------");

String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
str = str + str.toLowerCase();
str += "0123456789";
System.out.println(str);

System.out.println(str.length());
// 从所有的字符当中随机生成5个出来
// 随机取5个出来
// 每取出一个结果,在原来的基础 上面进行拼接
StringBuilder sb = new StringBuilder(5);
for (int i = 0; i < 5; i++) {
    // 角标要随机的值 (0 62:字符串的长度)
    int index = new Random().nextInt(str.length());
    char ch = str.charAt(index);
    sb.append(ch);
}

System.out.println(sb);

Date commonly used class of

Before we define the age of employees and the date of entry int and String is defined.

class Employee {
    int age;
    String hireDate;
}

So with each passing year, must amend the age; or to calculate the employee's length of service is not convenient. java which specializes in providing a date type.

Constructor

// 使用当前日期和时间来初始化对象(精确到毫秒)。
Date()

// 第二个构造函数接收一个参数,该参数是从1970年1月1日 00:00:00 起的毫秒数。
Date(long date) 

eg get the current date and time

// 创建 一个日期对象
Date date = new Date();
System.out.println(date);


// 获取当前时间的毫秒数
long curTime = System.currentTimeMillis();
// 把一个毫秒值 转成日期类型
Date date2 = new Date(curTime);
System.out.println(date2);

In fact, more than two two pieces of code are the same, look Data () source, the essence is to use the number of milliseconds the current time system to create a Date object

public Date() {
    this(System.currentTimeMillis());
}

Date comparison

// 测试此日期是否在指定日期之后
boolean after(Date when) 

// 测试此日期是否在指定日期之前
boolean before(Date when) 

// 比较两个日期的顺序
int compareTo(Date anotherDate) 

// 比较两个日期的相等性
boolean equals(Object obj) 

eg

// 创建 一个日期对象
Date date = new Date(1564387266239L);       // 毫秒数大 时间迟
Date date2 = new Date(1564211606445L);      // 毫秒数小 时间早
System.out.println(date);                   // Mon Jul 29 16:01:06 GMT+08:00 2019
System.out.println(date2);                  // Sat Jul 27 15:13:26 GMT+08:00 2019

System.out.println(date.before(date2));     // false
System.out.println(date.after(date2));      // true
System.out.println(date.compareTo(date2));  // 1
System.out.println(date.equals(date2));     // false

Get and set time value in milliseconds: date and conversion milliseconds

long getTime() 
void setTime(long time)  

eg

// 创建 一个日期对象
Date date = new Date();
Date date2 = new Date();

long time = date.getTime();
System.out.println(time);

date2.setTime(1564387266239L);
// 输出的字符串不符合中文习惯
System.out.println(date2);
// toLocaleString 已经过时了,不建议使用,这里暂时使用
System.out.println(date2.toLocaleString());     

Date Format

Creation date formatting objects:

// 获取日期和时间使用 SHORT 风格的默认日期/时间格式器
static DateFormat getInstance()  

// 获取日期和时间使用 默认(DEFAULT) 风格的日期/时间格式器
static DateFormat getDateTimeInstance()  

// 获取日期和时间使用 给定日期和时间格式化风格的日期/时间格式器
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)

// 获取使用 默认(DEFAULT) 风格的日期格式器
static DateFormat getDateInstance() 

// 获取使用 给定格式化风格的日期格式器
static DateFormat getDateInstance(int style) 

// 获取使用 默认(DEFAULT) 风格的时间格式器
static DateFormat getTimeInstance() 

// 获取使用 给定格式化风格的时间格式器
static DateFormat getTimeInstance(int style) 

eg

Date date = new Date();
System.out.println(date);
System.out.println(date.toLocaleString());
System.out.println("------------日期格式化------------");

// 获取为日期和时间使用 SHORT 风格的默认日期/时间格式器
DateFormat df1 = DateFormat.getInstance();
// 相当于 : DateFormat df1 = DateFormat.getDateTimeInstance
// (DateFormat.SHORT, DateFormat.SHORT);

// 对指定的日期进行格式化: Date -> String
String time = df1.format(date);
System.out.println("SHORT 风格:   " + time);

// 获取为日期和时间使用 默认(DEFAULT) 风格的日期/时间格式器
DateFormat df2 = DateFormat.getDateTimeInstance();
// 相当于 : DateFormat df2 = DateFormat.getDateTimeInstance
// (DateFormat.DEFAULT, DateFormat.DEFAULT);
System.out.println("DEFAULT 风格: " + df2.format(date));

DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
System.out.println("MEDIUM 风格:  " + df3.format(date));

DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
System.out.println("LONG 风格:    " + df4.format(date));

DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
System.out.println("FULL 风格:    " + df5.format(date));

Output:

Mon Jul 29 17:16:05 GMT+08:00 2019
2019-7-29 17:16:05
------------日期格式化------------
SHORT 风格:   19-7-29 下午5:16
DEFAULT 风格: 2019-7-29 17:16:05
MEDIUM 风格:  2019-7-29 17:16:05
LONG 风格:    2019年7月29日 下午05时16分05秒
FULL 风格:    2019年7月29日 星期一 下午05时16分05秒 GMT+08:00

Date and String conversion

(1) Date -> String: format formatted

Date date = new Date();
        
DateFormat df = DateFormat.getInstance();
String format = df.format(date);
System.out.println(format);

(2) String -> Date: parse parse

DateFormat df = DateFormat.getInstance();
        
String source = "19-7-29 下午5:23";
try {
    Date parse = df.parse(source);
    System.out.println(parse);
} catch (ParseException e) {
    System.out.println("日期字符串格式错误!");
    e.printStackTrace();
}

Guess you like

Origin www.cnblogs.com/xzh0717/p/11265283.html