package zongjie;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 获取当前时间,以“xxxx年xx月xx日 xx时xx分xx秒”的格式打印出来。
*/
public class Time1 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String nowTime2 = sdf.format(date);
System.out.println(nowTime2);
}
}
package zongjie;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Date;
public class Time2 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();//返回当前操作系统的时间
System.out.println(now);//2022-03-29T19:42:56.429
Date date = new Date(1000);//距离起始时间多了1000毫秒
System.out.println(date);//Thu Jan 01 08:00:01 CST 1970 08是因为在东八区
//使用指定年月日时分秒初始化一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.of(2022,3,29,13,52,20);
System.out.println(localDateTime);//2022-03-29T13:52:20
//获取年
int getYear = localDateTime.getYear();
System.out.println(getYear);//2022
//获取月
int getMonth = localDateTime.getMonthValue();
System.out.println(getMonth);//3
//获取英文版本的月份
Month month1 = localDateTime.getMonth();
System.out.println(month1);//MARCH
//获取本日是当前月份的第几天
int getday= localDateTime.getDayOfMonth();
System.out.println(getday);//29
//获取本日是当前年份第几天
int getday1 = localDateTime.getDayOfYear();
System.out.println(getday1);//88
//获取本日是星期几
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println(dayOfWeek);//TUESDAY
//获取时分秒
int hour = localDateTime.getHour();//时
int minute = localDateTime.getMinute();//分
int second = localDateTime.getSecond();//秒
System.out.println(hour);//13
System.out.println(minute);//52
System.out.println(second);//20
}
}
package zongjie;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Time3 {
public static void main(String[] args) {
//把一个LocalDateTime格式化一个字符串
LocalDateTime localDateTime = LocalDateTime.of(2022,3,22,15,30,24);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = localDateTime.format(pattern);
System.out.println(s);
//把一个日期字符解析为一个LocalDateTime对象
String s1 = "2022年03月29日 14:45:23";
DateTimeFormatter pattern1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(s1,pattern1);
System.out.println(parse);
//使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象
/**
* 将此时间加1年,将此时间减1年
*/
// 将此时间加1年 参数为正就是加几年、为负数就是减几年
LocalDateTime newLocalDateTime = localDateTime.plusYears(1);
System.out.println(newLocalDateTime);
//将此时间减1年 参数为正就是减几年、为负数就是加几年(和plus相反)
LocalDateTime localDateTime1 = localDateTime.minusYears(1);
System.out.println(localDateTime1);
/**
* 修改相关的方法:withXXX
*/
//修改年
LocalDateTime localDateTime2 = localDateTime.withYear(2023);
System.out.println(localDateTime2);
//修改月
LocalDateTime localDateTime3 = localDateTime.withMonth(12);
System.out.println(localDateTime3);
}
}
package jdk8dateperiod;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
/**
* period:是一个时间间隔类,主要用来获取两个时间之间的年,月,日,总月数
*/
public class JDK8DatePeriodDemo {
public static void main(String[] args) {
//LocalDateTime.of()
LocalDate localDate1 = LocalDate.of(2022,1,1);
LocalDate localDate2 = LocalDate.of(2048,12,12);
Period period = Period.between(localDate1,localDate2);//计算两个时间的间隔
System.out.println(period);//P26Y11M11D 相差26年11个月11天
System.out.println("----------------------------------------------");
//根据相差26年11个月11天获取
int years = period.getYears();//计算两个时间相差的年数26
System.out.println(years);
int months = period.getMonths();//计算两个时间相差的月数//11
System.out.println(months);
int days = period.getDays();//计算两个时间相差的天数//11
System.out.println(days);
System.out.println("-------------------------------------------------");
long months1 = period.toTotalMonths();//获取两个时间间隔的总月数
System.out.println(months1);
System.out.println("======================================================");
System.out.println("======================================================");
/**
* 获取两个时间之间的总天数
*/
LocalDateTime localDateTime1 = LocalDateTime.of(2020,1,1,15,12,56);
LocalDateTime localDateTime2 = LocalDateTime.of(2021,2,2,15,14,36);
Duration duration = Duration.between(localDateTime1,localDateTime2);//获取小时、分、秒
System.out.println(duration);//PT9552H1M40S
System.out.println(duration.toMillis());//获取时间间隔的毫秒
System.out.println(duration.toNanos());//获取时间间隔的纳秒
}
}
package date;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
//分配一个Date对象,初始化,代表被分配的时间
Date d1 = new Date();//把当前时间封装为一个Date对象
System.out.println(d1);//输出当前时间
//从时间原点开始,过了0毫秒
//在东八区,所以加了八小时
Date d2 = new Date(0L);//时间原点 + date
System.out.println(d2);//Thu Jan 01 08:00:00 CST 1970 因为在东八区,所以加了八小时
//1970年1月1日 上午的9点
long date = 60L*60*1000;//60毫秒*60*1000=1小时
Date d3 = new Date(date);
System.out.println(d3);//Thu Jan 01 09:00:00 CST 1970
}
}
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 构造方法:
* public SimpleDateFormat():使用默认格式
* public SimpleDateFormat(String pattern):使用指定格式
*
* Date-->String 格式化
*
*
* 听过查看API,找到了对应的模式
* 年 y
* 月 M
* 日 d
* 时 H
* 分 m
* 秒 s
*/
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//格式化:从Date到String
Date d = new Date();
//创建指定的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);//2022年03月29日 13:02:46x
System.out.println("----------------------------------------------");
//从String到Date
String date= "2022-3-29 13:04:50";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf1.parse(date);
System.out.println(date1);