Java日期处理

一、简介

 

二、常见的类

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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值