JAVA学习第十三章——常用API(五【时间】)

Date时间类

方法名说明
public Date()创建Date对象,表示当前时间
public Date(long date)创建Date对象,表示指定时间
public void setTime(long time)设置/修改毫秒值
public long getTime()获取时间对象的毫秒值
    public static void main(String[] args) {
        //1.创建对象表示一个时间
        Date d = new Date();
        System.out.println(d);
        System.out.println("---------------------------------");

        //2.创建对象表示一个指定的时间
        Date d2 = new Date(0L);
        System.out.println(d2);
        System.out.println("---------------------------------");

        //3.setTime修改时间
        d2.setTime(1000L);
        System.out.println(d2);
        System.out.println("---------------------------------");

        //4.getTime获取当前时间的毫秒值
        long time = d2.getTime();
        System.out.println(time);

    }

时间计算

需求1:打印时间原点开始一年之后的时间

    public static void main(String[] args) {
        //创建对象,表示时间原点
        Date d1 = new Date(0);

        //获取d1时间的毫秒值
        long time = d1.getTime();

        //一年的毫秒值
        time = time + 1000L * 60 * 60 * 24 *365;

        d1.setTime(time);
        System.out.println(d1);

    }

需求2:定义任意两个Date对象,比较一下哪个时间在前,哪个时间在后

    public static void main(String[] args) {

        Random r = new Random();

        Date d1 = new Date(Math.abs(r.nextInt()));
        Date d2 = new Date(Math.abs(r.nextInt()));
        long time1 = d1.getTime();
        long time2 = d2.getTime();
        System.out.println(d1);
        System.out.println(d2);

        if(time1 > time2){
            System.out.println("第一个时间在后面,第二个时间在前面");
        }else if(time1 < time2){
            System.out.println("第一个时间在前面,第二个时间在后面");
        }else {
            System.out.println("表示两个时间一样");
        }

    }

SimpleDateFormat类

构造方法说明
public SimpleDateFormat()构造一个SimpleDateFormat,使用默认格式
public SimpleDateFormat(String pattern)构造一个SimpleDateFormat,使用指定的格式
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date d = new Date(0L);
        String str = sdf.format(d);
        System.out.println(str);
        System.out.println("----------------------------------");

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EE");
        String str2 = sdf2.format(d);
        System.out.println(str2);
    }
    public static void main(String[] args) throws ParseException {
        String str = "2023-11-11 11:11:11";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(str);
        System.out.println(date.getTime());
    }
常用方法说明
public final String format(Date date)格式化(日期对象 -> 字符串)
public Date parse(String source)解析(字符串 -> 日期对象)

练习

假设,出生年月日为:2000-11-11
请用字符串表示这个数据,并将其转换为:2000年11月11日

    public static void main(String[] args) throws ParseException {
        //假设,出生年月日为:2000-11-11
        //请用字符串表示这个数据,并将其转换为:2000年11月11日
        String str = "2000-11-11";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        String result = sdf1.format(date);
        System.out.println(result);
    }

需求:
秒杀活动:2023年11月11日 0:0:0 (毫秒值)
开始时间:2023年11月11日 0:10:0(毫秒值)

小贾下单并付款的时间为:2023年11月11日 0:01:00
小皮下单并付款的时间为:2023年11月11日 0:11:0
用代码说明这两位同学有没有参加上秒杀活动

    public static void main(String[] args) throws ParseException {
        //1.定义三个时间
        String startStr = "2023年11月11日 0:0:0";
        String endStr = "2023年11月11日 0:10:0";
        String orderStr = "2023年11月11日 0:01:00";
        
        //2.解析时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date startDate = sdf.parse(startStr);
        Date endDate = sdf.parse(endStr);
        Date orderDate = sdf.parse(orderStr);
        
        //3.转化为毫秒值
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        long orderTime = orderDate.getTime();
        
        //4.判断
        if(orderTime >= startTime && orderTime <= endTime){
            System.out.println("参加成功");
        }else {
            System.out.println("参加失败");
        }
    }

