外观
日期时间类
Date类
Date类的构造方法
public Date():创建一个当前时间戳的日期对象。public Date(long when):创建一个时间戳为when的时间对象。public Date(int year, int month, int date):创建一个日期为year年month月date日的日期对象。需要注意month是从0开始的,也就是说若要创建一个月份为1的日期对象需要使用0作为参数,此处我们也可以用Calendar类的月份常量代替。此方法已被弃用。public Date(int year, int month, int date, int hrs, int min):创建一个日期为year年month月date日hrs小时min分的日期对象,month月份的规则同上,此方法已被弃用。public Date(int year, int month, int date, int hrs, int min, int sec):创建一个日期为year年month月date日hrs小时min分sec秒的日期对象,month月份的规则同上,此方法已被弃用。
Date类的成员方法
after(Date when):判断当前日期是否在指定日期之后。before(Date when):判断当前日期是否在指定日期之前。
时间格式化
使用DateFormat类
DateFormat是抽象类,不能使用new实例化,可以根据需要选用的时间格式选择以下四种方法中的一种。
getInstance()getDateInstance()getTimeInstance()getDateTimeInstance()
DateFormat的格式化风格有以下几种:SHORT、MEDIUM、LONG、FULL。它们可以作为以上四种实例化方法的参数,输出对应格式的时间。
在实例化DateFormat对象后,调用对象的format(date)方法即可,其中date是Date的对象。
public class ClassDateFormat {
public static void main(String[] args) {
//DateFormat类是抽象类,不能用new实例化
//DateFormat类的格式化风格有SHORT,MEDIUM,LONG,FULL四种
//DateFormat类要想创建对象,可以调用以下方法:
//getInstance() getDateInstance() getTimeInstance() getDateTimeInstance()
//下面给几种DateFormat类实例化的例子
//注:使用sout方法输出时,要用该对象的format(Date date)方法将它转换为字符串
DateFormat df1 = DateFormat.getInstance();
System.out.println(df1.format(new Date())); //按照默认格式输出
DateFormat df2 = DateFormat.getTimeInstance(DateFormat.LONG);
System.out.println(df2.format(new Date())); //输出长类型格式的时间
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
System.out.println(df3.format(new Date())); //输出常量类型的日期
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
System.out.println(df4.format(new Date())); //输出长类型的日期和时间
}
}运行结果:
23-4-19 下午6:57
下午06时57分23秒
2023年4月19日
2023年4月19日 下午06时57分23秒使用SimpleDateFormat类
SimpleDateFormat继承自DateFormat类,可以使用向上转型实例化此对象,使用该类可以按照我们想要的格式格式化时间。
SimpleDateFormat类的构造方法:
public SimpleDateFormat(String pattern):按照pattern指定的模式格式化时间,其中每个子字符串所代表的意义如下:
| 符号 | 作用 |
|---|---|
| yyyy | 年份 |
| MM | 月份 |
| dd | 一个月中的某一天 |
| D | 一年中的某一天 |
| HH | 一天中的某个小时 |
| mm | 一小时中的某一分钟 |
| ss | 一分钟的某一秒 |
| E | 星期 |
| a | 上午或下午 |
在实例化SimpleDateFormat对象后,使用此对象的format(date)方法就可以格式化日期,参数date为Date的实例化对象。format()方法会返回一个字符串,表示格式化后的时间。
package FrequentClasses.ClassDateAndTime;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClassSimpleDateFormat {
public static void main(String[] args) {
//常用的时间格式:
//2021/10/25 yyyy/MM/dd
//2021.10.25 yyyy.MM.dd
//2021-10-25 00:11:22 星期日 yyyy-MM-dd HH:mm:ss EEEE
//下午3时 ah时
//今年已经过了256天 今年已经过了D天
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEEE HH时mm分ss秒");
System.out.print("各位观众大家好,现在是");
System.out.print(df.format(new Date()));
System.out.println(",欢迎收看新闻。");
}
}运行结果:
各位观众大家好,现在是2023年04月19日 星期三 18时57分52秒,欢迎收看新闻。Calendar类
因为Date类在设计之初没有考虑到国际化,且很多方法都已经过时了,因此JDK提供了新的时间处理类:Calendar类。
Calender类是抽象类,因此需要调用Calendar.getInstance()方法获取其对象。
Calendar类的成员方法:
getTime():返回一个字面值为当前日历对象所表示的时间的日期对象。set(int year, int month, int date, int hourOfDay, int minute, int second):设置当前日历对象所表示的时间。after(Object when):判断当前日历所表示的时间是否在when所表示的时间后面。若符合则返回true,否则返回false。before(Object when):判断当前日历所表示的时间是否在when所表示的时间前面。若符合则返回true,否则返回false。add(int field, int amount):将当前日历对象所表示的时间的一个字段加上一个时间量。field表示该时间如何表示。如c.add(Calendar.DAY_OF_MONTH, 30):表示加上30天。get(int field)获取日历对象的某一字段,获取对应的时间值。
package FrequentClasses.ClassDateAndTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class ClassCalendar {
public static void main(String[] args) {
//因为Date类在设计之初没有考虑到国际化 且很多方法都已经过时了 因此JDK提供了新的时间处理类 Calendar类
//Calender类是抽象类 因此需要调用Calendar.getInstance()方法获取其对象
Calendar calendar1 = Calendar.getInstance(); //默认的创建日历对象的方法
Date date = calendar1.getTime(); //getTime()方法返回一个字面值为当前日历对象所表示的时间的日期对象
System.out.println("使用日历实例化的日期对象所表示的时间是:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2020, Calendar.OCTOBER, 15, 12, 12, 14);
System.out.println("calender2所表示的时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar2.getTime()));
System.out.println("calender1在calender2后面吗?" + calendar1.after(calendar2));
System.out.println("calender1在calender2前面吗?" + calendar1.before(calendar2));
calendar2.add(Calendar.DAY_OF_MONTH, 30); //add()函数可以给日历对象的一个字段加减时间量 这里把calender2的日加了30天
System.out.println("calender2加了30天后所表示的时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar2.getTime()));
//注:Calendar.MONTH的第一个月使用0记录的 因此要获得月份数字的话需要在后面加1
//add(Calender.DAY_PF_MONTH, 0)获取的要把月份是上个月的最后一天 所以在调用时需加1
//Calendar.DAY_OF_WEEK的第一天是周日 要注意
int day = calendar2.get(Calendar.DAY_OF_MONTH); //get()方法可以获得指定字段所表示的时间值
System.out.println("calender2中该天在一个月中的位置为第" + day + "天");
}
}运行结果:
使用日历实例化的日期对象所表示的时间是:2023-04-19 18:58:21
calender2所表示的时间为:2020-10-15 12:12:14
calender1在calender2后面吗?true
calender1在calender2前面吗?false
calender2加了30天后所表示的时间为:2020-11-14 12:12:14
calender2中该天在一个月中的位置为第14天