Date时间类
方法名 说明 public Date() 创建Date对象,表示当前时间 public Date(long date) 创建Date对象,表示指定时间 public void setTime(long time) 设置/修改毫秒值 public long getTime() 获取时间对象的毫秒值
public static void main ( String [ ] args) {
Date d = new Date ( ) ;
System . out. println ( d) ;
System . out. println ( "---------------------------------" ) ;
Date d2 = new Date ( 0L ) ;
System . out. println ( d2) ;
System . out. println ( "---------------------------------" ) ;
d2. setTime ( 1000L ) ;
System . out. println ( d2) ;
System . out. println ( "---------------------------------" ) ;
long time = d2. getTime ( ) ;
System . out. println ( time) ;
}
时间计算
需求1:打印时间原点开始一年之后的时间
public static void main ( String [ ] args) {
Date d1 = new Date ( 0 ) ;
long time = d1. getTime ( ) ;
time = time + 1000L * 60 * 60 * 24 * 365 ;
d1. setTime ( time) ;
System . out. println ( d1) ;
}
需求2:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后
public static void main ( String [ ] args) {
Random r = new Random ( ) ;
Date d1 = new Date ( Math . abs ( r. nextInt ( ) ) ) ;
Date d2 = new Date ( Math . abs ( r. nextInt ( ) ) ) ;
long time1 = d1. getTime ( ) ;
long time2 = d2. getTime ( ) ;
System . out. println ( d1) ;
System . out. println ( d2) ;
if ( time1 > time2) {
System . out. println ( "第一个时间在后面,第二个时间在前面" ) ;
} else if ( time1 < time2) {
System . out. println ( "第一个时间在前面,第二个时间在后面" ) ;
} else {
System . out. println ( "表示两个时间一样" ) ;
}
}
SimpleDateFormat类
构造方法 说明 public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认格式 public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat,使用指定的格式
public static void main ( String [ ] args) {
SimpleDateFormat sdf = new SimpleDateFormat ( ) ;
Date d = new Date ( 0L ) ;
String str = sdf. format ( d) ;
System . out. println ( str) ;
System . out. println ( "----------------------------------" ) ;
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss EE" ) ;
String str2 = sdf2. format ( d) ;
System . out. println ( str2) ;
}
public static void main ( String [ ] args) throws ParseException {
String str = "2023-11-11 11:11:11" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = sdf. parse ( str) ;
System . out. println ( date. getTime ( ) ) ;
}
常用方法 说明 public final String format(Date date) 格式化(日期对象 -> 字符串) public Date parse(String source) 解析(字符串 -> 日期对象)
练习
假设,出生年月日为:2000-11-11 请用字符串表示这个数据,并将其转换为:2000年11月11日
public static void main ( String [ ] args) throws ParseException {
String str = "2000-11-11" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date = sdf. parse ( str) ;
SimpleDateFormat sdf1 = new SimpleDateFormat ( "yyyy年MM月dd日" ) ;
String result = sdf1. format ( date) ;
System . out. println ( result) ;
}
需求: 秒杀活动:2023年11月11日 0:0:0 (毫秒值) 开始时间:2023年11月11日 0:10:0(毫秒值)
小贾下单并付款的时间为:2023年11月11日 0:01:00 小皮下单并付款的时间为:2023年11月11日 0:11:0 用代码说明这两位同学有没有参加上秒杀活动
public static void main ( String [ ] args) throws ParseException {
String startStr = "2023年11月11日 0:0:0" ;
String endStr = "2023年11月11日 0:10:0" ;
String orderStr = "2023年11月11日 0:01:00" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
Date startDate = sdf. parse ( startStr) ;
Date endDate = sdf. parse ( endStr) ;
Date orderDate = sdf. parse ( orderStr) ;
long startTime = startDate. getTime ( ) ;
long endTime = endDate. getTime ( ) ;
long orderTime = orderDate. getTime ( ) ;
if ( orderTime >= startTime && orderTime <= endTime) {
System . out. println ( "参加成功" ) ;
} else {
System . out. println ( "参加失败" ) ;
}
}
Calendar
方法名 说明 public static Calendar getInstance() 获取当前时间的日历对象 public final Date getTime() 获取日期对象 public final setTime(Date date) 给日历设置日期对象 public long getTimeInMillis() 拿到时间毫秒值 public void setTimeInMillis(long millis) 给日历设置时间毫秒值 public int get(int field) 取日历中的某个字段信息 public void set(int field,int value) 修改日历的某个字段信息 public void add(int field,int amount) 为某个字段增加/减少指定的值
public static void main ( String [ ] args) {
Calendar c = Calendar . getInstance ( ) ;
Date d = new Date ( 0L ) ;
c. setTime ( d) ;
c. set ( Calendar . YEAR , 2023 ) ;
c. set ( Calendar . MONTH , 8 ) ;
c. set ( Calendar . DAY_OF_MONTH , 10 ) ;
c. add ( Calendar . MONTH , 1 ) ;
int year = c. get ( Calendar . YEAR ) ;
int month = c. get ( Calendar . MONTH ) + 1 ;
int date = c. get ( Calendar . DAY_OF_MONTH ) ;
int week = c. get ( Calendar . DAY_OF_WEEK ) ;
System . out. println ( year + ", " + month + ", " + date + ", " + getWeek ( week) ) ;
}
public static String getWeek ( int index) {
String [ ] arr = { "" , "星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" } ;
return arr[ index] ;
}
Zoneld时区
方法名 说明 static Set< String > getAvailableZoneIds() 获取Java中支持的所有时区 static ZoneId systemDefault() 获取系统默认时区 static ZoneId of(String zoneId) 获取一个指定时区
public static void main ( String [ ] args) {
Set < String > zoneIds = ZoneId . getAvailableZoneIds ( ) ;
System . out. println ( zoneIds. size ( ) ) ;
System . out. println ( zoneIds) ;
ZoneId zoneId = ZoneId . systemDefault ( ) ;
System . out. println ( zoneId) ;
ZoneId id = ZoneId . of ( "America/Marigot" ) ;
System . out. println ( id) ;
}
Instant时间戳
方法名 说明 static Instant now() 获取当前时间的Instant对象(标准时间) static Instant ofXxxx(long epochMilli) 根据(秒/毫秒/纳秒)获取Instant对象 ZonedDateTime atZone(ZoneId zone) 指定时区 boolean isXxx(Instant otherInstant) 判断系列的方法 Instant minusXxx(long millisToSubtract) 减少时间系列的方法 Instant plusXxx(long millisToSubtract) 增加时间系列的方法
public static void main ( String [ ] args) {
Instant now = Instant . now ( ) ;
System . out. println ( now) ;
System . out. println ( "----------------------------------" ) ;
Instant instant = Instant . ofEpochMilli ( 0L ) ;
System . out. println ( instant) ;
System . out. println ( "----------------------------------" ) ;
Instant instant1 = Instant . ofEpochSecond ( 1L ) ;
System . out. println ( instant1) ;
System . out. println ( "----------------------------------" ) ;
Instant instant2 = Instant . ofEpochSecond ( 1L , 1000000000L ) ;
System . out. println ( instant2) ;
System . out. println ( "----------------------------------" ) ;
ZonedDateTime time = Instant . now ( ) . atZone ( ZoneId . of ( "Asia/Shanghai" ) ) ;
System . out. println ( time) ;
System . out. println ( "----------------------------------" ) ;
Instant instant3 = Instant . ofEpochMilli ( 0L ) ;
Instant instant4 = Instant . ofEpochMilli ( 1000L ) ;
boolean result1 = instant3. isBefore ( instant4) ;
System . out. println ( result1) ;
System . out. println ( "----------------------------------" ) ;
boolean result2 = instant3. isAfter ( instant4) ;
System . out. println ( result2) ;
System . out. println ( "----------------------------------" ) ;
Instant instant5 = Instant . ofEpochMilli ( 3000L ) ;
System . out. println ( instant5) ;
Instant instant6 = instant5. minusSeconds ( 1 ) ;
System . out. println ( instant6) ;
System . out. println ( "----------------------------------" ) ;
}
ZoneDateTime带时区的时间
方法名 说明 static ZonedDateTime now() 获取当前时间的ZonedDateTime对象 static ZonedDateTime ofXxx() 获取指定时间的ZonedDateTime对象 ZonedDateTime withXxx(时间) 修改时间系列的方法 ZonedDateTime minusXxx(时间) 减少时间系列的方法 ZonedDateTime plusXxx(时间) 增加时间系列的方法
public static void main ( String [ ] args) {
ZonedDateTime now = ZonedDateTime . now ( ) ;
System . out. println ( now) ;
System . out. println ( "-----------------------------------------" ) ;
ZonedDateTime time1 = ZonedDateTime . of ( 2023 , 10 , 1 , 11 , 12 , 12 , 0 , ZoneId . of ( "Asia/Shanghai" ) ) ;
System . out. println ( time1) ;
System . out. println ( "-----------------------------------------" ) ;
ZonedDateTime time3 = time1. withYear ( 2000 ) ;
System . out. println ( time3) ;
System . out. println ( "-----------------------------------------" ) ;
ZonedDateTime time4 = time3. minusYears ( 1 ) ;
System . out. println ( time4) ;
System . out. println ( "-----------------------------------------" ) ;
ZonedDateTime time5 = time4. plusYears ( 10 ) ;
System . out. println ( time5) ;
}
DateTimeFormatter
方法名 说明 static DateTimeFormatter ofPattern(格式) 获取格式对象 String format(时间对象) 按照指定方式格式化
public static void main ( String [ ] args) {
ZonedDateTime time = Instant . now ( ) . atZone ( ZoneId . of ( "Asia/Shanghai" ) ) ;
DateTimeFormatter dtf1 = DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss EE a" ) ;
System . out. println ( dtf1. format ( time) ) ;
}
LocalDate、LocalTime、LocalDateTime
方法名 说明 static XXX now() 获取当前时间的对象 static XXX of() 获取指定时间的对象 get开头的方法 获取日历中的年、月、日、时、分、秒等信息 isBefore,isAfeter 比较两个LocalDate with开头的 修改时间系列的方法 minus开头的 减少时间系列的方法 plus开头的 增加时间系列的方法 public LocalDate toLocalDate() LocalDateTime转换成一个LocalDate对象 public LocalTime toLocalTime() LocalDateTime转换成一个LocalTime对象
public static void main ( String [ ] args) {
LocalDate nowDate = LocalDate . now ( ) ;
System . out. println ( "今天的日期:" + nowDate) ;
System . out. println ( "----------------------------------" ) ;
LocalDate ldDate = LocalDate . of ( 2023 , 1 , 1 ) ;
System . out. println ( "指定日期:" + ldDate) ;
System . out. println ( "----------------------------------" ) ;
int year = ldDate. getYear ( ) ;
System . out. println ( "year:" + year) ;
System . out. println ( "----------------------------------" ) ;
Month m = ldDate. getMonth ( ) ;
System . out. println ( m) ;
System . out. println ( m. getValue ( ) ) ;
System . out. println ( "----------------------------------" ) ;
int month = ldDate. getMonthValue ( ) ;
System . out. println ( "month:" + month) ;
System . out. println ( "----------------------------------" ) ;
int day = ldDate. getDayOfMonth ( ) ;
System . out. println ( "day:" + day) ;
System . out. println ( "----------------------------------" ) ;
int dayOfYear = ldDate. getDayOfYear ( ) ;
System . out. println ( "dayOfYear:" + dayOfYear) ;
System . out. println ( "----------------------------------" ) ;
DayOfWeek dayOfWeek = ldDate. getDayOfWeek ( ) ;
System . out. println ( dayOfWeek) ;
System . out. println ( dayOfWeek. getValue ( ) ) ;
System . out. println ( "----------------------------------" ) ;
System . out. println ( ldDate. isBefore ( ldDate) ) ;
System . out. println ( ldDate. isAfter ( ldDate) ) ;
System . out. println ( "----------------------------------" ) ;
LocalDate withLocalDate = ldDate. withYear ( 2000 ) ;
System . out. println ( withLocalDate) ;
System . out. println ( "----------------------------------" ) ;
LocalDate minusYears = ldDate. minusYears ( 1 ) ;
System . out. println ( minusYears) ;
System . out. println ( "----------------------------------" ) ;
LocalDate plusYears = ldDate. plusYears ( 10 ) ;
System . out. println ( plusYears) ;
System . out. println ( "----------------------------------" ) ;
}
Tips 以上学习内容均来自于B站黑马程序员