Calendar

方法名说明
public static Calendar getInstance()获取当前时间的日历对象
public final Date getTime()获取日期对象
public final setTime(Date date)给日历设置日期对象
public long getTimeInMillis()拿到时间毫秒值
public void setTimeInMillis(long millis)给日历设置时间毫秒值
public int get(int field)取日历中的某个字段信息
public void set(int field,int value)修改日历的某个字段信息
public void add(int field,int amount)为某个字段增加/减少指定的值
    public static void main(String[] args) {
        //1.获取日历对象
        Calendar c = Calendar.getInstance();

        Date d = new Date(0L);
        c.setTime(d);

        c.set(Calendar.YEAR,2023);
        c.set(Calendar.MONTH,8);
        c.set(Calendar.DAY_OF_MONTH,10);

        c.add(Calendar.MONTH,1);

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DAY_OF_MONTH);
        int week = c.get(Calendar.DAY_OF_WEEK);
        System.out.println(year + ", " + month + ", " + date + ", " + getWeek(week));
    }

    public static String getWeek(int index){
        String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        return arr[index];
    }

Zoneld时区

方法名说明
static Set< String > getAvailableZoneIds()获取Java中支持的所有时区
static ZoneId systemDefault()获取系统默认时区
static ZoneId of(String zoneId)获取一个指定时区
    public static void main(String[] args) {
        //1.获取所有的时区名称
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds.size());
        System.out.println(zoneIds);

        //2.获取当前系统的默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);

        //3.获取指定的时区
        ZoneId id = ZoneId.of("America/Marigot");
        System.out.println(id);
    }

Instant时间戳

方法名说明
static Instant now()获取当前时间的Instant对象(标准时间)
static Instant ofXxxx(long epochMilli)根据(秒/毫秒/纳秒)获取Instant对象
ZonedDateTime atZone(ZoneId zone)指定时区
boolean isXxx(Instant otherInstant)判断系列的方法
Instant minusXxx(long millisToSubtract)减少时间系列的方法
Instant plusXxx(long millisToSubtract)增加时间系列的方法
    public static void main(String[] args) {
        //1.获取当前时间的Instant对象
        Instant now = Instant.now();
        System.out.println(now);
        System.out.println("----------------------------------");

        //2.指定时间
        Instant instant = Instant.ofEpochMilli(0L);
        System.out.println(instant);
        System.out.println("----------------------------------");

        Instant instant1 = Instant.ofEpochSecond(1L);
        System.out.println(instant1);
        System.out.println("----------------------------------");

        Instant instant2 = Instant.ofEpochSecond(1L, 1000000000L);
        System.out.println(instant2);
        System.out.println("----------------------------------");

        //3.指定时区
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(time);
        System.out.println("----------------------------------");

        //4.判断
        //isBefore:判断调用者代表的时间是否在参数表示时间的前面
        Instant instant3 = Instant.ofEpochMilli(0L);
        Instant instant4 = Instant.ofEpochMilli(1000L);
        boolean result1 = instant3.isBefore(instant4);
        System.out.println(result1);
        System.out.println("----------------------------------");

        boolean result2 = instant3.isAfter(instant4);
        System.out.println(result2);
        System.out.println("----------------------------------");

        //5.减少时间
        Instant instant5 = Instant.ofEpochMilli(3000L);
        System.out.println(instant5);

        Instant instant6 = instant5.minusSeconds(1);
        System.out.println(instant6);
        System.out.println("----------------------------------");
    }

ZoneDateTime带时区的时间

