Java时间类 (Date、SimplDateFormat、calland)

Date 类

  • 主要用途
    获取当前系统时间。

  • 构造方法
    格式:Date d = new Date();

    public class Demo {
        public static void main(String[] args) {
            Date d1 = new Date();//无参构造
            System.out.println(d1);
            
            Date d2 = new Date((1000*60*60));//带参构造
            System.out.println(d2);
        }
    }
    ---*---
    输出结果:
    Sun Apr 18 20:50:09 CST 2021 // 4月 18 20:50:09 中国时区 2021
    Thu Jan 01 09:00:00 CST 1970 // 1月 01 09:00:00 中国时区 1970
    

    需要注意的是,对象创建时会记录当前时间至 1970年1月1日00:00:00 之间的毫秒值到对象中。由于此类重写了toString方法,所以会根据毫秒值输出现在的系统时间,而不是直接输出毫秒值。

  • 常用方法 getTime() / setTime()

    public class Demo {
        public static void main(String[] args)  {
            Date d1 = new Date();
            long l1 = d1.getTime();//获取当前时间至 1970-1-1 00:00:00 的毫秒值
            System.out.println(l1);
    
            long l2 = 1000*60*60*24;
            d1.setTime(l2);			//设置时间
            System.out.println(d1);
        }
    }
    ---*---
    输出结果:
    1618752504108
    Fri Jan 02 08:00:00 CST 1970
    

SimplDateFormat 类

  • 主要用途
    日期格式和日期解析

  • 构造方法
    格式:SimplDateFormat sdf = new SimplDateFormat(“现在的时间是:yyyy年MM月dd日 HH:mm:ss”);

    public class Demo {
        public static void main(String[] args)  {
            Date d1 = new Date();
            //无参构造
            SimpleDateFormat fmt1 = new SimpleDateFormat();
            System.out.println(fmt1.format(d1));
            //带参构造
            SimpleDateFormat fmt2 = new SimpleDateFormat("现在的时间是:yyyy年MM月dd日 HH:mm:ss");
            System.out.println(fmt2.format(d1));
        }
    }
    ---*---
    输出结果:
    2021/4/18 下午10:05 //默认格式
    现在的时间是:2021041822:0520 //自定义格式
    

    日期和时间格式由模式字符串指定的,我们需要了解一下下面常用的有特殊含义的字符。凡是出现下列字符的时候就会把对应的字符替换成日期。(M和MM的区别:3月 / 03月)

    字符描述
    y (小写)
    M (大写)
    d (小写)
    H (大写)时(0-23)
    m (小写)
    s (小写)
    E (大写)星期
    a(小写)上午/下午
  • 常用方法 format() / parse()

    public class Demo {
        public static void main(String[] args) throws ParseException {
            Date d1 = new Date();
            SimpleDateFormat fmt1 = new SimpleDateFormat("y年M月d日 HH:mm:ss");
            //format()方法将日期对象设置成指定格式的字符串
            String s = fmt1.format(d1);
            System.out.println(s);
    
            String ss = "2099年1月1日 12:12:12";
            //parse()方法将格式和自己匹配的字符串解析成日期对象
            Date d2 = fmt1.parse(ss);
            System.out.println(d2);
        }
    }
    ---*---
    输出结果:
    202141823:14:54
    Thu Jan 01 12:12:12 CST 2099
    

    format():给一个日期对象,输出自定义格式字符串。
    parse():日期子字符串,解析成日期对象。(这里涉及到异常:字符串和SimplDateFormat对象格式不匹配)


Calendar 类

  • 主要用途
    获取日期,设置日期

  • 构造方法

    public class Demo {
        public static void main(String[] args) {
            Calendar cld = Calendar.getInstance();//通过getInstance()方法创建对象
        }
    }
    ---*---
    演示类
    

    由于Calenda类是抽象类,所以不能直接 new 。需通过其类方法 getInstance() 获取对象。(获取到对象后对象中即存在当前系统日历的字段值)

  • 常用方法
    1.get() 返回当前对象的日历字段的值

    public class Demo {
        public static void main(String[] args) {
            Calendar cld = Calendar.getInstance();
            int y = cld.get(Calendar.YEAR);
            int minth = cld.get(Calendar.MONTH) + 1;
            int d = cld.get(Calendar.DAY_OF_MONTH);
            int h = cld.get(Calendar.HOUR);
            int m = cld.get(Calendar.MINUTE);
            int s = cld.get(Calendar.SECOND);
            System.out.println(y + "年" + minth + "月" + d + "日" + h + ":" + m + ":" + s );
        }
    }
    ---*---
    输出结果:
    20214190:1957
    

    注意:月份是从 0 开始计数的,也就是说当前系统月份为2的话 Calendar.MONTH 返回 1 ,最大天数29 。1就代表2月,所以我们打印输出给人看的时候需要在月份上加1。

    2.add() 修改日历日期

    public class Demo {
        public static void main(String[] args) {
    
            Calendar cld = Calendar.getInstance();
            //设置一个20年前的一个月后的5天后的日期
            cld.add(Calendar.YEAR,-20);
            cld.add(Calendar.MONTH,1);
            cld.add(Calendar.DAY_OF_MONTH,5);
            
    		//获取设置后的日历字段
            System.out.println(cld.get(Calendar.YEAR));
            System.out.println(cld.get(Calendar.MONTH)+1);
            System.out.println(cld.get(Calendar.DAY_OF_MONTH));
        }
    }
    ---*---
    输出结果:
    2001 //2021
    5	 //4
    24	 //19
    

    很明显,用 add() 方法可以修改日历日期。

    3.set() 设置日历日期

    public class Demo {
        public static void main(String[] args) {
    
            Calendar cld = Calendar.getInstance();
            //设置一个日期
            cld.set(2021,3,19,0,48,50);
            //获取年月日时分秒
            int y = cld.get(Calendar.YEAR);
            int minth = cld.get(Calendar.MONTH) + 1;
            int d = cld.get(Calendar.DAY_OF_MONTH);
            int h = cld.get(Calendar.HOUR);
            int m = cld.get(Calendar.MINUTE);
            int s = cld.get(Calendar.SECOND);
            //打印输出
            System.out.println(y + "年" + minth + "月" + d + "日" + h + ":" + m + ":" + s );
        }
    }
    ---*---
    输出结果:
    20214190:4850
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值