一、简介


二、常见的类
java.util.Date;
java.util.Calender
java.util.GregorianCalendar
java.text.DateFormat
java.text.SimpleDateFormat
三、常见用法
3.1获取当前时间
获取当前时间有两种办法,通过Calender获取还可以获取日期相关信息,比如当前星期几,一年中第几天等
Date currentTime = new Date();
Date currentTime2 = Calender.getInstance().getTime();
备注:currentTime.getTime()获取从1970.01.01 00:00:00到当前时间的总毫秒数
calendar.getTime()获取当前date对象
3.2按照相应格式打印时间
常用时间格式是SimpleDateFormat,它是DateFormat的子类,DateFormat是一个抽象类,它定义了日期格式化的字段和方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
Date date = new Date();
sdf.format(date);
具体格式字符代表

具体请看API
3.3将字符串转换成时间
String time = "2012-10-16";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.parse(time);
3.4时间加减
Date today = new Date();
long totalMilisecond = today.getTime()-7*24*60*60*1000;
Date sevenDayBefore = new Date(totalMilisecond );
3.5时间间隔
Date bornDay = new GregorianCalendar(1900,1,1).getTime();
Date today = new Date();
long diff= today.getTime()-bornDay.getTime();
System.out.println(bornDay+":"+today);
System.out.println("you were born total "+diff/(24*60*60*1000)+" day");
3.6比较日期
String[] input ={"2012-10-10","2012-10-11"};
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 =sdf.parse(input[0]);
Date d2 =sdf.parse(input[1]);
String relation =null ;
if (d1.equals(d2)) {
relation = " the same as ";
}else if(d1.before(d2)){
relation =" < ";
}else if(d1.after(d2)){
relation =" > ";
}
System.out.println(sdf.format(d1) +relation+sdf.format(d2) );
3.7获取当前日期信息
Calendar c = Calendar.getInstance();
System.out.println("year : "+c.get(Calendar.YEAR));
System.out.println("month : "+(c.get(Calendar.MONTH)+1));
System.out.println("day : "+c.get(Calendar.DATE));
System.out.println("day of week : "+c.get(Calendar.DAY_OF_WEEK));
System.out.println("day of month : "+c.get(Calendar.DAY_OF_MONTH));
System.out.println("day of year: "+c.get(Calendar.DAY_OF_YEAR));
System.out.println("week in month : "+c.get(Calendar.WEEK_OF_MONTH));
System.out.println("week in year : "+c.get(Calendar.WEEK_OF_YEAR));
System.out.println("day of week in month : "+c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : "+c.get(Calendar.HOUR));
System.out.println("AM or PM : "+c.get(Calendar.AM_PM));
System.out.println("Hour of day(24-hour clock) : "+c.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute : "+c.get(Calendar.MINUTE));
System.out.println("Second : "+c.get(Calendar.SECOND));
四、一个日期的小应用

public class ReminderService {
Timer timer = new Timer();
public static void main(String[] args) throws IOException {
new ReminderService().load();
}
void load() throws IOException{
BufferedReader bf = new BufferedReader(new FileReader("ReminderService.txt"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String aLine;
while((aLine=bf.readLine())!=null){
ParsePosition pp = new ParsePosition(0);
Date date = sdf.parse(aLine, pp);
if (date == null) {
message("Invalide date in \" "+aLine+" \"");
continue;
}
String mesg = aLine.substring(pp.getIndex());
timer.schedule(new Item(mesg), date);
}
}
class Item extends TimerTask{
String message;
public Item(String m) {
this.message=m;
}
@Override
public void run() {
message(message);
}
}
private void message(String message){
System.out.println("message: "+message);
JOptionPane.showMessageDialog(null, message, "Timer Alert", JOptionPane.INFORMATION_MESSAGE);
}
}
本文介绍Java中日期时间类的基本使用,包括获取当前时间、按指定格式打印时间、字符串与时间对象互相转换、进行时间加减运算、计算时间间隔、比较日期大小及获取当前日期的相关信息。
742

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