方法名说明
static ZonedDateTime now()获取当前时间的ZonedDateTime对象
static ZonedDateTime ofXxx()获取指定时间的ZonedDateTime对象
ZonedDateTime withXxx(时间)修改时间系列的方法
ZonedDateTime minusXxx(时间)减少时间系列的方法
ZonedDateTime plusXxx(时间)增加时间系列的方法
    public static void main(String[] args) {
        //1.获取当前时间对象(带时区)
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
        System.out.println("-----------------------------------------");

        //2.获取指定的时间对象(带时区)
        ZonedDateTime time1 = ZonedDateTime.of(2023,10,1,11,12,12,0, ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);
        System.out.println("-----------------------------------------");

        //3.修改时间系列的方法
        ZonedDateTime time3 = time1.withYear(2000);
        System.out.println(time3);
        System.out.println("-----------------------------------------");

        //4.减少时间
        ZonedDateTime time4 = time3.minusYears(1);
        System.out.println(time4);
        System.out.println("-----------------------------------------");

        //5.增加时间
        ZonedDateTime time5 = time4.plusYears(10);
        System.out.println(time5);
    }

DateTimeFormatter

方法名说明
static DateTimeFormatter ofPattern(格式)获取格式对象
String format(时间对象)按照指定方式格式化
    public static void main(String[] args) {
        //获取时间对象
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

        //解析/格式化器
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");

        //格式化
        System.out.println(dtf1.format(time));
    }

LocalDate、LocalTime、LocalDateTime

方法名说明
static XXX now()获取当前时间的对象
static XXX of()获取指定时间的对象
get开头的方法获取日历中的年、月、日、时、分、秒等信息
isBefore,isAfeter比较两个LocalDate
with开头的修改时间系列的方法
minus开头的减少时间系列的方法
plus开头的增加时间系列的方法
public LocalDate toLocalDate()LocalDateTime转换成一个LocalDate对象
public LocalTime toLocalTime()LocalDateTime转换成一个LocalTime对象
    public static void main(String[] args) {
        //1.获取当前时间的日历对象(包含:年月日)
        LocalDate nowDate = LocalDate.now();
        System.out.println("今天的日期:" + nowDate);
        System.out.println("----------------------------------");

        //2.获取指定的时间的日历对象
        LocalDate ldDate = LocalDate.of(2023, 1, 1);
        System.out.println("指定日期:" + ldDate);
        System.out.println("----------------------------------");

        //3.get系列方法获取日历中的每一个属性值
        int year = ldDate.getYear();
        System.out.println("year:" + year);
        System.out.println("----------------------------------");

        //获取月
        //方式一
        Month m = ldDate.getMonth();
        System.out.println(m);
        System.out.println(m.getValue());
        System.out.println("----------------------------------");

        //方式二
        int month = ldDate.getMonthValue();
        System.out.println("month:" + month);
        System.out.println("----------------------------------");

        //获取日
        int day = ldDate.getDayOfMonth();
        System.out.println("day:" + day);
        System.out.println("----------------------------------");

        //获取一年的第几天
        int dayOfYear = ldDate.getDayOfYear();
        System.out.println("dayOfYear:" + dayOfYear);
        System.out.println("----------------------------------");

        //获取星期
        DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
        System.out.println(dayOfWeek);
        System.out.println(dayOfWeek.getValue());
        System.out.println("----------------------------------");

        //is开头的方法表示判断
        System.out.println(ldDate.isBefore(ldDate));
        System.out.println(ldDate.isAfter(ldDate));
        System.out.println("----------------------------------");

        //with开头的方法表示修改,只能修改年月日
        LocalDate withLocalDate = ldDate.withYear(2000);
        System.out.println(withLocalDate);
        System.out.println("----------------------------------");

        //minus开头的方法表示减少,只能减少年月日
        LocalDate minusYears = ldDate.minusYears(1);
        System.out.println(minusYears);
        System.out.println("----------------------------------");

        //plus开头的方法表示增加,只能增加年月日
        LocalDate plusYears = ldDate.plusYears(10);
        System.out.println(plusYears);
        System.out.println("----------------------------------");
    }

Tips
以上学习内容均来自于B站黑马程序员

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值