新Date localdate localtime localdatetime zonelddatetime instant datetimeformar period dura 27

 

 localdate

package Api;

import java.time.LocalDate;

public class local1 {
    public static void main(String[] args) {
        LocalDate ld= LocalDate.now();
        System.out.println(ld);
        //1获取日期对象的信息
        int year = ld.getYear();//年
        int month = ld. getMonthValue(); //月1-12
        int day = ld.getDayOfMonth();//日
        int day0fYear = ld.getDayOfYear();//一年中的第几天
        int day0fWeek = ld . getDayOfWeek(). getValue(); //星期几
        System. out . println(year);
        System. out . println(day);
        System. out . println(day0fWeek);
        // 2、直接修改某个信息: withYear、 withMonth、 withDayOfMonth、 withDayOfYear
        LocalDate ld2 = ld. withYear(2099);
        LocalDate ld3 = ld.withMonth(12);
        System. out . println(ld2);
        System. out . println(ld3);
        System. out. println(ld);

     // 3、把某个信息加多少: plusYears、 plusMonths、plusDays、 plusWeeks
        LocalDate ld4 = ld. plusYears(2);
        LocalDate ld5 = ld. plusMonths(2);

// 4、把某个信息减多少: minusYears. minusMonths、 minusDays. minusWeeks
         LocalDate ld6 = ld. minusYears(2);
        LocalDate ld7 = ld. minusMonths(2);

//5. 获得指定日期的LocalDate对象: public static LocalDate of(int year, int month,intdate)
      LocalDate ld8 = LocalDate.of(2033,11,14);
        LocalDate ld9 = LocalDate.of(2022,12,24);

// 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfter
        System. out . println(ld8. equals(ld9));// true
        System. out . println(ld8. isAfter(ld)); // true
        System. out. println(ld8.isBefore(ld)); //falsel

    }

}
2024-07-25
2024
25
4
2099-07-25
2024-12-25
2024-07-25
false
true
false

localtime

package Api;

import java.time.LocalTime;

public class local2 {
    public static void main(String[] args) {
        //日、获取本地时间对象
        LocalTime lt = LocalTime.now(); //获取当前 时,分,秒,纳秒
        System. out . println(lt);
     // 1、获取时间中的信息
        int hour = lt. getHour(); //时
        int minute = lt. getMinute(); //分
        int second = lt. getSecond(); //秒
        int nano = lt. getNano(); //纳秒

       // 2, 修改时间: wi thHour. withMinute, wi thSecond, wi thNano
        LocalTime lt3 = lt. withHour(10) ;
        LocalTime lt4 = lt. withMinute(10);
        LocalTime lt5 = lt. withSecond(10);
        LocalTime lt6 = lt. withNano(10);

        //加多少: plusHours, plusMinutes, plusSeconds, plusNanos
        LocalTime lt7 = lt. plusHours(10);
        LocalTime lt8 = lt. plusMinutes(10);
        LocalTime lt9 = lt. plusSeconds(10);
        LocalTime lt10 = lt. plusNanos(10);

        // 4. 8b: minusHours. minusMinutes. minusSeconds, minusNanos
        LocalTime lt11 = lt . minusHours(10);
        LocalTime lt12 = lt. minusMinutes(10);
        LocalTime lt13 = lt. minusSeconds(10);
        LocalTime lt14 = lt. minusNanos(10);

        //5.获取指定时间的localtime对象
       // public static LocalTime of(int hour, int minute, int second)
         LocalTime lt15 = LocalTime.of(11,33,33);
        LocalTime lt16 = LocalTime.of(11,11,11);

       // 6、判断2个时间对象,是否相等, 在前还是在后: equals isBefore isAfter
        System. out. println(lt15. equals(lt16));
        System. out . println(lt15. isAfter(lt));
        System. out . println(lt15. isBefore(lt));
    }
}
14:23:39.197
false
false
true

localdatetime

package Api;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class local3 {
    public static void main(String[] args) {
        LocalDateTime ldt=LocalDateTime.now();//年,月,日。时,分。秒。纳秒,
        System.out.println(ldt);
        //加时间。修改时间。。。。。都一样
        // 7。可以把localdatetime转为localdate,loacltime
       // public LocalDate toLocalDate()
      //public LocalTime toLocalTime()

      //public static LocalDateTime of(LocalDate date, LocalTime time)
        LocalDate ld = ldt. toLocalDate();
        LocalTime lt = ldt. toLocalTime();
        LocalDateTime ldt10 = LocalDateTime .of(ld, lt);

    }
}

zonelddatetime

package Api;

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class local4 {
    public static void main(String[] args) {
        //目标:了 解时区和带时区的时间。
       // 1、ZoneId的常見方法:

        // public static ZoneId systemDefault(): #HZ É#iÁJHfE
        ZoneId zoneId = ZoneId. systemDefault();
        System. out . println(zoneId.getId());
        System. out . println(zoneId);//一样

         // public static Set<String> getAvailableZoneIds(): 获取java支持的全部时区id
         System. out . println(ZoneId. getAvailableZoneIds());

        // public static ZoneId of(String zoneId) : 把某个时区id封装成zoneldid对象n
        ZoneId zonedid1= ZoneId.of("America/Cuiaba");

        // 2, ZonedDateTime:带时区的时间
        // public static ZonedDateTime now(ZoneId zone):获取某个时区的zonelddatetime对象
        ZonedDateTime now=ZonedDateTime.now(zonedid1);

        //世界标准时间
        ZonedDateTime now1=ZonedDateTime.now(Clock.systemUTC());
        System.out.println(now1);
        //piblic static zonedatetime now()获取默认时区的zoned达特time对象
        ZonedDateTime now2 =ZonedDateTime.now();
        System.out.println(now2);
        //Calendar instance=Calendar.getInstance(TimeZone.getTimeZone(zonedid1));
    }

}
Asia/Shanghai
Asia/Shanghai
[Asia/Aden,。。。。。。。。。。。。。。]
2024-07-25T07:14:07.678Z
2024-07-25T15:14:07.678+08:00[Asia/Shanghai]

 instant

datetimeformar

pepp

package Api;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class local6 {
    public static void main(String[] args) {
        //1创建一个日期时间格式化器对象出来
        DateTimeFormatter now1 =DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        //2对时间进行格式化
        LocalDateTime now2=LocalDateTime.now();
        System.out.println(now2);
        String r= now1.format(now2);//正向格式化
        System.out.println(r);
        //另一种
        String d= now2.format(now1);
        System.out.println(d);
        //解析时间,一般使用 loacldatetime
        String s="2024年07月25日 17:39:41";
        LocalDateTime g= LocalDateTime.parse(s,now1);
        System.out.println(g);
    }
}
2024-07-25T17:42:22.614
2024年07月25日 17:42:22
2024年07月25日 17:42:22
2024-07-25T17:39:41

 period

duration

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值