黑马JAVA P121 时间日期:Date、SimpleDateformat、Calendar

 

 

package com.itheima.d1_date;

import java.util.Date;

/**
 * 目标:学会使用Date类处理时间,获取时间的信息
 */
public class DateDDemo1 {
    public static void main(String[] args) {
        //1.创建一个Date类的对象:代表系统此刻日期时间对象
        Date d = new Date();
        System.out.println(d);

        //2.获取时间毫秒值
        long time = d.getTime();
        System.out.println(time);
//        long time1 = System.currentTimeMillis();
//        System.out.println(time1);

        System.out.println("--------");
        //1.得到当前时间
        Date d1 = new Date();
        System.out.println(d1);

        //2.当前时间往后走1小时  121s
        long time2 = System.currentTimeMillis();
        time2 +=  (60 * 60 + 121) * 1000;

//        //3.把时间毫秒值转换成对应的日期对象。
//        Date d2 = new Date(time2);
//        System.out.println(d2);

        Date d3 = new Date();
        d3.setTime(time2);
        System.out.println(d3);

    }
}

 

 

 

 

 

 

 

 

package com.itheima.d2_simpledateformat;

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

/**
 * 目标:SimpleDateFormat简单日期格式化类的使用
 * 格式化时间
 * 解析时间
 */
public class SimpleDateFormatDemo01 {
    public static void main(String[] args) {
        //1.日期对象
        Date d = new Date();
        System.out.println(d);

        //2.格式化这个日期对象(指定最终格式化的形式)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
        //3.开始格式化日期对象成为喜欢的字符串形式
        String rs = sdf.format(d);
        System.out.println(rs);

        System.out.println("------------");

        //4.格式化时间毫秒值
        // 需求:请问121秒后的时间是多少
        long time1 = System.currentTimeMillis() + 121 * 1000;
        String rs2 = sdf.format(time1);
        System.out.println(rs2);

        System.out.println("--------解析字符串时间,下个代码----------");

    }
}

 

 

 

package com.itheima.d2_simpledateformat;

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

public class SimpleDateFormatDemo02 {
    public static void main(String[] args) throws ParseException {
        //目标:学会使用SimpleDateFormat 解析字符串时间成为日期对象。
        // 有一个时间  2021年08年06日 11:11:11 往后2天 14 小时 49 分 06 秒后的时间是多少
        // 1. 把字符串时间拿到程序中来
        String dateStr = "2021年08月06日 11:11:11";

        //2.把字符串时间解析成日期对象(本节的重点):形式必须与被解析时间的形式完全一样,否则运行时解析报错!
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date d = sdf.parse(dateStr);

        //3.往后走2天 14小时 49分 06 秒
        long time = d.getTime() + (2L*24*60*60 + 14*60*60 + 49*60 + 6) * 1000;

        //4.格式化这个时间毫秒值就是结果
        System.out.println(sdf.format(time));
    }
}

 

 

package com.itheima.d2_simpledateformat;

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

public class SimpleDateFormatDemo03 {
    public static void main(String[] args) throws ParseException {
        //1.开始 和 结束时间
        String startTime = "2021-11-11 00:00:00";
        String endTime = "2021-11-11 00:10:00";

        //2. 小贾 小皮

        String xiaojia = "2021-11-11 00:03:47";
        String xiaopi = "2021-11-11 00:10:11";

        //3.解析他们的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = sdf.parse(startTime);
        Date d2 = sdf.parse(endTime);
        Date d3 = sdf.parse(xiaojia);
        Date d4 = sdf.parse(xiaopi);

        if(d3.after(d1) && d3.before(d2)){
            System.out.println("小贾秒杀成功,可以发货了!");
        }else{
            System.out.println("小贾秒杀失败!");
        }

        if(d4.after(d1) && d4.before(d2)){
            System.out.println("小皮秒杀成功,可以发货了!");
        }else{
            System.out.println("小皮秒杀失败!");
        }
    }
}

 

 

 

 

package com.itheima.d3_calendar;

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

public class CalendarDemo01 {
    public static void main(String[] args) {
        //1.拿到系统冲此刻日历对象
        Calendar cal = Calendar.getInstance();
        System.out.println(cal);

        //2.获取日历信息:public int get(int field): 取日期中的某个字段信息。
        int year = cal.get(Calendar.YEAR);
        System.out.println(year);
        int mm = cal.get(Calendar.MONTH) + 1;
        System.out.println(year);
        int days = cal.get(Calendar.DAY_OF_YEAR);
        System.out.println(days);

//        public void set(int field,int value): 修改日历的某个信息。
//        cal.set(Calendar.HOUR , 12);
//        System.out.println(cal);

//        public void add(int field, int amount): 为某个字段增加、减少指定的值
          //请问64天后是什么时间
        cal.add(Calendar.DAY_OF_YEAR, 64);
        cal.add(Calendar.MINUTE , 59);

        //5.public final Date getTime(): 拿到此刻日期对象。
        Date d = cal.getTime();
        System.out.println(d);

        //6.public long getTimeInMillis(): 拿到此刻时间毫秒值
        long time = cal.getTimeInMillis();
        System.out.println(time);


    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值