时间戳和正则表达式

时间戳和正则表达式

时间戳

一、定义

时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。 它的提出主要是为用户提供一份电子证据, 以证明用户的某些数据的产生时间。

二、精度

  1. 精确到秒
  2. 精确到毫秒

三、时间戳和时间的转换

Date————>离不开SimpleDateFormat(格式化时间)/或者此类下 setTime(); 或者 getTime();

​ 若只获取年月日:LocalDate————>离不开DateFormatter(格式化日期)

四、生成时间戳

法一:

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

public class time{

    public static void main(String[] args){
        Date date = new Date();//为系统当前时间
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";//设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
    
}

运行结果:

在这里插入图片描述

#注:定义 SimpleDateFormat 时 new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); 里面字符串头尾不能有空格,有空格那是用转换时对应的时间空格也要有空格(两者是对应的)

在这里插入图片描述

法二:

 public static void main(String[] args){
        long totalSeconds = System.currentTimeMillis() / 1000;
        //获取unix时间戳至今的秒数

        long currentSeconds = totalSeconds % 60; //获取当前秒数

        long currentMinutes = totalSeconds / 60 % 60; //获取当前分钟数

        long currentHours = totalSeconds / 3600 % 24 + 8;
        //获取当前小时数  北京属于东八时区,时间要在格林尼治时间(GMT)基础上+8
        
        System.out.println(currentHours);
        System.out.println(currentMinutes);
        System.out.println(currentSeconds);

    }

运行结果:

在这里插入图片描述

#注:慎用 System.currentTimeMillis() 。同样的代码循环执行数次,分析每一次的执行时间,发现一大部分执行时间为小于1毫秒,但其间也发现有相当一部分的执行时间有非常大的跳跃,而且时间都近似16毫秒左右。这个1毫秒和16毫秒结果,以计算机的运行速度看,差距是惊人的。因为这个方法调用了个 native方法,获取的时间精度会依赖于操作系统的实现机制

正则表达式(regular expression)——了解

一、概念

是一种字符串匹配的模式

二、作用

  1. 检查一个字符串是否含有某种子串
  2. 替换匹配的子串
  3. 提取某个字符串中匹配的子串
  4. 某些界面看到的让输入用户名密码注册登录的界面内部语法就用到了正则表达式

三、常用正则表达式

在这里插入图片描述

原文地址:https://www.cnblogs.com/ryyy/p/14228113.html