java-date和Calendar运用

1、求出自己已经出生多少天

2、判断闰年

package cn.burce.Calendar;

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

public class CalendarDemo1 {
    public static void main(String[] args) throws Exception {
        birth();
        leapYear();
    }

    // 求出自己已经出生多少天
    public static void birth() throws Exception {
        // 屏幕输入生日
        System.out.println("请输入你的出生日期,格式为yyyymmdd");
        String birthday = new Scanner(System.in).next();
        long now = new Date().getTime();// 转成毫秒值
        // System.out.println("当前毫秒数" + now);

        // 将字符串格式转为Date格式
        // 创建SimpleDateFormat对象,写日期模式
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
        // 调用parse,将字符串转为日期
        Date birthdaydate = s.parse(birthday);
        // System.out.println(birthdaydate);
        long first = birthdaydate.getTime();// 转成毫秒值
        // System.out.println("生日毫秒数" + first);
        if (now - first < 0)
        {
            System.out.println("你还没出生呢!");
        } else
        {
            System.out.println("出生天数为" + (now - first) / 3600 / 24 / 1000);
            System.out.println("出生年数为" + (now - first) / 3600 / 24 / 1000 / 365);
        }
    }

    // 闰年计算
    // 算法:日历设置到指定年份的3月1号,add向前一天,获取天数,29的为闰年
    public static void leapYear() {
        System.out.println("请输入年份");
        String year = new Scanner(System.in).next();
// int year = new Scanner(System.in).nextInt(); 也可以直接变为int
int y = Integer.parseInt(year); Calendar c = Calendar.getInstance(); // 将日历设置到指定年的3月1号//外国2月就是我们三月 c.set(y, 2, 1); // add向前一天 c.add(Calendar.DAY_OF_MONTH, -1); // 获取天数 int day = c.get(Calendar.DAY_OF_MONTH); if (day == 29) { System.out.println("你输入的是闰年!"); } else { System.out.println("你输入的不是闰年!"); } } }

 l  Date: 日期/时间类

构造方法:

public Date()// 系统当前日期时间

public Date(long date) 得到一个197011 0点这个时间基础上,加上参数date 毫秒值对应的日期时间

    方法:

    public long getTime() 获取日期所对应的毫秒值

 

l  DateFormat:是日期/时间格式化子类的抽象类, 使用其子类SimpleDateFormat 实例化

构造方法:

public SimpleDateFormat() 默认的格式化操作

public SimpleDateFormat(String pattern) 按照指定的格式,进行日期格式化

                                    日期和时间模式

                                    y   

                                    M  年中的月份 

                                    d  月份中的天数

                                    H  一天中的小时数(0-23

                                    m  小时中的分钟数

                                    s  分钟中的秒数

                                    S  毫秒数

方法:

                           public final String format(Date date) 把日期 格式化成字符串

                           public Date parse(String source) 把日期字符串 转换成 日期对象

 

l  Calendar:日历类,可获取日期中指定字段的值

方法:

                      public static Calendar getInstance() //获取日期对象

public int get(int field)     //获取时间字段值

public void add(int field,int amount)       //指定字段增加某值

public final void set(int field,int value)//设置指定字段的值

public final Date getTime()      //获取该日历对象转成的日期对象

原文地址:https://www.cnblogs.com/BruceKing/p/13345306.html