Java中涉及时间的类主要有Date, DateFormat(SimpleDateFormat), Calendar 。
(1)Date 时间类型,主要负责时间的存储和时间间隔的计算。 从属包 java.util
常用方法:
getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此Date 对象表示的毫秒数。
after(Date when) 测试此日期是否在指定日期之后。
before(Date when) 测试此日期是否在指定日期之前。
compareTo(Date anotherDate) 比较两个日期的顺序。当大于一个日期,返回1;等于返回0;小于返回-1。
(2)SimpleDateFormat 主要负责将时间转换成指定格式的字符串,及将指定格式的字符串转化成Date。 从属包 java.text
常用方法:
SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat
。
parse(String source) 从给定字符串的开始解析文本,以生成一个日期。
format(Date date)
将一个 Date 格式化为日期/时间字符串。
(3)Calendar 主要负责时间的计算,获取指定的年,月,日等信息。 从属包 java.util
常用方法:
getInstance()返回Calendar实例对象
setTime(Date date)
使用给定的Date
设置此 Calendar 的时间。
get(int field) 返回给定日历字段的值。可以Calendar.YEAR , Calendar.DAY_OF_MONTH 等获取指定的日期值。
getTime()返回操作后的日期。
add(int field, int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。
1 Date转String案例
package cjr.otherApi;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDome {
public static void main(String[] args) {
Date date = new Date();
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//2016-05-19 10:21:02
SimpleDateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//2016年05月19日 10时21分02秒
DateFormat df3 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");//2016/05/19 10:21:02 上午
DateFormat df4 = new SimpleDateFormat("yyyy-MM-dd");//2016-05-19
System.out.println(df1.format(date));
System.out.println(df2.format(date));
System.out.println(df3.format(date));
System.out.println(df4.format(date));
}
}
2016年05月19日 10时21分02秒
2016/05/19 10:21:02 上午
2016-05-19
2 String转Date案例
package cjr.otherApi;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDome {
public static void main(String[] args) throws ParseException {
String strDate = "1990年10月01日 00时00分00秒";
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = df.parse(strDate);
Date now = new Date() ;
System.out.println(date.compareTo(now));
System.out.println(date.compareTo(date));
System.out.println(now.compareTo(date));
String strDate1 = "1990年10月1日";
DateFormat df1 = new SimpleDateFormat("yyyy年M月d日");
Date date1 = df.parse(strDate);
System.out.println(date);
System.out.println(date1);
}
}
运行结果:
0
1
Mon Oct 01 00:00:00 CST 1990
Mon Oct 01 00:00:00 CST 1990
3 时间Calendar操作案例
package cjr.otherApi;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
//=============获取年月日等信息==================
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day =cal.get(Calendar.DAY_OF_MONTH);
System.out.println("现在时间是:"+year+"年"+month+"月"+day+"日");
//=============时间增减========================
cal.add(Calendar.DAY_OF_YEAR, 20);
System.out.println("20天后是:" + cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日");
cal.add(Calendar.DAY_OF_YEAR, -30);
System.out.println("10天前是:" + cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日");
}
}
运行结果:
20天后是:2016年5月8日
10天前是:2016年4月9日