日期类:Date、SimpleDateForma和Calendar

  • Post author:
  • Post category:其他




1.Date类

  • Date类概述

    ​ Date 代表了一个特定的时间,精确到毫秒,得到毫秒值表示1970年1月1日00:00:00.000 GMT之后的毫秒数。

  • 构造方法

    • public Date()

      分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 。
    • public Date(long date)

      分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数。
//public Date()
Date d1 = new Date();

//public Date(long date)
long date = 1000*60*60;
Date d2 = new Date(date);
  • 常用方法

    • public long getTime()

      获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
    • public void setTime(long time)

      设置时间,给的是毫秒值
 		//创建日期对象
        Date d = new Date();
        System.out.println(d.getTime());
        //1秒=1000毫秒
        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
        
		//currentTimeMillis() 返回当前时间(以毫秒为单位)
		long time = System.currentTimeMillis();
        d.setTime(time);

        System.out.println(d);



2.SimpleDateFormat类

  • SimpleDateFormat类概述

    SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

  • 构造方法

    • public SimpleDateFormat()

      构造一个SimpleDateFormat,使用默认模式和日期格式
    • public SimpleDateFormat(String pattern)

      构造一个SimpleDateFormat使用给定的模式和默认的日期格式
  • 常用方法

    • 格式化(从Date到String)

      • public final String format(Date date):将日期格式化成日期/时间字符串
    • 解析(从String到Date)

      • public Date parse(String source):从给定字符串的开始解析文本以生成日期
 //格式化:从 Date 到 String
 Date d = new Date();
 //public   SimpleDateFormat()
SimpleDateFormat sdf1 = new SimpleDateFormat();
// public SimpleDateFormat(String pattern)
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf2.format(d);
System.out.println(s);

//从 String 到 Date
String ss = "2048-08-09 11:11:11";
 //ParseException
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date da = sdf3.parse(ss);
System.out.println(dd);



3.Calendar类

  • Calendar类概述

    • Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
    • Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。
    • 该方法返回一个Calendar 对象。
    • 其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();
  • 常用方法
方法名 说明
public int get(int field) 返回给定日历字段的值
public abstract void add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段
public final void set(int year,int month,int date) 设置当前日历的年月日

示例

//获取日历类对象
Calendar c = Calendar.getInstance();
//public int get(int field)
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");

 //public abstract void add(int field, int amount)
 //需求:3年前的今天
 c.add(Calendar.YEAR,-3);
 year = c.get(Calendar.YEAR);
 month = c.get(Calendar.MONTH) + 1;
 date = c.get(Calendar.DATE);
 System.out.println(year + "年" + month + "月" + date + "日");
 
 //public final void set(int year,int month,int date)
 c.set(2050,10,10);
 year = c.get(Calendar.YEAR);
 month = c.get(Calendar.MONTH) + 1;
 date = c.get(Calendar.DATE);
 System.out.println(year + "年" + month + "月" + date + "日");

案例:获取任意一年的二月有多少天

		 //键盘录入任意的年份
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年:");
        int year = sc.nextInt();

        //设置日历对象的年、月、日
        Calendar c = Calendar.getInstance();
        c.set(year, 2, 1);

        //3月1日往前推一天,就是2月的最后一天
        c.add(Calendar.DATE, -1);

        //获取这一天输出即可
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年的2月份有" + date + "天");



应用

  • 求两个日期之间相隔的天数

    例如:写一个方法(fun3(“2010-09-20”,“2010-09-21”) ) 结果为1天

代码

public static int  fun3(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //其日历字段已使用当前日期和时间初始化
        Calendar ca = Calendar.getInstance();

        //parse()  从给定字符串的开始解析文本以生成日期
        //setTime() 使用给定的 Date设置此日历的时间。
        ca.setTime(sdf.parse(start));

        //getTimeInMillis()  从给定的长值设置此日历的当前时间。
        long time1 = ca.getTimeInMillis();
        ca.setTime(sdf.parse(end));
        long time2 = ca.getTimeInMillis();
        long days = (time2 - time1) / (1000 * 60 * 60 * 24);

        return Integer.parseInt(String.valueOf(days));
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(fun3("2010-09-8","2010-09-21"));

    }
  • 请使用日期时间相关的API,计算出一个人已经出生了多少天。

代码

 public static int  fun3(String start) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //其日历字段已使用当前日期和时间初始化
        Calendar ca = Calendar.getInstance();

        //parse()  从给定字符串的开始解析文本以生成日期
        //setTime() 使用给定的 Date设置此日历的时间。
        //getTimeInMillis()  从给定的长值设置此日历的当前时间。
        ca.setTime(sdf.parse(start));
        long time1 =ca.getTimeInMillis();

//        ca.setTime(sdf.parse(end));
//        long time2 = ca.getTimeInMillis();
        //获取当前时间
//        Date d=new Date();
//        long time2=d.getTime();
        //获取当前时间
        long time2=System.currentTimeMillis();
        long days = (time2 - time1) / (1000 * 60 * 60 * 24);

        return Integer.parseInt(String.valueOf(days));
    }


    public static void main(String[] args) throws ParseException {
    	 Scanner sc = new Scanner(System.in);
        System.out.println("请输入出生日期:");
        int year = sc.nextInt();
        System.out.println(fun3(year+"天");
        }
  • 验证for循环打印数字1-9999所需要使用的时间(毫秒)

代码

		long start = System.currentTimeMillis();
        for (int i = 1; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();

        System.out.println("打印数字1-9999所需要使用的时间"+(end - start)+"毫秒");

  • 求出今天距离2023年1月1日还有多少天

代码

public static int  fun3(String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //其日历字段已使用当前日期和时间初始化
        Calendar ca = Calendar.getInstance();

        //parse()  从给定字符串的开始解析文本以生成日期
        //setTime() 使用给定的 Date设置此日历的时间。
        //getTimeInMillis()  从给定的长值设置此日历的当前时间。
        ca.setTime(sdf.parse(end));
        long time1 =ca.getTimeInMillis();

//        ca.setTime(sdf.parse(end));
//        long time2 = ca.getTimeInMillis();
        //获取当前时间
//        Date d=new Date();
//        long time2=d.getTime();
        //获取当前时间
        long time2=System.currentTimeMillis();
        long days = (time1 - time2) / (1000 * 60 * 60 * 24);

        return Integer.parseInt(String.valueOf(days));
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(fun3("2023-01-01")+"天");

    }



版权声明:本文为X18160013原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。