日常的开发中,我们经常需要处理日期和时间的相关数据,可以看看下面的基本的用法。
public class CalendarDemo {
/*
* 获取年月日时分秒
*/
@Test
public void test1(){
Calendar c=Calendar.getInstance();
System.out.println("年:"+c.get(Calendar.YEAR));
System.out.println("月:"+(c.get(Calendar.MONTH)+1));//月份0-11
System.out.println("日:"+c.get(Calendar.DATE));
System.out.println("时:"+c.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+c.get(Calendar.MINUTE));
System.out.println("秒:"+c.get(Calendar.SECOND));
}
//java8
@Test
public void test2(){
LocalDateTime dt=LocalDateTime.now();
System.out.println("年:"+dt.getYear());
System.out.println("月:"+dt.getMonthValue());//月份1-12
System.out.println("日:"+dt.getDayOfMonth());
System.out.println("时:"+dt.getHour());
System.out.println("分:"+dt.getMinute());
System.out.println("秒:"+dt.getSecond());
}
//如何取得从 0 1970 年 1 1 月 月 1 1 日 日 0 0 时 0 0 分 0 秒到现在的毫秒数 ?
@Test
public void test3(){
System.out.println("方式一:"+Calendar.getInstance().getTimeInMillis());
System.out.println("方式二:"+System.currentTimeMillis());
System.out.println("方式三(java8):"+Clock.systemDefaultZone().millis());
}
@Test
public void test4(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为 1 号,当前日期既为本月第一天
String first = format.format(c.getTime());
System.out.println("===============first:"+first);
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:"+last);
//Java 8
LocalDate today = LocalDate.now();
//本月的第一天
LocalDate firstday = LocalDate.of(today.getYear(),today.getMonth(),1);
//本月的最后一天
LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月的第一天"+firstday);
System.out.println("本月的最后一天"+lastDay);
}
//LocalDateTime年月日十分秒;LocalDate日期;LocalTime时间;三个包的方法都差不多
@Test
public void test5(){
LocalDateTime dt1=LocalDateTime.now();
LocalDateTime dt2=LocalDateTime.of(2017, 5, 1, 11, 11, 11);
System.out.println(dt1.isBefore(dt2));
System.out.println(dt1.isAfter(dt2));
}
@Test
public void testTemporalAdjusters() {
LocalDate date = LocalDate.parse("2017-08-15");
//获取这个月的第一个周末的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY)));//2017-08-06
//获取上个月的最后一周
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY)));//2017-07-30
//获取这个月的倒数第一个周末的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(-1, DayOfWeek.SUNDAY)));//2017-08-27
//获取这个月的第一个周末的时间,上面的dayOfWeekInMonth更灵活,可以定义第几周
System.out.println(date.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));//2017-08-06
//其底层调用的是
// return TemporalAdjusters.dayOfWeekInMonth(1, dayOfWeek);
//明年的第一天
System.out.println(date.with(TemporalAdjusters.firstDayOfNextYear()));//2018-01-01
//获取下周5的时间
System.out.println(date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));//2017-08-18
System.out.println(date.with(TemporalAdjusters.next(DayOfWeek.TUESDAY)));//2017-08-22,始终返回下周的某个时间
//获取周2时间
System.out.println(date.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY)));//2017-08-15,如果当前时间刚好是星期三,那么就返回当前时间
// 获取本月最后一天
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth()));//2017-08-31
}
//格式化日期
@Test
public void test6(){
//以前用SimpleDateFormat
//java8
LocalDateTime dt1=LocalDateTime.now();
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dt1.format(formatter));
}
//打印昨天的当前时刻
@Test
public void test7(){
//java8
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime today=LocalDateTime.now();
LocalDateTime yesterday=today.minusDays(1);
System.out.println(yesterday.format(formatter));
}
/*
* 大多数日期/时间 API 类都实现了一系列工具方法,如:加/减天数、周数、月份数,等等。
* 还有其他的工具方法能够使用 TemporalAdjuster 调整日期,并计算两个日期间的周期。
*/
@Test
public void test8(){
//java8
LocalDate today=LocalDate.now();
System.out.println("90天后:"+today.plusDays(90));
System.out.println("本月最后一天:"+today.with(TemporalAdjusters.lastDayOfMonth()));
}
}