美文网首页工作生活
5.Java----日期类

5.Java----日期类

作者: _麻辣香锅不要辣 | 来源:发表于2019-07-15 16:27 被阅读0次
    JDK1.8之前
    使用Date
    System.out.println(System.currentTimeMillis());
            //java.util.date
            Date date = new Date();
            System.out.println(date);
            System.out.println(date.toString());
            System.out.println(date.getTime());
            //java.sql.date
            java.sql.Date date1 = new java.sql.Date(date.getTime());
            System.out.println(date1);
            System.out.println(date1.toString());
            System.out.println(date1.getTime());
    
    使用SimpleDateFormat来格式化Date
           //simpledateformat
            SimpleDateFormat sdf = new SimpleDateFormat();
            //格式化日期
            String str_date = sdf.format(new Date());
            System.out.println(str_date);
            //解析
            Date date2 = sdf.parse(str_date);
            System.out.println(date2);
    
            //按指定格式格式化
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            String str_date2 = sdf2.format(new Date());
            System.out.println(str_date2);
            //解析
            Date date3 = sdf2.parse(str_date2);
            System.out.println(date3);
    
    Calendar类
    @Test
        public void CalenderTest(){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(99,10,31));
            //getTime()
            System.out.println(calendar.getTime());
            //
            System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//周日为1,周一为2
            System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
            //
            calendar.set(Calendar.DAY_OF_YEAR,1);
            System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
    
            calendar.add(Calendar.DAY_OF_YEAR,10);
            System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
    
            //localtime localdate localdatetime
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);
        }
    

    Calender是一个抽象类,无法直接new一个对象。使用Calendar.getInstance()或者 new GregorianCalendar()来创建

    JDK1.8中

    JDK1.8日期使用

    相关文章

      网友评论

        本文标题:5.Java----日期类

        本文链接:https://www.haomeiwen.com/subject/weyxhctx.html