如何使用Java了解当前年份当天的数量? [英] How to know the number of the current day in the current years using Java?

查看:173
本文介绍了如何使用Java了解当前年份当天的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用Java计算一年中当天的天数。

I have to calculate in Java what is the number of the current day in the year.

例如,如果今天是 1月1日 resut应 1 。如果是 2月2日,则结果应为 36

For example if today is the 1 of January the resut should be 1. If it is the 5 of February the result should be 36

如何在Java中自动执行此操作?存在本机支持此功能的某些类(例如日历)?

How can I automatically do it in Java? Exist some class (such as Calendar) that natively supports this feature?

推荐答案

您可以使用< a href =http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html =nofollow> java.util.Calendar 类。请注意,月份为零。因此,对于1月1日的情况,它应该是:

You can use java.util.Calendar class. Be careful that month is zero based. So in your case for the first of January it should be:

    Calendar calendar = new GregorianCalendar(2015, 0, 1);
    int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);  

这篇关于如何使用Java了解当前年份当天的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