Java 时间日期类+实例(任意一个月的天数),超详细

1.Date类

public Date()
//并初始化,以便它代表它被分配的时间,精确到毫秒
public Date(long date)
//初始化为表示从标准基准时间起指定的毫秒数分配一个 Date对象,并初始化
public long getTime()
//获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
public void setTime(long time)
//设置时间,给的是毫秒值

	Date d1 = new Date();
	System.out.println(d1);
//runing:Wed May 19 16:11:26 CST 2021

分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数

	long date = 1000*60*60;
    Date d2=new Date(date);
    System.out.println(d2);
//running:Thu Jan 01 09:00:00 CST 1970

获得系统的当前时间

        long time=System.currentTimeMillis();
        d.setTime(time);
        System.out.println(d);
 //running:Wed May 19 16:11:26 CST 2021

日期转字符串

		SimpleDateFormat sdf=new SimpleDateFormat();
        String s = sdf.format(d);
        System.out.println(s);
 //running:21-5-19 下午4:11

日期转成特定格式字符串

		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s2 = sdf1.format(d);
        System.out.println(s2);
//running:2021-05-19 16:11:26

字符串转化为标准日期

        String s3="2021-05-19  13:14:52" ;
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf2.parse(s3);
        System.out.println(date1);
//running:Wed May 19 13:14:52 CST 2021

2.日期工具类

定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法。

//日期工具类
public class DateUtils {
    private DateUtils(){
    }
    public static String DatetoString(Date date,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        String s=sdf.format(date);
        return s;
    }
    public static Date stringToDate(String s,String format) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date=sdf.parse(s);
        return date;
    }
}

//测试类

public class Demo {
    public static void main(String[] args) throws ParseException {
        Date date =new Date();

        String s1=DateUtils.DatetoString(date,"yyyy年MM月dd日 HH:mm:ss");
        System.out.println(s1);

        String s2="2021-5-20 15:20:00";
        Date dd=DateUtils.stringToDate(s2,"yyyy-MM-dd HH:mm:ss");
        System.out.println(dd);
    }
}
// running:2021年05月19日 16:39:41
           Thu May 20 15:20:00 CST 2021

3.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();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MARCH)+1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
 // running :2021年5月19日

//需求: 2年后5天前

        c.add(Calendar.YEAR,2);
        c.add(Calendar.DATE,-5);
        int year1=c.get(Calendar.YEAR);
        int month1=c.get(Calendar.MARCH)+1;
        int date1 = c.get(Calendar.DATE);
        System.out.println(year1 + "年" + month1 + "月" + date1 + "日");
 // running :2023年5月14日

//设置当年的年月日

        c.set(2021,5,20);
        int year2=c.get(Calendar.YEAR);
        int month2=c.get(Calendar.MARCH);
        int date2 = c.get(Calendar.DATE);
        System.out.println(year2 + "年" + month2 + "月" + date2 + "日");
 // running : 2021年5月20日

4.求某年二月的天数

思路:3月1日往前推一天,就是2月的最后一天

public class CalendarTest {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入年:");
        int year = sc.nextInt();

        Calendar c=Calendar.getInstance();
        c.set(year,2,1);//2表示三月

        c.add(Calendar.DATE,-1);
        int date=c.get(Calendar.DATE);
        System.out.println(year + "年的2月份有" + date + "天");
    }
}
//running:  请输入年:2021
                2021年的2月份有28天
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Devour123

愿每一份用功都可以得到回报

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值