Java日期处理
一、简介
二、常见的类
java.util.Date;
java.util.Calender
java.util.GregorianCalendar
java.text.DateFormat
java.text.SimpleDateFormat
三、常见用法
3.1获取当前时间
获取当前时间有两种办法,通过Calender获取还可以获取日期相关信息,比如当前星期几,一年中第几天等
1 Date currentTime = new Date(); 2 Date currentTime2 = Calender.getInstance().getTime();
备注:currentTime.getTime()获取从1970.01.01 00:00:00到当前时间的总毫秒数
calendar.getTime()获取当前date对象
3.2按照相应格式打印时间
常用时间格式是SimpleDateFormat,它是DateFormat的子类,DateFormat是一个抽象类,它定义了日期格式化的字段和方法
1 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") 2 Date date = new Date(); 3 sdf.format(date);
具体格式字符代表
具体详情请看API
3.3将字符串转换成时间
1 String time = "2012-10-16"; 2 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 3 sdf.parse(time);
3.4时间加减
1 Date today = new Date(); 2 long totalMilisecond = today.getTime()-7*24*60*60*1000; 3 Date sevenDayBefore = new Date(totalMilisecond );
3.5时间间隔
1 Date bornDay = new GregorianCalendar(1900,1,1).getTime(); 2 Date today = new Date(); 3 long diff= today.getTime()-bornDay.getTime(); 4 System.out.println(bornDay+":"+today); 5 System.out.println("you were born total "+diff/(24*60*60*1000)+" day");
3.6比较日期
1 String[] input ={"2012-10-10","2012-10-11"}; 2 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 3 Date d1 =sdf.parse(input[0]); 4 Date d2 =sdf.parse(input[1]); 5 String relation =null ; 6 if (d1.equals(d2)) { 7 relation = " the same as "; 8 }else if(d1.before(d2)){ 9 relation =" < "; 10 }else if(d1.after(d2)){ 11 relation =" > "; 12 } 13 System.out.println(sdf.format(d1) +relation+sdf.format(d2) );
3.7获取当前日期信息
1 Calendar c = Calendar.getInstance(); 2 System.out.println("year : "+c.get(Calendar.YEAR)); 3 System.out.println("month : "+(c.get(Calendar.MONTH)+1)); 4 System.out.println("day : "+c.get(Calendar.DATE)); 5 System.out.println("day of week : "+c.get(Calendar.DAY_OF_WEEK)); 6 System.out.println("day of month : "+c.get(Calendar.DAY_OF_MONTH)); 7 System.out.println("day of year: "+c.get(Calendar.DAY_OF_YEAR)); 8 System.out.println("week in month : "+c.get(Calendar.WEEK_OF_MONTH)); 9 System.out.println("week in year : "+c.get(Calendar.WEEK_OF_YEAR)); 10 System.out.println("day of week in month : "+c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); 11 System.out.println("Hour : "+c.get(Calendar.HOUR)); 12 System.out.println("AM or PM : "+c.get(Calendar.AM_PM)); 13 System.out.println("Hour of day(24-hour clock) : "+c.get(Calendar.HOUR_OF_DAY)); 14 System.out.println("Minute : "+c.get(Calendar.MINUTE)); 15 System.out.println("Second : "+c.get(Calendar.SECOND));
四、一个日期的小应用
1 public class ReminderService { 2 3 Timer timer = new Timer(); 4 5 public static void main(String[] args) throws IOException { 6 new ReminderService().load(); 7 } 8 9 void load() throws IOException{ 10 BufferedReader bf = new BufferedReader(new FileReader("ReminderService.txt")); 11 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 12 String aLine; 13 while((aLine=bf.readLine())!=null){ 14 ParsePosition pp = new ParsePosition(0); 15 Date date = sdf.parse(aLine, pp); 16 if (date == null) { 17 message("Invalide date in \" "+aLine+" \""); 18 continue; 19 } 20 String mesg = aLine.substring(pp.getIndex()); 21 timer.schedule(new Item(mesg), date); 22 } 23 24 } 25 26 class Item extends TimerTask{ 27 String message; 28 29 public Item(String m) { 30 this.message=m; 31 } 32 33 @Override 34 public void run() { 35 message(message); 36 } 37 38 } 39 40 private void message(String message){ 41 System.out.println("message: "+message); 42 JOptionPane.showMessageDialog(null, message, "Timer Alert", JOptionPane.INFORMATION_MESSAGE); 43 } 44 }