计算世界时间的主要标准有:
- UTC(Coordinated Universal Time)国际协调时间
- GMT(Greenwich Mean Time)格林威治标准时间
- CST(Central Standard Time)
纪元(epoch):UTC 时间1970年1月1日 00:00:00
1. System 类
System 类代表系统级的属性和控制方法,该类的构造器是 private 的,内部成员及方法都是 static 的,所以可以直接调用in、out、err三个成员变量,分别是标准输入流、标准输出流、标准错误输出流
System类提供的public static long current Time Millis,用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差,这个毫秒数称为时间戳,此方法适于计算时间差
// 获取毫秒值
System.currentTimeMillis();
// 获取纳秒值
System.nanoTime();
exit(int status) 0代表正常退出,非0代表异常退出
gc() 垃圾回收器,请求系统进行垃圾回收
getProperty(String key)获取系统属性

2. Date 类
用与纪元时间点的一个毫秒数差值(可正可负)来表示
java.util.Date 类
Date date1 = new Date();
System.out.println(date1.toString());//Sun Nov 21 14:07:54 CST 2021
System.out.println(date1.getTime());//1637474992658 时间戳
java.sql.Date(java.util.Date的子类,数据库中的date类型交互)
Date date = new Date(1637474992658L);//2021-11-21 加L是为了Long型
System.out.println(date);
//java.util.Date转化为java.sql.Date
java.util.Date date1 = new java.util.Date();
java.sql.Date date2 = new java.sql.Date(date1.getTime());
System.out.println(date2);
3. SimpleDateFormat 日期格式化类
月份:M
分钟:m
24小时制:H
12小时制:h
//格式化日期--->字符串
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
System.out.println(simpleDateFormat.format(date));//21-11-21 下午5:14
//字符串--->日期
String str = "21-11-21 下午5:14";//这是默认构造器,条件局限性较大
System.out.println(simpleDateFormat.parse(str));
//自定义构造器进行
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");//2021-11-21 05:22:59 下午
System.out.println(sdf.format(date));
//与数据库进行交互,将date字符串转为sql.Date类型
String date = "2021-11-21";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = simpleDateFormat.parse(date);
java.sql.Date date2 = new java.sql.Date(date1.getTime());
System.out.println(date2.getClass());//class java.sql.Date
4. Calendar 类
Calendar抽象基类,一般不用
- 可变性:对于日期时间来说不应该可变
- 偏移性:Date中年份是从1900年开始。月份从0开始
- 格式化:只对Date有用,Calendar不行
- 线程不安全
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//获取这个月的第几天
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//获取这一年的第几天
calendar.set(Calendar.DAY_OF_MONTH,22);//修改这个月的天数是第几天
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//显示的就是修改后的天数
calendar.add(Calendar.DAY_OF_MONTH,3);//往上加几天,减就是负数
System.out.println(calendar.getTime());//日历类获取Date数据
5. Joda-Time
Java8引入的新时间API Joda-Time
//获取当前的时间属性
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getHour());
//设置当前的时间属性
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 11, 21, 21, 30, 15);
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(20);//不影响其它位置的数据(不可变性)
System.out.println(localDateTime2);
//获取毫秒值
Instant instant = Instant.now();
System.out.println(instant);//2021-11-22T06:28:07.264Z 时间差8小时这里指本初子午线时间,因为咱们在东八区
//处理
System.out.println(instant.atOffset(ZoneOffset.ofHours(8)));//2021-11-22T14:31:24.317+08:00
//格式化解析日期或时间
//预定义格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String str = dateTimeFormatter.format(localDateTime);//2021-11-22T17:27:08.297
System.out.println(str);
//Long格式
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(dateTimeFormatter1.format(localDateTime));//2021年11月22日 下午05时27分08秒
//自定义格式
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(dateTimeFormatter2.format(localDateTime));//2021-11-22 05:32:31
//解析
TemporalAccessor temporalAccessor = dateTimeFormatter2.parse("2021-11-22 05:32:31");
System.out.println(temporalAccessor);
// 获取今年的天数
int days = LocalDate.now().lengthOfYear();
System.out.println(days);
// 获取某年的天数
int daysOfYear = LocalDate.of(2017, 12, 31).lengthOfYear();
System.out.println(daysOfYear);
Comparable比较器
//Java中的对象平时只能进行==或!=比较,不能进行 > <
//但是在开发场景中需要对多个对象进行排序,比较对象的大小
//实现两个接口中的任何一个Comparable(自然排序)或Comparator(定制排序)
//String、包装类实现了Comparable接口,重写了compareTo()方法,比较两个对象大小
String[] strings = {"D", "C", "E", "A", "F", "B"};
Arrays.sort(strings);
System.out.println(Arrays.toString(strings));//[A, B, C, D, E, F]
package xyz.tylt.learn.test;
import java.util.Arrays;
public class Student implements Comparable{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//指明按照什么方式进行排序
@Override
public int compareTo(Object o) {
if (o instanceof Student){
Student student =(Student) o;//判断对象后进行强转
if (this.age>student.age){
return 1;
}
else if (this.age==student.age){
return 0;
}
else {
return -1;
}
}
else {
throw new RuntimeException("传入数据异常");
}
}
public static void main(String[] args) {
//对于自定义类来说,实现排序需要实现Comparable接口,指明如何排序
//重写了compareTo(obj)规则
//如果当前对象this大于形参对象obj,返回正整数
//如果当前对象this小于形参对象obj,返回负整数
//如果当前对象this等于形参对象obj,返回零
Student [] students = new Student[4];
students[0] = new Student("aaa",13);
students[1] = new Student("ccc",15);
students[2] = new Student("bbb",18);
students[3] = new Student("ddd",22);
Arrays.sort(students);
System.out.println(Arrays.toString(students));//[Student{name='aaa', age=13}, Student{name='ccc', age=15}, Student{name='bbb', age=18}, Student{name='ddd', age=22}]
}
}

被折叠的 条评论
为什么被折叠?



