java常用类四:Math和Date
一、Date类
1.概述
Date类是一个较为简单的操作类,在使用中直接使用java.util.Date类的构造方法并进行输出就可以得到一个完整的日期,构造方法定义如下:
public Date()
得到当前系统日期:
import java.util.Date
public class DateDemo01{
public static void main(String args[]){
Date date = new Date(); //直接实例化Date对象
System.out.println("当前日期为:"+ date); //当前日期为:Tue May 25 23:19:04 CST 2021
}
}
从程序的运行结果看,已经得到了系统的当前日期,但是对于此日期可以发现格式并不是很符合平常看到的格式,而且现在的时间也不能准确地精确到毫秒,要想按照用户自己的格式显示时间可以使用Calendar类完成操作。
2.演示示例
2.1 Calendar类
Calendar类可以将取得的时间精确到毫秒。但是这个类本身是一个抽象类,如果想使用这个抽象类,那么则必须依靠对象的多态性,通过子类进行父类的实例化操作,Calendar类的子类是GregorianCalendar类。下面通过代码演示Calendar中提供的部分常量。
import java.util.*;
public class DateDemo02 {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar(); //实例化Calendar类对象
System.out.println("YEAR:"+calendar.get(Calendar.YEAR)); //获取年
System.out.println("MONTH:"+(calendar.get(Calendar.MONTH)+1)); //获取月
//获取日
System.out.println("DAY_OF_MONTH:"+calendar.get(Calendar.DAY_OF_MONTH));
//获取时
System.out.println("HOUR_OF_MONTH:"+calendar.get(Calendar.HOUR_OF_DAY));
//获取分
System.out.println("MINUTE:"+calendar.get(Calendar.MINUTE));
//获取秒
System.out.println("SECOND:"+calendar.get(Calendar.SECOND));
//获取毫秒
System.out.println("MILLISECOND:"+calendar.get(Calendar.MILLISECOND));
}
}
以上示例通过GregorianCalendar子类实例化Calendar类,然后通过Calendar类中的各种常量及方法取得系统的当前时间。
2.2 DateFormat类
在java.util.Date类中取得的时间是一个非常正确的时间,但是因为器显示的格式不理想,不符合咱们的习惯要求,那么实际上此时就可以为此类进行格式化操作,变成符合我们习惯的操作。这里需要注意的是DateFormat类与MessageFormat类都属于Format类的子类,专门用于格式化数据使用。DateFormat类的定义如下:
public abstract class DateFormat extends Format
从定义上看DateFormat类是一个抽象类,所以肯定无法直接实例化,但是此抽象类中提供了一个静态方法,可以直接取得本类的实例,几个常用方法通过代码给大家演示一下:
import java.text.DateFormat;
import java.util.Date;
public class DateDemo03 {
public static void main(String[] args) {
DateFormat df1 = null; //声明DateFormat类的对象
DateFormat df2 = null;
df1 = DateFormat.getDateInstance(); //取得日期
df2 = DateFormat.getDateTimeInstance();
//格式化日期
System.out.println("DAIE:"+df1.format(new Date())); //DAIE:2021年5月26日
//DAIETIME:2021年5月26日 上午12:14:48
System.out.println("DAIETIME:"+df2.format(new Date()));
}
}
从结果中发现,第2个DATETIME显示了时间,但还不是比较合理的中文显示格式,如果想取更加合理的时间,则必须在构造DateFormat对象时传递若干个参数。修改结果如下:
import java.text.DateFormat;
import java.util.Date;
import java.util.Local;
public class DateDemo04 {
public static void main(String[] args) {
DateFormat df1 = null; //声明DateFormat类的对象
DateFormat df2 = null;
//得到日期的DateFormat对象
df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD, new Locale("zh", "CN"));
df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD, DateFormat.ERA_FIELD,new Locale("zh", "CN"));
//按照日期格式化
System.out.println("DAIE:"+df1.format(new Date())); //DAIE:2021年5月26日
//DAIETIME:2021年5月26日 中国标准时间 下午6:03:15
System.out.println("DAIETIME:"+df2.format(new Date()));
}
}
上面的日期时间已经被格式化,格式是其默认的时间显示格式,但是如果现在要想得到用户自己需要的日期显示格式,则必须通过DateFormat的子类SimpleDateFormat类完成
2.3 SimpleDateFormat类
在项目开发中,会经常将一个日期转换为另一种日期格式,例如,原日期为2021-5-2618:09:25:345,转换后日期为2021年5月26日18时09分25秒345毫秒。从两个日期可以发现,日期的数字完全一样,只是日期格式有所不同,要想实现转换就必须使用java.text包中的SimpleDateFormat类完成。
import java.util.*; //导入需要的工具包
import java.text.*; //导入SimpleDateFormat所在的包
class DateTime{ //以后直接通过此类就可以取得日期时间
private SimpleDateFormat sdf = null; //声明SimpleDateFormate对象
//得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
public String getDate(){
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return this.sdf.format(new Date()); //将当前日期进行格式化操作
}
//得一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
public String getDateComplete(){
this.sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
return this.sdf.format(new Date()); //将当前日期进行格式化操作
}
//得到一个时间戳
public String getTimeStamp(){
this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return this.sdf.format(new Date()); //将当前日期进行格式化操作
}
class DateDemo07{
public static void main(String args[]){
DateTime dt = new DateTime();
//系统日期:2021-05-26 18:21:57.801
System.out.println("系统日期:"+dt.getDate());
//中文日期:2021年05月26日 18时21分57秒804毫秒
System.out.println("中文日期:"+dt.getDateComplete());
//时间戳:20210526182157804
System.out.println("时间戳:"+dt.getTimeStamp());
}
}
}
本文详细介绍了Java Date类的使用,包括获取当前日期、Calendar类的精确时间操作,以及如何通过DateFormat和SimpleDateFormat定制日期格式。同时涵盖了Math类的基础数学运算功能。

5860

被折叠的 条评论
为什么被折叠?



