Java Date类简介说明
下文笔者讲述Java中Date类的功能简介说明,如下所示:
例
在JDK1.0中 Date类是唯一的一个代表时间的类 但是由于Date类不便于实现国际化 所以从JDK1.1版本开始 笔者建议使用Calendar类进行时间和日期处理
Date类构造函数 //1.使用系统当前时间创建日期对象 Date() //2.使用年,月,日创建日期对象 Date(int year, int month, int date) Date类常用方法 void setTime(long date) //通过时间戳设置时间 String toString() //格式化时间 Date类中方法比较少通常配合Calendar(时间操作)及SimpleDateFormat(格式化日期)类一起操作
例
1. 返回当前时间的年月日时分秒
public static String DateDemo1() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
2. 获取一个日期的月份对应的第一天
public static Date DateDemo2(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, 1);
return DateUtil.setMinTime(calendar).getTime();
}
3. 设置时间前推或后推指定分钟的时间
public static Date DateDemo3(String sj1, String jj) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d;
try {
d = format.parse(sj1);
long Time = (d.getTime() / 1000) + Integer.parseInt(jj) * 60;
d.setTime(Time * 1000);
} catch (Exception e) {}
return d;
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


