Date工具类的使用

Date

在类 Date 所有可以接受或返回年、月、日期、小时、分钟和秒值的方法中,将使用下面的表示形式:

1.年份 y 由整数 y - 1900 表示。
2.月份由从 0 至 11 的整数表示;0 是一月、1 是二月等等;因此 11 是十二月。
3.日期(一月中的某天)按通常方式由整数 1 至 31 表示。
4.小时由从 0 至 23 的整数表示。因此,从午夜到 1 a.m. 的时间是 0 点,从中午到 1 p.m. 的时间是 12 点。
5.分钟按通常方式由 0 至 59 的整数表示。
6.秒由 0 至 61 的整数表示;值 60 和 61 只对闰秒发生,尽管那样,也只用在实际正确跟踪闰秒的 Java 实现中。于按当前引入闰秒的方式,两个闰秒在同一分钟内发生是极不可能的,但此规范遵循 ISO C 的日期和时间约定。

代码如下:

package com.czh;

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

/**
 * Date工具类的使用
 */
public class Main {
    //日期工具类的具体使用
    public static void main(String[] args)throws Exception {
        //获取当前时间是星期几
        SimpleDateFormat dateFormat=new SimpleDateFormat("EEEE");
        String s = dateFormat.format(new Date());
        System.out.println("今天是"+s);
        //根据日期获取该月的第一天和最后一天
         getday();
        //输入年份判断是否是闰年
        leapYear();
        //根据生日求出年龄
        age();
        //获取当前日期
        getdate();
        //获取当前日期时间
        gettime();
    }
    //获取当前日期
    public static void getdate() throws Exception{
        DateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat2.format(new Date());
        System.out.println("当前日期是"+format);
    }
    //获取当前日期
    public static void gettime() throws Exception{
        DateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat2.format(new Date());
        System.out.println("当前日期是"+format);
    }
    //根据生日求出年龄
    public static void age() throws Exception{
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入生日日期");
        String str=scanner.next();
        DateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
        Date date2=dateFormat2.parse(str);
        //获取出生日期的毫秒值
        long mytime=date2.getTime();
        //获取当前时间的毫秒值
        long newdate=new Date().getTime();
        //用当前时间的毫秒值减去出生日期的毫秒值
        long datetime=newdate-mytime;
        //转换为天数
        long day=datetime/1000/60/60/24/365;
        //输出
        System.out.println(day+"岁");


    }
    //输入年份判断是否是闰年
    public static void leapYear(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("手动输入年份");
        int year=scanner.nextInt();
        if(year%4==0 && year%100!=0){
            System.out.println("该年份是闰年");
        }else if (year%400==0){
            System.out.println("该年份是闰年");
        }else {
            System.out.println("该年份不是闰年");
        }
    }
    //根据日期获取该月的第一天和最后一天
    public static void getday() throws Exception{
        Scanner scanner=new Scanner(System.in);
        System.out.println("手动输入日期");
        String str=scanner.next();
        DateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
        Date date2=dateFormat2.parse(str);
        //设置为当月的某一天
        date2.setDate(1);
        System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
        switch (date2.getMonth()+1){
            case 1:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 2:
                date2.setDate(28);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 3:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 4:
                date2.setDate(30);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 5:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 6:
                date2.setDate(30);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 7:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 8:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 9:
                date2.setDate(30);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 10:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 11:
                date2.setDate(30);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            case 12:
                date2.setDate(31);
                System.out.println(date2.getMonth()+1+"月"+date2.getDate()+"日");
                break;
            default:
                break;

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值