java.util.Date;
java.util.Calender
java.util.GregorianCalendar
java.text.DateFormat
java.text.SimpleDateFormat
获取当前时间有两种办法,通过Calender获取还可以获取日期相关信息,比如当前星期几,一年中第几天等
Date currentTime = new Date(); Date currentTime2 = Calender.getInstance().getTime();
备注:currentTime.getTime()获取从1970.01.01 00:00:00到当前时间的总毫秒数
calendar.getTime()获取当前date对象
常用时间格式是SimpleDateFormat,它是DateFormat的子类,DateFormat是一个抽象类,它定义了日期格式化的字段和方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") Date date = new Date(); sdf.format(date);
具体格式字符代表
具体请看API
String time = "2012-10-16"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.parse(time);
Date today = new Date(); long totalMilisecond = today.getTime()-7*24*60*60*1000; Date sevenDayBefore = new Date(totalMilisecond );
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");
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) );
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); } }