在程序的执行过程中我们不可避免对时间的获取,转换操作,所以,了解常用的时间类还是很有必要的
计算机内的时间:
时间在计算机里是一维的,就如同一个直尺一般,而在计算机里,将1970年1月1日定为基准时间,每个度量单位是毫秒,一般用Long类型的变量来计量时间:Long now = System.currentTimeMillis();
时间处理核心类Date:
- 创建Date对象:
Date d = new Date(); //java.util.Date;Date类的无参构造实际上是调用有参构造Date(Long date)并将当前时间System.currentTimeMillis()作为有参构造Date(Long date)的参数。 System.out.println(d); //所以实际上打印的是当前系统时间 Date b = new Date(2000); //在时间基准的基础上往后推2000毫秒 System.out.println(b); //打印结果:Thu Jan 01 08:00:02 CST 1970;8小时的时间差是由于时区引起的。
- 常用方法:
- 测试此时间是否在指定时间之后:after(Date when);
- 得到当前时间距离时间基准的毫秒数:getTime();
- 其他的方法大多被Calendar类的方法取代,不被推荐使用了。
字符串时间对象转换DateFormat:
-
简述: DateFormat是一个抽象类,大多时候都使用其的子类SimpleDateFormat类来实现。
-
把时间对象按照"格式字符串指定的格式"转成相应的字符串:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //需要传递格式化字符 df.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); //设置时区 String str = df.format(new Date(400000)); //将new Date(400000)强制转换为格式化字符行驶的字符串进行返回 System.out.println(str); //,返回结果:1970-01-01 08:06:40
-
把字符串按照"格式字符串指定的格式"转成相应的时间对象:
DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒"); //指定可以转换的字符串格式 try { Date date = df2.parse("1983年05月10日 10时45分59秒"); System.out.println(date); //输出:Tue May 10 10:45:59 CST 1983 } catch (ParseException e) { e.printStackTrace(); }
-
需要输出指定日期或时间元素可以参考下表去实现:如需输出今天在今年中处在第几天,可以使用以下代码实现:
DateFormat df3 = new SimpleDateFormat("D"); String str3 = df3.format(new Date()); System.out.println(str3);
日期类Calendar:
- 简述:Calendar类提供了关于日期计算的相关功能,比如:年、月、日、时、分、秒的展示和计算;GregorianCalendar是Calendar的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
- 通过get方法获取calendar对象相关属性:
Calendar calendar = new GregorianCalendar(2999,10,9,22,10,5); //创建日期类,拥有不同的构造函数,这里用的是GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,int minute, int second),分别对应年月日时分秒 System.out.println(calendar); //java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2999,MONTH=10,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=9,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=10,SECOND=5,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?] int year = calendar.get(Calendar.YEAR); //获取年 int month = calendar.get(Calendar.MONTH); //获取月,0-11表示对应的月份。0是1月,1是2月……11是12月 int weekday = calendar.get(Calendar.DAY_OF_WEEK); //获取星期几。1-7,1对应星期日,2对应星期一……7对应星期六 int day = calendar.get(Calendar.DATE); //得到该天为该月的第几天,也可以用DAY_OF_MONTH
- 设置日期的相关属性:
//设置日期的相关属性 Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR,8012);
- 日期的计算:
Calendar c2 = new GregorianCalendar(); c2.add(Calendar.DATE,100); //在当天的基础上,往后一百天 c2.add(Calendar.YEAR,100); //在当天的基础上,往前一百年
- 日期对象和时间对象的转换:
Calendar c2 = new GregorianCalendar(); c2.add(Calendar.DATE,100); //在当天的基础上,往后一百天 c2.add(Calendar.YEAR,100); //在当天的基础上,往前一百年 //日期对象和时间对象的转换 Date d4 = c2.getTime(); //日期对象转时间对象 Calendar c3 = new GregorianCalendar(); c3.setTime(d4); //时间对象转日期对象