专业网站开发,上海职业技能培训机构,wordpress微信插件,房地产新闻时事热点日期类常用的有三个#xff0c;Date类#xff0c;Calendar(日历)类和日期格式转换类(DateFormat)Date类中的大部分的方法都已经过时#xff0c;一般只会用到构造方法取得系统当前的时间。public class DateDemo {public static void main(String[] args) {Date date new Da…日期类常用的有三个Date类Calendar(日历)类和日期格式转换类(DateFormat)Date类中的大部分的方法都已经过时一般只会用到构造方法取得系统当前的时间。public class DateDemo {public static void main(String[] args) {Date date new Date();System.out.println(date);}}结果输出当前系统的时间Fri Mar 10 16:50:37 CST 2017我们可以看到这种格式的时间我们看着并不习惯所以在展示时间的时候必须要转换一下输出格式这时候我们要用到日期格式转换类DateFormat了。public class FormatDemo {public static void main(String[] args) {Date dnew Date();System.out.println(d);Format fnew SimpleDateFormat(yyyy-MM-dd hh-mm-ss);String sf.format(d);System.out.println(s);}}这时输出时间为2017-03-10 04-54-06这样就看着很舒服了。CalendarCalendar 类是一个抽象类它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法并为操作日历字段(例如获得下星期的日期)提供了一些方法。可以使用三种方法更改日历字段set()、add() 和 roll()。1set(f, value) 将日历字段f 更改为value。2add(f, delta) 将delta 添加到f 字段中。3roll(f, delta) 将delta 添加到f 字段中但不更改更大的字段。public class Test {public static void main(String[] args) {Calendar cnew GregorianCalendar();c.set(Calendar.DAY_OF_MONTH,1);System.out.println(输出的是本月第一天);System.out.println((c.get(Calendar.MARCH)1)月的c.get(Calendar.DAY_OF_MONTH)号);c.roll(Calendar.DAY_OF_MONTH,-1);System.out.println(输出的是本月最后一天);System.out.println((c.get(Calendar.MARCH)1)月的c.get(Calendar.DAY_OF_MONTH)号);}}输出结果为输出的是本月第一天3月的1号输出的是本月最后一天3月的31号Roll方法在操作的过程中一号天数减一之后直接又返回本月的最后一天日期变动在本月内循环而不会去改变月份即不会更改更大的字段。比较add方法public class Test {public static void main(String[] args) {Calendar cnew GregorianCalendar();c.set(Calendar.DAY_OF_MONTH,1);System.out.println(输出的是本月第一天);System.out.println((c.get(Calendar.MARCH)1)月的c.get(Calendar.DAY_OF_MONTH)号);c.add(Calendar.DAY_OF_MONTH,-1);System.out.println(输出的是上个月最后一天);System.out.println((c.get(Calendar.MARCH)1)月的c.get(Calendar.DAY_OF_MONTH)号);}}输出结果为输出的是本月第一天3月的1号输出的是本月最后一天2月的28号可以看出在三月一号的基础上减去一之后自动月份自动变到了二月。这个时roll方法和ad方法的区别。