Java 日期 API
JDK8之前
日期与时间戳之间的转换
public class Test {
public static void main(String[] args) {
Date date = new Date();
System.out.println("date = " + date); // date = Sun Sep 26 14:48:52 CST 2021
Date date1 = new Date(1632638970000L);
System.out.println("date1 = " + date1); // date1 = Sun Sep 26 14:49:30 CST 2021
long time = date.getTime();
System.out.println("time = " + time); // time = 1632639026166
date.setTime(1632638970000L);
System.out.println("date = " + date); // date = Sun Sep 26 14:49:30 CST 2021
}
}
日历获取年月日字段
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int milliSecond = calendar.get(Calendar.MILLISECOND);
// 2021-9-26 15:14:6:16
System.out.print(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + ":" + milliSecond);
}
}
日期格式化
public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
// 将日期对象格式化为字符串
String format = sdf.format(date);
System.out.println("format = " + format); // format = 2021-09-26 16:02:17
// 将字符串解析为日期对象
Date parse = sdf.parse(format);
System.out.println("parse = " + parse); // parse = Sun Sep 26 16:02:17 CST 2021
}
}
JDK8之后
之前存在的问题
- Java 1.0 中包含了一个 Date 类,但是它的大多数方法已经在 Java 1.1 中引入 Calendar 类的时候被废弃了。
-
格式化只对 Date 有效,Calendar 则不行。
-
它们不是线程安全的,不能处理闰秒等
注:API 功能更加强大,但好像用的更多的还是老版本写法
本地日期时间
-
Instant 是带时区的(以UTC为准),用来替换以前的 Date
-
LocalDateTIme 是不带时区(以本地时区为准)的,用来替换以前的 Calendar
public class Test {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("now = " + now); // now = 2021-09-26
LocalTime now1 = LocalTime.now();
System.out.println("now1 = " + now1); // now1 = 20:02:32.659
LocalDateTime now2 = LocalDateTime.now();
System.out.println("now2 = " + now2); // now2 = 2021-09-26T20:02:32.660
Instant now = Instant.now();
System.out.println("now = " + now); // now = 2021-09-27T01:54:39.351Z
LocalDateTime now1 = LocalDateTime.now();
System.out.println("now1 = " + now1); // now1 = 2021-09-27T09:54:39.401
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
System.out.println("获取年份:" + year);
int monthValue = now.getMonthValue();
System.out.println("获取月份:" + monthValue);
int dayOfMonth = now.getDayOfMonth();
System.out.println("获取天数:" + dayOfMonth);
int dayOfYear = now.getDayOfYear();
System.out.println("获取一年中的第" + dayOfYear + "天");
DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println("获取星期" + dayOfWeek.getValue());
int hour = now.getHour();
System.out.println("获取小时:" + hour);
int minute = now.getMinute();
System.out.println("获取分钟:" + minute);
int second = now.getSecond();
System.out.println("获取秒:" + second);
int nano = now.getNano();
System.out.println("获取纳秒:" + nano);
}
}
日期格式化
public class Test {
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将日期格式化为字符串
String format = df.format(LocalDateTime.now());
System.out.println("format = " + format); // format = 2021-09-27 13:55:51
// 将字符串解析为日期
TemporalAccessor parse = df.parse("2011-11-11 11:11:11");
LocalDateTime from = LocalDateTime.from(parse);
System.out.println("from = " + from); // from = 2011-11-11T11:11:11
}
}
标签:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类