常用时间相关API(Java)
文章目录
Date
import java.util.Date;
public Date() | 创建Date对象,表示当前时间 |
---|---|
public Date(long date) | 创建Date对象,表示指定时间 |
public void setTime(long time) | 设置/修改毫秒值 |
public long getTime() | 获取时间对象的毫秒值 |
创建对象表示一段时间
Date d1=new Date();
System.out.println(d1);
一般表示程序运行的时间
创建对象表示一个限定的时间
Date d2=new Date(0L);
System.out.println(d2);
因为要求形参是一个long类型的数值,因此最好在0后面加上L表示long类型
参数为0的时候表示时间原点
setTime 修改时间
d2.setTime(1000L);
System.out.println(d2);
表示是在d2的时间下再加1000毫秒,1000毫秒表示1秒
getTime 获取当前时间的毫秒值
long time=d2.getTime();
System.out.println(time);
表示获取的时间与时间原点的时间间隔
因为刚才在setTime中将d2的时间加了1000毫秒,所以才会有1000毫秒的时间间隔
练习
需求一:打印时间原点开始一年的时间
需求二:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后
需求一:打印时间原点开始一年的时间
//创建时间对象,表示时间原点
Date date=new Date(0l);
//获取时间毫秒值
long time = date.getTime();
//时间后面加一年
time=time+1000L*60*60*24*365;
//再把计算好的时间毫秒值,再设置到date里
date.setTime(time);
//输出
System.out.println(date);
为了防止出现意外,tim在运算的时候让1000成为1000L
需求二:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后
Random r = new Random();
//创建两个任意Date对象
Date d1 = new Date(Math.abs(r.nextInt()));
System.out.println(d1);
Date d2 = new Date(Math.abs(r.nextInt()));
System.out.println(d2);
//获取时间毫秒值
long time1 = d1.getTime();
long time2 = d2.getTime();
//比较时间毫秒值
if (time1 > time2) {
System.out.println("第一个时间在后,第二个时间在前");
} else if (time1 < time2) {
System.out.println("第二个时间在后,第一个时间在前");
} else {
System.out.println("两个时间一样");
}
在创建对象的时候用到Math.abs()
,是为了让生成的随机数都是0和正整数
SimpleDateFormat
import java.text.SimpleDateFormat;
作用
格式化:把时间变成需要的格式
解析:把字符串表示的时间变成Date对象
构造方法 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个SimpleDateForma对象,使用默认格式 |
public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat对昂,然后使用指定格式 |
pattern
表示要指定的格式
常用方法 | 说明 |
---|---|
public final String format(Date date) | 格式化(日期对象->字符串) |
public Date parse(String source) | 解析(字符串->日期对象) |
格式化的是啊金形式的常用的模式对应关系
y | 年 | M | 月 | d | 日 |
---|---|---|---|---|---|
H | 时 | m | 分 | s | 秒 |
利用空参构造创建SimpleDateFormat对象,默认格式
SimpleDateFormat sdf=new SimpleDateFormat();
Date date=new Date(0l);
String str= sdf.format(date);
System.out.println(str);
默认的格式是yyyy/MM/d HH:mm
的格式,在时间和日期之间有一个空格
利用带参构造创建SimpleDateFormat对象,指定格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date=new Date(0l);
String str= sdf.format(date);
System.out.println(str);
解析时间字符串
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Teat {
public static void main(String[] args) throws ParseException {
String str="1970年01月01日 08:00:00";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = sdf.parse(str);
System.out.println(date);
}
}
注意:创建对象的格式必须要跟字符串的格式一样,否则无法解析
Calendar
import java.util.Calendar;
Calendar代表了系统当前时间的日历对象,可以单独修改、获取时间中的年、月、日
细节:Calendar是一个抽象类,不能直接创建对象
获取Calendar日历类对象的方法
方法类 | 说明 |
---|---|
public static Calendar getInstance() | 获取当前时间的日历对象 |
Calender方法
方法名 | 说明 |
---|---|
public final Date getTime() | 获取日期对象 |
public final setTime(Date date) | 给日历设置日期对象 |
public long getTimeInMillis() | 那到时间毫秒值 |
public void setTimeInMilllis(long millis) | 给日历设置时间毫秒值 |
public int get(int field) | 取日历中的某个字段信息 |
public void set(int field,int value) | 修改日历的某个字段的信息 |
public void add(int field,int amount) | 为某个字段增加/减少指定的值 |
获取日历对象
Calendar calendar = Calendar.getInstance();
结果
java.util.GregorianCalendar[time=1678278984894,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=“Asia/Shanghai”,offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=67,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=36,SECOND=24,MILLISECOND=894,ZONE_OFFSET=28800000,DST_OFFSET=0]
细节
Calendar是一个抽象类,不能直接new,而是通过静态方法获取到子类对象
月份的范围是0到11获取出来是0表示1月
星期日表示一周中的第一天
1(星期日) 2(星期一) 3(星期二) 4(星期三) 5(星期四)6(星期五) 7(星期六)
底层逻辑
会根据系统的不同时区来获取不同的日历对象,默认表示当前时间
会把时间中的纪元,年,月,日等信息都放到一个数组中
ZoneId时区
方法名 | 说明 |
---|---|
static Set getAvailableZoneIds() | 获取java中支持的所有时区 |
static ZoneId systemDefault() | 获取系统默认时区 |
static ZoneId of(String zoneId) | 获取一个指定时区 |
static Set getAvailableZoneIds() 获取所有的时区名称
import java.time.ZoneId;
import java.time.ZoneId;
import java.util.Set;
public class Teat {
public static void main(String[] args) {
//获取所有的时区
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds);
}
}
集合的长度是600,表示java中含有600个时区
static ZoneId systemDefault() 获取当前系统的默认时区
import java.time.ZoneId;
import java.util.Set;
public class Teat {
public static void main(String[] args) {
//获取当前系统默认的时区
ZoneId systemDefault = ZoneId.systemDefault();
System.out.println(systemDefault);
}
}
static ZoneId of(String zoneId) 获取一个指定时区
ZoneId of = ZoneId.of("Asia/Pontianak");
System.out.println(of);
Instant时间戳
import java.time.Instant;
方法名 | 说明 |
---|---|
static Instant now() | 获取当前时间的Instant对象(标准时间) |
static Instant ofXxxx(long epocchMilli) | 根据(秒、毫秒、纳秒)获取Instant对象 |
ZonedDateTime atZone(ZoneId zone) | 指定时区 |
boolean isXxx(Instant millisToSubtract) | 判断系列的方法 |
Instant minusXxx(long millisToSubtract) | 减少时间系列的方法 |
Instant plusXxx(long millisToSubtract) | 增加时间系列的方法 |
Instant now() 获取当前时间的Instant对象(标准时间)
Instant now = Instant.now();
System.out.println(now);
结果表示标准时间,跟时区时间相比是不一样的
static Instant ofXxxx(long epocchMilli) 根据(秒、毫秒、纳秒)获取Instant对象
从上往下分别表示毫秒 秒 纳秒
毫秒
Instant ofEpochMilli = Instant.ofEpochMilli(0L);
System.out.println(ofEpochMilli);
输出结果表示在时间原点上加了0毫秒
秒
Instant ofEpochSecond = Instant.ofEpochSecond(1L);
System.out.println(ofEpochSecond);
输出结果表示在时间原点上加了1秒
纳秒
Instant ofEpochSecond = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(ofEpochSecond);
输出结果表示在时间原点上加了一秒再加1000000000纳秒
ZonedDateTime atZone(ZoneId zone) 指定时区
非静态的,要用对象调用
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);
输出结果表示的是当前的时间
boolean isXxx(Instant millisToSubtract) 用于对时间的判断
Instant instant1 = Instant.ofEpochSecond(0L);
Instant instant2 = Instant.ofEpochSecond(10L);
System.out.println(instant1);
System.out.println(instant2);
boolean result1 = instant1.isAfter(instant2);
System.out.println(result1);
boolean result2 = instant1.isBefore(instant2);
System.out.println(result2);
输出结果
isAfter判断调用者代表的时间是否在参数表示时间的后面
isBefore判断调用者代表的时间是否在参数表示时间的前面
Instant minusXxx(long millisToSubtract) 减少时间系列的方法
Instant plusXxx(long millisToSubtract) 增加时间系列的对象
Instant instant1 = Instant.ofEpochSecond(0L);
System.out.println(instant1);
//minusSeconds
Instant instant3 = instant1.minusSeconds(1L);
System.out.println(instant3);
//plusSeconds
Instant instant4 = instant1.plusSeconds(1L);
System.out.println(instant4);
在Instant中,时间是没法直接增加删除的,所以会重新创建一个对象
时间的增加和减少是可以用秒。毫秒。纳秒来表示
·