Java日期函数的使用方法及其常见应用
Java日期函数是指用于处理日期和时间的Java类库(Java.util包和Java.time包),使用Java日期函数可以方便地操作日期和时间,实现时间的计算、格式化、比较以及转换等,是Java开发中常用的功能之一。下面将介绍Java日期函数的使用方法及其常见应用。
一、Java日期函数的使用方法
1.获取日期时间
获取当前日期时间
使用Java的Calendar类可以获取当前日期时间,代码如下:
import java.util.*;
public class Demo {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
System.out.println(now);
}
}
输出:
Wed Sep 15 10:38:45 CST 2021
使用Java 8的LocalDateTime类也可以获取当前日期时间,代码如下:
import java.time.*;
public class Demo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
}
}
输出:
2021-09-15T10:38:45.377
2.日期时间格式化
将日期时间按照指定格式进行格式化,常用的格式化方式有SimpleDateFormat和DateTimeFormatter。
使用SimpleDateFormat进行格式化,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String formatted = sdf.format(now);
System.out.println(formatted);
}
}
输出:
2021-09-15 10:38:45
使用DateTimeFormatter进行格式化,代码如下:
import java.time.*;
import java.time.format.*;
public class Demo {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String formatted = now.format(formatter);
System.out.println(formatted);
}
}
输出:
2021-09-15 10:38:45
3.日期时间比较
判断两个日期时间的先后顺序,可以使用compareTo方法或isBefore/isAfter方法。
使用compareTo方法进行比较,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
try {
Date date1 = sdf.parse("2021-01-01 00:00:00");
Date date2 = sdf.parse("2022-01-01 00:00:00");
boolean after1 = now.compareTo(date1) > 0;
boolean before2 = now.compareTo(date2) < 0;
System.out.println(after1 + ", " + before2);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出:
true, true
使用isBefore/isAfter方法进行比较,代码如下:
import java.time.*;
public class Demo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime date1 = LocalDateTime.parse("2021-01-01T00:00:00");
LocalDateTime date2 = LocalDateTime.parse("2022-01-01T00:00:00");
boolean after1 = now.isAfter(date1);
boolean before2 = now.isBefore(date2);
System.out.println(after1 + ", " + before2);
}
}
输出:
true, true
4.日期时间计算
对日期时间进行加减操作,需要使用Calendar类或LocalDateTime类中提供的方法。
使用Calendar类进行日期时间计算,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
calendar.setTime(now);
calendar.add(Calendar.DATE, -7); // 减去7天
Date before7Days = calendar.getTime();
String formatted = sdf.format(before7Days);
System.out.println(formatted);
}
}
输出:
2021-09-08 10:38:45
使用LocalDateTime类进行日期时间计算,代码如下:
import java.time.*;
public class Demo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime before7Days = now.minusDays(7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = before7Days.format(formatter);
System.out.println(formatted);
}
}
输出:
2021-09-08 10:38:45
二、Java日期函数的常见应用
1.计算某个日期之前/之后的日期
假设需要计算当前日期之前/之后1个月的日期,可以使用Calendar类或LocalDateTime类的方法进行计算。
使用Calendar类进行计算,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
calendar.setTime(now);
calendar.add(Calendar.MONTH, -1); // 减去1个月
Date before1Month = calendar.getTime();
String formatted = sdf.format(before1Month);
System.out.println(formatted);
calendar.setTime(now);
calendar.add(Calendar.MONTH, 1); // 添加1个月
Date after1Month = calendar.getTime();
formatted = sdf.format(after1Month);
System.out.println(formatted);
}
}
输出:
2021-08-15 2021-10-15
使用LocalDateTime类进行计算,代码如下:
import java.time.*;
import java.time.format.*;
public class Demo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime before1Month = now.minusMonths(1);
LocalDateTime after1Month = now.plusMonths(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatted1 = before1Month.format(formatter);
String formatted2 = after1Month.format(formatter);
System.out.println(formatted1);
System.out.println(formatted2);
}
}
输出:
2021-08-15 2021-10-15
2.计算日期之间相差的天数
计算两个日期之间相差的天数可以使用Calendar类或LocalDate类进行计算。
使用Calendar类进行计算,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = sdf.parse("2021-01-01");
Date date2 = sdf.parse("2021-10-01");
long diff = date2.getTime() - date1.getTime();
long days = diff / (1000 * 60 * 60 * 24);
System.out.println(days);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出:
273
使用LocalDate类进行计算,代码如下:
import java.time.*;
public class Demo {
public static void main(String[] args) {
String date1 = "2021-01-01";
String date2 = "2021-10-01";
LocalDate d1 = LocalDate.parse(date1);
LocalDate d2 = LocalDate.parse(date2);
long days = d1.until(d2, ChronoUnit.DAYS);
System.out.println(days);
}
}
输出:
273
3.日期时间转换
将日期时间字符串转换成Date对象或LocalDateTime对象,可以使用SimpleDateFormat或DateTimeFormatter进行转换。
使用SimpleDateFormat进行转换,代码如下:
import java.text.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
String str = "2021-09-15 10:38:45";
Date date = sdf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出:
Wed Sep 15 10:38:45 CST 2021
使用DateTimeFormatter进行转换,代码如下:
`java
import java.time.*;
import java.time.format.*;
public class Demo {
public static void main(String[] args) {
DateTimeFormatter formatter
