常用API(一):秒杀活动 Calendar

package com.itheima.秒杀活动;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class aa {
    public static void main(String[] args) throws ParseException {
        String start="2023年11月11日 0:0:0";
        String end="2023年11月11日 0:10:0";
        String xj="2023年11月11日 0:1:18";
        String xp="2023年11月11日 0:10:57";
        SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date start1= s.parse(start);
        Date end1= s.parse(end);
        Date xj1= s.parse(xj);
        Date xp1= s.parse(xp);
        long starttime=start1.getTime();
        long endtime=end1.getTime();
        long xjtime=xj1.getTime();
        long xptime=xp1.getTime();
        if(xjtime>=starttime&& xjtime<=endtime){
            System.out.println("秒杀成功");
        }
        else {

            System.out.println("秒杀失败");
        }
        if(xptime>=starttime&& xptime<=endtime){
            System.out.println("秒杀成功");
        }
        else {

            System.out.println("秒杀失败");
        }
    }
}

2.Calendar

 

package com.itheima.秒杀活动;

import java.util.Calendar;
import java.util.Date;

public class calender {
    public static void main(String[] args) {
        //拿日期
        Calendar now=Calendar.getInstance();
        System.out.println(now);
        //拿日期中的对象
        int year=now.get(Calendar.YEAR);
        System.out.println(now.get(Calendar.YEAR));
        int month=now.get(Calendar.MONTH)+1;
        System.out.println(month);
        //拿日期中的日期对象
        Date n=now.getTime();
        System.out.println(n);
        //修改日历中的某个信息
        now.set(Calendar.MONTH,4);
        System.out.println(now);

        //为某个信息增加或减少多少
        now.add(Calendar.DAY_OF_MONTH,2);
        System.out.println(now);

        now.add(Calendar.DAY_OF_MONTH,-2);
        System.out.println(now);
        //拿到时间毫秒值
        long time=now.getTimeInMillis();
        System.out.println(time);
    }
}

 

posted @ 2024-04-06 20:25  小彭先森  阅读(3)  评论(0编辑  收藏  举报