Java中的时间日期函数:Date和Calendar类的使用详解
Java中的时间日期函数Date和Calendar类,是Java中操作时间日期的核心类之一。本文将详细介绍Date和Calendar类的使用方法。
1.Date类
1.1 Date类的定义
Date类是Java中用于表示日期和时间的类。它的构造方法如下:
public Date() public Date(long time)
其中,无参构造方法创建的是表示当前日期和时间的Date对象,有参构造方法创建的是表示从1970年1月1日0时0分0秒UTC至指定时间之间经过的毫秒数的Date对象。
1.2 Date类的常用方法
* getTime():获取Date对象表示的时间点的毫秒数。
* setTime(long time):将Date对象的时间设置为指定毫秒数后的时间。
* before(Date when):如果Date对象的时间早于when指定的时间,则返回true,否则返回false。
* after(Date when):如果Date对象的时间晚于when指定的时间,则返回true,否则返回false。
* equals(Object obj):如果obj指定的对象和Date对象表示的时间相同,则返回true,否则返回false。
下面是一段使用Date类的代码示例:
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
// 创建Date对象表示当前日期和时间
Date now = new Date();
System.out.println("当前时间:" + now);
// 创建指定时间的Date对象
Date date = new Date(1635710634000L);
System.out.println("指定时间:" + date);
// 获取毫秒数
long time = now.getTime();
System.out.println("当前时间的毫秒数:" + time);
// 设置时间
now.setTime(1635710634000L);
System.out.println("修改后的当前时间:" + now);
// 比较时间
boolean isBefore = now.before(date);
System.out.println("当前时间是否早于指定时间:" + isBefore);
boolean isAfter = now.after(date);
System.out.println("当前时间是否晚于指定时间:" + isAfter);
boolean isEqual = now.equals(date);
System.out.println("当前时间是否等于指定时间:" + isEqual);
}
}
该代码输出:
当前时间:Tue Nov 02 08:30:38 CST 2021 指定时间:Sun Oct 31 22:17:14 CST 2021 当前时间的毫秒数:1635810638217 修改后的当前时间:Sun Oct 31 22:17:14 CST 2021 当前时间是否早于指定时间:false 当前时间是否晚于指定时间:true 当前时间是否等于指定时间:true
2.Calendar类
2.1 Calendar类的定义
Calendar类是Java中的日历类,提供了操作日期和时间的各种方法。它是一个抽象类,不能直接创建对象,需要调用其静态方法getInstance()获取实例对象。
public static Calendar getInstance() public static Calendar getInstance(TimeZone zone) public static Calendar getInstance(Locale aLocale) public static Calendar getInstance(TimeZone zone, Locale aLocale)
其中,无参方法获取的是当前时区的系统日历对象,有参方法可以指定时区或语言环境。
2.2 Calendar类的常用方法
* get(int field):获取指定字段的值,如Calendar.YEAR、Calendar.MONTH、Calendar.DAY_OF_MONTH等。
* set(int field, int value):将指定字段设置为指定的值。
* add(int field, int amount):将指定字段的值加上指定的量。
* getTime():获取日历对象表示的时间点所对应的Date对象。
* setTime(Date date):将日历对象表示的时间设置为指定的Date对象的时间。
下面是一段使用Calendar类的代码示例:
import java.util.Calendar;
import java.util.Locale;
public class TestCalendar {
public static void main(String[] args) {
// 获取当前日期
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
int day = now.get(Calendar.DAY_OF_MONTH);
System.out.println("当前日期:" + year + "-" + month + "-" + day);
// 设置日期
now.set(Calendar.YEAR, 2022);
now.set(Calendar.MONTH, 10);
now.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("修改后的日期:" + now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DAY_OF_MONTH));
// 添加日期
now.add(Calendar.MONTH, 1);
System.out.println("添加一个月后的日期:" + now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DAY_OF_MONTH));
// 获取时间戳
long timeStamp = now.getTimeInMillis();
System.out.println("时间戳:" + timeStamp);
// 将时间戳转为日期
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
System.out.println("时间戳转换为日期:" + calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH));
// 按照指定的语言环境格式化输出
now.set(Calendar.YEAR, 2021);
now.set(Calendar.MONTH, 10);
now.set(Calendar.DAY_OF_MONTH, 2);
String dateString = now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println("英文格式的日期:" + dateString);
dateString = now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.CHINA);
System.out.println("中文格式的日期:" + dateString);
}
}
该代码输出:
当前日期:2021-11-2 修改后的日期:2022-11-1 添加一个月后的日期:2022-12-1 时间戳:1662097200000 时间戳转换为日期:2022-11-2 英文格式的日期:Tuesday 中文格式的日期:星期二
以上就是Java中Date和Calendar类的详细使用方法。需要注意的是,Java 8以后推荐使用新的时间日期API(如LocalDate、LocalTime、LocalDateTime、ZonedDateTime等),因为它们更加简单易用,并且提供了线程安全和时区处理等优势。
