date时间日期与java8中LocalDate使用

 
  
      (1)Date:表示特定的瞬间,精确到毫秒(因为闰秒的原因,所以其实结果并不是特别的准确,但是如果要求不是特别严格,影响并没有很大。)

    构造方法:Date()、Date(Long date)

    常用方法:

    void setTime(Long time):根据毫秒数设置该日期对象,默认构造函数设置该日期对象为当前日期。

    Long getTime():获取日期对象毫秒数。毫秒数都是以1970年1月1日0点0分0秒开始计算。

    (下面几个方法,在源代码中,实际比较的还是两个日期的毫秒数)

    int compareTo(Date date):比较俩个日期顺序,比参数date小,返回负数,相等返回0,大则返回正数。

    boolean before(Date date):判断是否在参数date之前,是则返回true。

    boolean after(Date date):同上,判断在是否在参数之后。

    boolean equals(Object date):当且仅当date不为空,是Date对象,而且毫秒数与调用方法的日期对象相等才为true。
    (2)SimpleDateFormat:继承DateFormat类,主要用来进行格式转换。

    构造函数:SimpleDateFormat(String pattern),最常用的构造方法。根据指定格式来转换字符串与日期。

    常用的俩个格式转换方法:字符串转换成Date类:Date parse(String date)、Date类转换成字符串类型:String format(Date date),都是继承DateFormat类的方法。其中,parse方法会抛出一个转换异常:
    如下使用案例:
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
        Date date=(Date)simpleDateFormat.parse("2018-09");
        System.out.println(date);
        String dateStr=simpleDateFormat.format(date);
        System.out.println(dateStr);
        //将转换格式应用为201809这种,年月之间没有分隔符号的,只要年月相同,结果就与上面相同
        simpleDateFormat.applyPattern("yyyyMM");
        Date date2=(Date)simpleDateFormat.parse("201809");
        System.out.println(date2);
        String dateStr2=simpleDateFormat.format(date2);
        System.out.println(dateStr2);
    上面用到的applyPattern方法用来切换需要转换的字符串格式。
    
    (3)Calendar:(一个日历对象)一个抽象类,为特定瞬间和一组日历字段之间的转换以及操作日历字段提供了方法。

  使用Calendar.getInstance获取该对象:Calendar nowTime=Calendar.getInstance();

   部分常用方法:

  void setTime(Date date):通过该类方法指定Calendar对象表示的日期。

  Date getTime():获取一个Date对象。

 void setTimeInMillis(Long time):同上。

 void add(int field,int amount):给当前对象的指定字段增加指定数值。Calender类为日历中的各种字段都设置了int类型的数值,比如,
 
    public final static int YEAR = 1;
    public final static int MONTH = 2;
    public final static int WEEK_OF_YEAR = 3;
字段‘年’所对应的int值为1,如果你想将当前日历类对象的年份加1年,那么只需这样做:

 
Calendar nowTime=Calendar.getInstance();
System.out.println(nowTime.get(Calendar.YEAR));
nowTime.add(YEAR,1);
System.out.println(nowTime.get(Calendar.YEAR));

//通过日历中的字段,获取一个月的天数
        Integer daysNumber=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//获取当天对应的星期数,星期天为1,星期六为7,详见Calendar类的常量
        Integer dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
        SimpleDateFormat类用于转换格式,String←→Date;

Date类用于获取毫秒数,或者作为设置Calendar对象日期的参数,date.getTime()、calendar.setTime(date)

Calendar类用于具体操作,比如获取指定年、月的天数,日期对应的星期数,对应的月份数等。


java8之后引入了localDate。localDateTime等
 // 获取当前日期  LocalDate now = LocalDate.now();
 // 设置日期
        LocalDate now2 = LocalDate.of(2099, 2, 28);
        // 解析日期,格式必须是yyyy-MM-dd
        LocalDate now3 = LocalDate.parse("2018-01-12");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String formatRs = now.format(dtf);

// 取本月第一天
        LocalDate firstDay = now.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate firstDay2 = now.withDayOfMonth(1);
 // 取本月第2天
        LocalDate secondDay = now.withDayOfMonth(2)
// 明年的这一天(年增加一年)
        LocalDate localDate = now.plusYears(1);
// 当前日期加上往后推20天(天增加20天和年增加10年)
        LocalDate plusDate = now.plus(20, ChronoUnit.DAYS);
        LocalDate plusYear = now.plus(10, ChronoUnit.YEARS);
// 当前日期往前推10天(天减少10天和年减少10年)
        LocalDate minusDay = now.minusDays(10);
        LocalDate minusYear = now.minus(10, ChronoUnit.YEARS);
比较日期大小 用minusDay.equals(LocalDate.of(2018, 04, 27));
// 判断日期前后  -> false   now.isAfter(LocalDate.of(2018, 04, 26));//false 
    now.isBefore(LocalDate.of(2018, 04, 26));//false
    // 计算两个日期之间的时间间隔   格式为:x年x月x天
        Period between = Period.between(now, LocalDate.of(2018, 05, 28));
        long bwDays = ChronoUnit.DAYS.between(now, LocalDate.of(2018, 05, 28));
//localDate转Date
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = now.atStartOfDay(zoneId);
        Instant instant = zdt.toInstant();
        Date fromDate = Date.from(instant);
        // Date转LocalDate
        Date date = new Date();
        Instant instantToUse = date.toInstant();
        ZoneId zoneIdToUse = ZoneId.systemDefault();
        LocalDate localDateToShow = instantToUse.atZone(zoneIdToUse).toLocalDate();
LocalTime :  只包括时间    LocalDate : 只包括日期         LocalDateTime : 包括日期和时间
LocalDateTime time = LocalDateTime.now();
System.out.println(time.toString()); //字符串表示
        System.out.println(time.toLocalTime()); //获取时间(LocalTime)
        System.out.println(time.toLocalDate()); //获取日期(LocalDate)
        System.out.println(time.getDayOfMonth()); //获取当前时间月份的第几天
        System.out.println(time.getDayOfWeek());  //获取当前周的第几天
        System.out.println(time.getDayOfYear());  //获取当前时间在该年属于第几天
        System.out.println(time.getHour());
        System.out.println(time.getMinute());
        System.out.println(time.getMonthValue());
        System.out.println(time.getMonth());
        System.out.println("-----------------------------------");
        //格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
        System.out.println(time.format(formatter));
        //构造时间
        LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);
        LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);
        //比较时间
        System.out.println(time.isAfter(startTime));
        System.out.println(time.isBefore(endTime));
 
        //时间运算,相加相减
        System.out.println(time.plusYears(2)); //加2年
        System.out.println(time.plusDays(2)); //加两天
        System.out.println(time.minusYears(2)); //减两年
        System.out.println(time.minusDays(2)); //减两天
 
        //获取毫秒数(使用Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        //获取秒数(使用Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());

猜你喜欢

转载自blog.csdn.net/zpflwy1314/article/details/91982934