欢迎来到我的主页:【一只认真写代码的程序猿】
本篇文章收录于专栏【小小爪哇】
如果这篇文章对你有帮助,希望点赞收藏加关注啦~
目录
4 DateTimeFormatter格式日期类(java8新特性)
1 第一代Date
Date:精确到毫秒,代表特定瞬间。SimpleDateFormat:格式和解析曰期的类SimpleDateFormat 格式化和解析日期的具体类。它允许进行格式化(日期 ->文本)解析(文本 ->日期)和规范化。
public static void main(String[] args) throws ParseException {
//1. 获取当前系统时间
//2. 这里的 Date 类是在 java.util 包
//3. 默认输出的日期格式是国外的方式, 因此通常需要对格式进行转换
Date d1 = new Date(); //获取当前系统时间
System.out.println("当前日期=" + d1);
//当前日期=Sun Dec 01 23:11:06 CST 2024
Date d2 = new Date(9234567); //通过指定毫秒数得到时间
System.out.println("d2=" + d2); //获取某个时间对应的毫秒数
//d2=Thu Jan 01 10:33:54 CST 1970
//1. 创建 SimpleDateFormat 对象, 可以指定相应的格式
//2. 这里的格式使用的字母是规定好, 不能乱写
SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 hh:mm:ss E");
String format = sdf.format(d1); // format:将日期转换成指定格式的字符串
System.out.println("当前日期=" + format);
//当前日期=2024 年 12 月 01 日 11:11:06 周日
//1. 可以把一个格式化的 String 转成对应的 Date
//2. 得到 Date 仍然在输出时, 还是按照国外的形式, 如果希望指定格式输出, 需要转换
//3. 在把 String -> Date , 使用的 sdf 格式需要和你给的 String 的格式一样, 否则会抛出转换异常
String s = "1996 年 01 月 01 日 10:20:30 星期一";
Date parse = sdf.parse(s);
System.out.println("parse=" + sdf.format(parse));
//parse=1996 年 01 月 01 日 10:20:30 周一
}
2 第二代Calendar
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> { }
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY OF MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
public static void main(String[] args) {
//1. Calendar 是一个抽象类, 并且构造器是 private
//2. 可以通过 getInstance() 来获取实例
//3. 提供大量的方法和字段提供给程序员
//4. Calendar 没有提供对应的格式化的类, 因此需要程序员自己组合来输出(灵活)
//5. 如果我们需要按照 24 小时进制来获取时间, Calendar.HOUR ==改成=> Calendar.HOUR_OF_DAY
Calendar c = Calendar.getInstance(); //创建日历类对象//比较简单, 自由
System.out.println("c=" + c);
//2.获取日历对象的某个日历字段
System.out.println("年: " + c.get(Calendar.YEAR));
//年: 2024
// 这里为什么要 + 1, 因为 Calendar 返回月时候, 是按照 0 开始编号
System.out.println("月: " + (c.get(Calendar.MONTH) + 1));//月: 12
System.out.println("日: " + c.get(Calendar.DAY_OF_MONTH));//日: 1
System.out.println("小时: " + c.get(Calendar.HOUR));//小时: 11
System.out.println("分钟: " + c.get(Calendar.MINUTE));//分钟: 14
System.out.println("秒: " + c.get(Calendar.SECOND));//秒: 4
//Calender 没有专门的格式化方法, 所以需要程序员自己来组合显示
System.out.println(c.get(Calendar.YEAR)
+ "-"+ (c.get(Calendar.MONTH) + 1)
+ "-"+c.get(Calendar.DAY_OF_MONTH)
+" " + c.get(Calendar.HOUR_OF_DAY)
+ ":" + c.get(Calendar.MINUTE)
+ ":"+ c.get(Calendar.SECOND));
//2024-12-1 23:14:4
}
3 第三代LocalDate
前面两代日期类的不足分析:
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar也存在问题是:
1)可变性:像日期和时间这样的类应该是不可变的。
2)偏移性:Date中的年份是从1900开始的,而月份都从0开始。
3)格式化:格式化只对Date有用,Calendar则不行。
4)此外,它们也不是线程安全的;不能处理闰秒等(每隔2天,多出1s)
1、LocalDate(日期/年月曰)、LocalTime(时间/时分秒)、LocalDateTime(日期时间/年月日时分秒)JDK8加入
2、LocalDate只包含日期,可以获取日期字段
3、LocalTime只包含时间,可以获取时间字段
4、LocalDateTime包含日期+时间,可以获取日期和时间字段
package DateTest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* @ClassName DateTest
* @Description 第三代日期类
* @Date 2024/12/1 23:08
* @Version V1.0
*/
public class DateTest {
public static void main(String[] args) {
//1. 使用 now() 返回表示当前日期时间的 对象
LocalDateTime ldt = LocalDateTime.now(); //LocalDate.now();//LocalTime.now()
System.out.println(ldt);
//2. 使用 DateTimeFormatter 对象来进行格式化
// 创建 DateTimeFormatter 对象
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(ldt);
System.out.println("格式化的日期=" + format);
System.out.println("年=" + ldt.getYear());
System.out.println("月=" + ldt.getMonth());
System.out.println("月=" + ldt.getMonthValue());
System.out.println("日=" + ldt.getDayOfMonth());
System.out.println("时=" + ldt.getHour());
System.out.println("分=" + ldt.getMinute());
System.out.println("秒=" + ldt.getSecond());
LocalDate now = LocalDate.now(); //可以获取年月日
LocalTime now2 = LocalTime.now();//获取到时分秒
//提供 plus 和 minus 方法可以对当前时间进行加或者减
//看看 890 天后, 是什么时候 把 年月日-时分秒
LocalDateTime localDateTime = ldt.plusDays(890);
System.out.println("890 天后=" + dateTimeFormatter.format(localDateTime));
//890 天后=2027-05-10 23:19:35
//看看在 3456 分钟前是什么时候, 把 年月日-时分秒输出
LocalDateTime localDateTime2 = ldt.minusMinutes(3456);
System.out.println("3456 分钟前 日期=" + dateTimeFormatter.format(localDateTime2));
//3456 分钟前 日期=2024-11-29 13:43:35
}
}
4 DateTimeFormatter格式日期类(java8新特性)
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String formattedDate1 = formatter1.format(localDate);
System.out.println(formattedDate1); //12月 01, 2024
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.CANADA);
String formattedDate2 = formatter2.format(localDate);
System.out.println(formattedDate2); //Dec 01, 2024
}
5 Instant时间戳
类似于Date,提供了一系列和Date类转换的方式:
Instant转Date:Date date = Date.from(instant);
Date转Instant:Instant Instant = date.toInstant();
public static void main(String[] args) {
//1.通过 静态方法 now() 获取表示当前时间戳的对象
Instant now = Instant.now();
System.out.println(now);
//2. 通过 from 可以把 Instant 转成 Date
Date date = Date.from(now);
//3. 通过 date 的 toInstant() 可以把 date 转成 Instant 对象
Instant instant = date.toInstant();
}
日期类的使用,自己需要的时候调用API查看即可,不需要太过于死记硬背。