1、将字符串转化为date类型、将时间戳转化为date类型
@Test
public void show(){
long timestamp = 1621234567890L; // 假设有一个时间戳
// 创建一个SimpleDateFormat对象,指定日期时间的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将时间戳转换为Date对象
Date date = new Date(timestamp);
// 使用SimpleDateFormat格式化Date对象为字符串
String formattedDate = sdf.format(date);
// 输出结果
System.out.println("时间戳:" + timestamp);
System.out.println("转换后的日期:" + formattedDate);
}
2、将字符串转化为时间戳
@Test
public void get(){
String timeString = "2021-01-01 00:00:00";
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
Date date = sdf.parse(timeString);
long timestamp = date.getTime();
System.out.println("时间戳:" + timestamp);
} catch (ParseException e) {
e.printStackTrace();
}
}
3、使用Calendar日历对象完成
// 为日历对象定义一个指定日期的数据
@Test
public void User(){
Calendar st=Calendar.getInstance();
st.set(2009,6 - 1,12);
System.out.println("年"+st.get(Calendar.YEAR));
System.out.println("月"+st.get(Calendar.MONTH));
System.out.println("日"+st.get(Calendar.DATE));
4将日历对象转化为时间戳(两种方法)
// 创建Calendar对象
Calendar calendar = Calendar.getInstance();
// 将Calendar对象转换为时间戳
long timeStamp = calendar.getTimeInMillis();
// 输出时间戳
System.out.println("时间戳:" + timeStamp);
//第二种
// 转换为时间戳
Date date = calendar.getTime();
long timestamp = date.getTime();
// 输出结果
System.out.println("Date: " + date);
System.out.println("Timestamp: " + timestamp);
5、两个时间类型的比较
@Test
public void Ust(){
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2020, 1, 1);
calendar2.set(2021, 1, 1);
if(calendar1.before(calendar2)){
System.out.println("calendar1在calendar2之前");
}else if(calendar1.after(calendar2)){
System.out.println("calendar1在calendar2之后");
}else{
System.out.println("calendar1和calendar2相同");
}
}
6、LocalDateTime时间类型演示
// 获取当前时间
// 根据指定的日期创建对象
// 往日期中进行加减法
public void Loca(){
LocalDateTime localDateTime=LocalDateTime.of(2018,1,12,4,1,1);
System.out.println(localDateTime);
LocalDateTime localDateTime1=LocalDateTime.now();
LocalDateTime plusyearsResult=localDateTime.plusYears(2L);
LocalDateTime plusMonths=localDateTime.plusMonths(3L);
LocalDateTime plusDays=localDateTime.plusDays(7L);
LocalDateTime plusHours=localDateTime.plusHours(2L);
LocalDateTime plusMinutes=localDateTime1.plusMinutes(10L);
LocalDateTime plus=localDateTime.plusSeconds(10L);
System.out.println("当前时间是:"+localDateTime+"\n"+
"当前时间加2年后为:"+plusyearsResult+"\n"+
"当前时间加3个月后:"+plusMonths+"\n"+"当前时间加七日后为:"
+plusDays+"\n"+"当前时间加2小时后:"+plusHours+"\n"+"当前时间加十分钟后为"+
plusMinutes+"\n"+"当前时间加十秒后为"+plus);
}
// 获取日期的年月日周时分秒
public void Dome(){
LocalDateTime localDateTime=LocalDateTime.now();
int year=localDateTime.getDayOfYear();
int MONth=localDateTime.getDayOfMonth();
DayOfWeek dayOfWeek=localDateTime.getDayOfWeek();
System.out.println("今天是:"+localDateTime+"\n"+
"本年当中第"+year+"\n"+"本月当中第"+ MONth+"\n本周当中第"+dayOfWeek.getValue()+
"-即"+dayOfWeek+"\n");
int years = localDateTime.getYear();
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
System.out.println("今天是"+ localDateTime + "\n"
+"年:" +years + "\n"
+"月︰" +month.getValue()+"-即"+month +
"\n"+"日" +day + "\n"
+"时:" +hour + "\n"+"分:" + minute + "\n"+"" +
"秒:" + second + "\n"
);
}
// 比较两个日期
// 转时间戳,考虑时区问题
@Test
public void Dome1(){
//判断两个时间点的前后
LocalDate localDate1 = LocalDate.of( 2017,8,8);
LocalDate localDate2 = LocalDate.of( 2018,8,8);
boolean date1IsBeforeDate2 = localDate1.isBefore(localDate2);
System.out.println("date1IsBeforeDate2 : " + date1IsBeforeDate2);
// date1IsBeforeDate2 == true
Instant instant = Instant.now();
//2019-06-08T16:50: 19.174z
System.out.println(instant);
Date date = Date.from(instant);
Instant instant2 = date.toInstant();
//Sun Jun 09 00: 50:19 CST 2019
System.out.println(date);
//2019-06-08T16:50: 19.174z
System.out.println(instant2);
}
7 计算两个日期间的间隔
@Test
public void Dome2(){
System.out.println("-------------------------------");
LocalDateTime date3 = LocalDateTime.now();
LocalDateTime date4 = LocalDateTime.of(2018, 1, 13, 22, 30, 10);
Duration duration = Duration.between(date3, date4);
System.out.println(date3 + " 与 " + date4 + " 间隔 " + "\n"
+ " 天 :" + duration.toDays() + "\n"
+ " 时 :" + duration.toHours() + "\n"
+ " 分 :" + duration.toMinutes() + "\n"
+ " 毫秒 :" + duration.toMillis() + "\n"
+ " 纳秒 :" + duration.toNanos() + "\n"
);
8. 自定义格式输出,日期类型转化为字符串
LocalDateTime date1 = LocalDateTime. now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String date2Str = formatter2.format(date1);
System.out.println(date2Str);
// 将字符串转化为日期
}
@Test
public void Dome3(){
String datetime = "2018-01-13 21:27:30";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(datetime,dtf);
System.out.println(ldt);
// 毫秒转日期?
System.out.println("---------long豪秒值转换为日期---------");
DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MW-dd HH:mm:ss");
String longToDateTime = df.format(LocalDateTime.ofInstant(
Instant.ofEpochMilli(System.currentTimeMillis()),ZoneId.of("Asia/Shanghai")));
System.out.println(longToDateTime);
}