爪哇一年中的朱利安日 [英] Julian day of the year in Java

查看:143
本文介绍了爪哇一年中的朱利安日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://www.rgagnon.com/上看过解决方案 javadetails / java-0506.html ,但它无法正常工作。例如。昨天(6月8日)应该是159,但它说它是245。

I have seen the "solution" at http://www.rgagnon.com/javadetails/java-0506.html, but it doesn't work correctly. E.g. yesterday (June 8) should have been 159, but it said it was 245.

那么,是否有人用Java解决方案获得当前日期的三位数Julian日(不是朱利安约会 - 我今年需要这一天)?

So, does someone have a solution in Java for getting the current date's three digit Julian day (not Julian date - I need the day this year)?

谢谢!标记

推荐答案

如果您想要的是每年的日期,为什么不使用GregorianCalendars DAY_OF_YEAR 字段?

If all you want is the day-of-year, why don'you just use GregorianCalendars DAY_OF_YEAR field?

import java.util.GregorianCalendar;
public class CalTest {
    public static void main(String[] argv) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.set(GregorianCalendar.DAY_OF_MONTH, 8);
        gc.set(GregorianCalendar.MONTH, GregorianCalendar.JUNE);
        gc.set(GregorianCalendar.YEAR, 2010);
        System.out.println(gc.get(GregorianCalendar.DAY_OF_YEAR));
}

}

或者,您可以计算今天朱利安日期与今年1月1日之间的差异。但一定要在结果中加1,因为1月1日不是一年中的第0天:

Alternatively, you could calculate the difference between today's Julian date and that of Jan 1st of this year. But be sure to add 1 to the result, since Jan 1st is not the zeroth day of the year:

int[] now = {2010, 6, 8};
int[] janFirst = {2010, 1, 1};
double dayOfYear = toJulian(now) - toJulian(janFirst) + 1
System.out.println(Double.valueOf(dayOfYear).intValue());

这篇关于爪哇一年中的朱利安日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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