zoukankan      html  css  js  c++  java
  • java_14 Date

    1.Date类的构造方法

      Date是表示时间的类
      空参构造
        public Date()

        public class Demo {
    	public static void main(String[] args) {
    		Date date = new Date();
    		System.out.println(date);
            }  
      }

      带参构造
        public Date(long times)

        public static void main(String[] args) {
    		Date date = new Date(30000);
    		System.out.println(date);
    	}
    

     
    2.Date类的get和set方法
        public long getTime()
        将当前的日期对象,转为对应的毫秒值

        public static void main(String[] args) {
    		Date date = new Date();
    		System.out.println(date);
    		long time = date.getTime();
    		System.out.println(time);
    	}
    

     
        public void setTime(long times);
       根据给定的毫秒值,生成对应的日期对象

        public static void main(String[] args) {
    		Date date = new Date();
    		System.out.println(date);
    		date.setTime(30000);
    		System.out.println(date);
    	}
    

     
    3.日期格式化SimpleDateFormat

        public static void main(String[] args) {
    		SimpleDateFormat s1 = new SimpleDateFormat();
    		String sd = s1.format(new Date());
    		System.out.println(sd);
    	}
    

        public static void main(String[] args) {
    		SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
    		String sd = s1.format(new Date());
    		System.out.println(sd);
    	}
    

    4.DateFormat类    

      parse  将字符串  解析为  日期的对象,即从1970年到输入的年与日的毫秒数

        public static void main(String[] args) throws ParseException {
    		SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
    		Date date = s1.parse("1993-6-23");
    		long time = date.getTime();
    		System.out.println(time);
    	}
    

     
    5 Calendar类

      Calendar 类是一个抽象类,所以不能直接new一个对象出来

      get()方法:获得当前日历的年月日

        public static void main(String[] args) {
    		Calendar c = Calendar.getInstance();	//不能直接new
    		int year = c.get(Calendar.YEAR);		//用Calendar.YEAR方法获得年
    		int month = c.get(Calendar.MONTH)+1;	//月份是从0开始计算的
    		int day = c.get(Calendar.DAY_OF_MONTH);	//获得日
    		System.out.println(year);
    		System.out.println(month);
    		System.out.println(day);
    	}
    

     

      set(int field,int value)方法:设置日历

        public static void main(String[] args) {
    		Calendar c = Calendar.getInstance();
    		c.set(2022, 4, 23);
    		int year = c.get(Calendar.YEAR);
    		int month = c.get(Calendar.MONTH);    //注意此处不加1
    		int day = c.get(Calendar.DAY_OF_MONTH);
    		System.out.println(year);
    		System.out.println(month);
    		System.out.println(day);
    	}
    

     

      add    日历偏移量

        public static void main(String[] args) {
    		Calendar c = Calendar.getInstance();
    		c.add(Calendar.DAY_OF_MONTH, 30);  //往后增加30天
    		int year = c.get(Calendar.YEAR);
    		int month = c.get(Calendar.MONTH)+1;
    		int day = c.get(Calendar.DAY_OF_MONTH);
    		System.out.println(year);
    		System.out.println(month);
    		System.out.println(day);
    	}
    

     

  • 相关阅读:
    深入浅出接口测试原理及步骤
    软件测试所需要掌握的技能
    Spring Boot(三)—— 自动装配原理
    Spring Boot(一)—— Spring Boot入门
    线程的六种状态
    有关于java中List.add方法进行添加元素,发生覆盖的问题
    《暗时间》读后感
    《基于UML的高校教务管理系统的设计与实现 》论文笔记(六)
    win7下硬盘安装ubuntu
    词频统计
  • 原文地址:https://www.cnblogs.com/smxbo/p/10666594.html
Copyright © 2011-2022 走看看