- 1.Date
- 2.SimpleDateFormat
- 3.JDK8新增日期类
- 3.1 LocalDateTime
- 3.2 Period
- 3.3 Duration
import java.util.Date;
public class DateDemo1 {
public static void main(String[] args) {
// public Date() 创建一个Date对象,表示默认时间
Date date1 = new Date();
System.out.println(date1); // Wed Nov 24 18:29:17 CST 2021
// public Date(long date) 创建一个Date对象,表示指定时间
Date date2 = new Date(0L); // 从时间原点开始过了0毫秒,中国在东八区需要+8小时
System.out.println(date2); // Thu Jan 01 08:00:00 CST 1970
Date date3 = new Date(3600L * 1000);
System.out.println(date3); // Thu Jan 01 09:00:00 CST 1970
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo2 {
public static void main(String[] args) {
// public long getTime() 获取时间对象的毫秒值
Date date1 = new Date(); // 把当前时间封装成一个date对象
long time1 = date1.getTime(); // 获取当前时间的毫秒值
System.out.println(time1); // 1637750501959
long time2 = System.currentTimeMillis();
System.out.println(time2); // 1637750501959
// public void setTime(long time) 设置时间,传递毫秒值
Date date2 = new Date();
date2.setTime(0L);
System.out.println(date2); // Thu Jan 01 08:00:00 CST 1970
}
}
2.SimpleDateFormat
SimpleDateFormat 可以对 Date 对象进行格式化和解析。
常用的模式字母及对应关系:y 年,M 月,d 日,H 时,m 分,s 秒。
格式化(从 Date 到 String):public final String format(Date date) 把时间按照固定格式进行展示。
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo3 {
public static void main(String[] args) {
// 当前时间的Date对象
Date date = new Date();
// 创建一个日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String result1 = sdf.format(date);
System.out.println(result1);
}
}
解析(从 String 到 Date):public Date parse(String source) 需要对时间进行计算。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo4 {
public static void main(String[] args) throws ParseException {
String s = "2022-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(s);
System.out.println(date); // Sat Jan 01 00:00:00 CST 2022
}
}
示例:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo5 {
public static void main(String[] args) throws ParseException {
String start = "2020年11月11日 0:0:0";
String end = "2020年11月11日 0:10:0";
String jia = "2020年11月11日 0:03:47";
String pi = "2020年11月11日 0:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
long startTime = sdf.parse(start).getTime();
long endTime = sdf.parse(end).getTime();
System.out.println(startTime); // 1605024000000
System.out.println(endTime); // 1605024600000
long jiaTime = sdf.parse(jia).getTime();
long piTime = sdf.parse(pi).getTime();
if(jiaTime >= startTime && jiaTime <= endTime){
System.out.println("小贾同学参加上了秒杀活动");
}else{
System.out.println("小贾同学没有参加上秒杀活动");
}
if(piTime >= startTime && piTime <= endTime){
System.out.println("小皮同学参加上了秒杀活动");
}else{
System.out.println("小皮同学没有参加上秒杀活动");
}
}
}
3.JDK8新增日期类
LocalDate 表示日期(年月日),例如 2020年11月11日。
LocalTime 表示时间(时分秒),例如 13:10:10。
LocalDateTime 表示日期+时间(年月日时分秒),例如 2020年11月11日13:10:10。
这三个类的使用方法十分相似,下面以 LocalDateTime 为例进行介绍。
3.1 LocalDateTimepackage com.qdu.jdk8date;
import java.time.LocalDateTime;
public class JDK8DateDemo2 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime);
}
}
package com.qdu.jdk8date;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
public class JDK8DateDemo3 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 20);
// public int getYear() 获取年
int year = localDateTime.getYear();
System.out.println("年为" +year); // 年为2020
// public int getMonthValue() 获取月份(1-12)
int month = localDateTime.getMonthValue();
System.out.println("月份为" + month); // 月份为11
Month month1 = localDateTime.getMonth();
System.out.println(month1); // NOVEMBER
// public int getDayOfMonth() 获取月份中的第几天(1-31)
int day = localDateTime.getDayOfMonth();
System.out.println("日期为" + day); // 日期为11
// public int getDayOfYear() 获取一年中的第几天(1-366)
int dayOfYear = localDateTime.getDayOfYear();
System.out.println("这是一年中的第" + dayOfYear + "天"); // 这是一年中的第316天
// public DayOfWeek getDayOfWeek() 获取星期
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("星期为" + dayOfWeek); // 星期为WEDNESDAY
// public int getMinute() 获取分钟
int minute = localDateTime.getMinute();
System.out.println("分钟为" + minute); // 分钟为11
// public int getHour() 获取小时
int hour = localDateTime.getHour();
System.out.println("小时为" + hour); // 小时为11
}
}
package com.qdu.jdk8date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class JDK8DateDemo4 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 8, 10, 12);
// public LocalDate toLocalDate() 转换成为一个LocalDate对象
LocalDate localDate = localDateTime.toLocalDate();
System.out.println(localDate); // 2020-12-12
// public LocalTime toLocalTime() 转换成为一个LocalTime对象
LocalTime localTime = localDateTime.toLocalTime();
System.out.println(localTime); // 08:10:12
}
}
package com.qdu.jdk8date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class JDK8DateDemo5 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 12, 13, 14, 15);
System.out.println(localDateTime); // 2020-11-12T13:14:15
// public String format(指定格式) 把一个LocalDateTime格式化成为一个字符串
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = localDateTime.format(pattern);
System.out.println(s); // 2020年11月12日 13:14:15
// public static LocalDateTime parse(准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象
s = "2020年11月12日 13:14:15";
LocalDateTime parse = LocalDateTime.parse(s, pattern);
System.out.println(parse); // 2020-11-12T13:14:15
}
}
package com.qdu.jdk8date;
import java.time.LocalDateTime;
public class JDK8DateDemo6 {
public static void main(String[] args) {
// public LocalDateTime plusYears (long years) 添加或者减去年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
LocalDateTime newLocalDateTime = localDateTime.plusYears(1);
System.out.println(newLocalDateTime); // 2021-11-11T13:14:15
newLocalDateTime = localDateTime.plusYears(-1);
System.out.println(newLocalDateTime); // 2019-11-11T13:14:15
}
}
package com.qdu.jdk8date;
import java.time.LocalDateTime;
public class JDK8DateDemo7 {
public static void main(String[] args) {
// public LocalDateTime minusYears (long years) 减去或者添加年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
LocalDateTime newLocalDateTime = localDateTime.minusYears(1);
System.out.println(newLocalDateTime); // 2019-11-11T13:14:15
newLocalDateTime = localDateTime.minusYears(-1);
System.out.println(newLocalDateTime); // 2021-11-11T13:14:15
}
}
package com.qdu.jdk8date;
import java.time.LocalDateTime;
public class JDK8DateDemo8 {
public static void main(String[] args) {
// public LocalDateTime withYear(int year) 修改年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
LocalDateTime newLocalDateTime = localDateTime.withYear(2048);
System.out.println(newLocalDateTime); // 2048-11-11T13:14:15
// newLocalDateTime = localDateTime.withMonth(20); // Invalid value for MonthOfYear (valid values 1 - 12)
// System.out.println(newLocalDateTime);
}
}
3.2 Period
package com.qdu.jdk8date;
import java.time.LocalDate;
import java.time.Period;
public class JDK8DateDemo9 {
public static void main(String[] args) {
// public static Period between(开始时间, 结束时间) 计算两个"时间"的间隔
LocalDate localDate1 = LocalDate.of(2020, 1, 1);
LocalDate localDate2 = LocalDate.of(2048, 12, 12);
Period period = Period.between(localDate1, localDate2);
System.out.println(period); // P28Y11M11D
// public int getYears() 获得这段时间的年数
System.out.println(period.getYears()); // 28
// public int getMonths() 获得此期间的月数
System.out.println(period.getMonths()); // 11
// public int getDays() 获得此期间的天数
System.out.println(period.getDays()); // 11
// public long toTotalMonths() 获取此期间的总月数
System.out.println(period.toTotalMonths()); // 347
}
}
3.3 Duration
package com.qdu.jdk8date;
import java.time.Duration;
import java.time.LocalDateTime;
public class JDK8DateDemo10 {
public static void main(String[] args) {
// public static Duration between(开始时间,结束时间) 计算两个“时间"的间隔
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
Duration duration = Duration.between(localDateTime1, localDateTime2);
System.out.println(duration); // PT21H57M58S
// public long toSeconds() 获得此时间间隔的秒
System.out.println(duration.toSeconds()); // 79078
// public int toMillis() 获得此时间间隔的毫秒
System.out.println(duration.toMillis()); // 79078000
// public int tonanos() 获得此时间间隔的纳秒
System.out.println(duration.toNanos()); // 79078000000000
}
}



