爪哇岛一年中的儒略日

发布于 2024-09-05 02:55:00 字数 270 浏览 7 评论 0 原文

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

那么,有人在 Java 中有一个解决方案来获取当前日期的三位数儒略日(不是儒略日期 - 我需要今年的这一天)吗?

谢谢!标记

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.

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)?

Thanks! Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

救赎№ 2024-09-12 02:55:00
DateFormat d = new SimpleDateFormat("D");
System.out.println(d.format(date));
DateFormat d = new SimpleDateFormat("D");
System.out.println(d.format(date));
东京女 2024-09-12 02:55:00

tl;dr

LocalDate.now().getDayOfYear()

…或…

org.threeten.extra.DayOfYear.now() 

“儒略日”术语

术语“儒略日”有时被宽松地用来表示序号 一年中的某一天,或序数日期,含义1 到 365 或 366 之间的数字(闰年)。 1 月 1 日为 1,1 月 2 日为 2,12 月 31 日为 365(闰年为 366)。

这种对 Julian 的松散(不正确)使用可能来自 在天文学和其他跟踪日期领域中使用,作为自公元前 4713 年 1 月 1 日世界时中午以来的连续天数计数(在 儒略历)。如今,儒略日期已接近二百五十万,即今天的2,457,576

java.time

Java 8 及更高版本中内置的 java.time 框架提供了一种简单的工具来获取一年中的某一天。

LocalDate 类表示仅日期值,没有时间和时区。您可以询问一年中的哪一天。

LocalDate localDate = LocalDate.of ( 2010 , Month.JUNE , 8 );
int dayOfYear = localDate.getDayOfYear ();

转储到控制台。结果显示 2010 年 6 月 8 日确实是第 159 天。

System.out.println ( "localDate: " + localDate + " | dayOfYear: " + dayOfYear );

本地日期:2010-06-08 |年中某日:159

时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,法国巴黎午夜过后几分钟就是新的一天,而魁北克蒙特利尔仍然是“昨天”。

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
int dayOfYear = today.getDayOfYear ();

走向另一个方向,从数字到日期。

LocalDate ld = Year.of( 2017 ).atDay( 159 ) ;

org.thirden.extra.DayOfYear

ThreeTen-Extra 库向 Java 内置的 java.time 类添加了功能。

该库提供了一个类来明确表示任何一年的序数日:DayOfYear.使用此类而不是单纯的整数可以使您的代码更加自文档化,提供类型安全并确保有效值。

DayOfYear dayOfYear = DayOfYear.from( LocalDate.of ( 2010 , Month.JUNE , 8 ) ) ;

获取特定年份的 DayOfYear 日期。

LocalDate localDate = dayOfYear.atYear( 2023 ) ;

关于 java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date< /a>, <代码>日历,& SimpleDateFormat< /代码>

要了解更多信息,请参阅 Oracle 教程 。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310

Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。

您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 jeps/170" rel="noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 和 Hibernate 5 JPA 2.2 支持 java.time。

从哪里获取 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 Interval YearWeek YearQuarter ,以及更多

tl;dr

LocalDate.now().getDayOfYear()

…or…

org.threeten.extra.DayOfYear.now() 

“Julian day” terminology

The term “Julian day” is sometimes used loosely to mean the ordinal day of the year, or Ordinal date, meaning a number from 1 to 365 or 366 (leap years). January 1 is 1, January 2 is 2, December 31 is 365 (or 366 in leap years).

This loose (incorrect) use of Julian probably comes from the use in astronomy and other fields of tracking dates as a continuous count of days since noon Universal Time on January 1, 4713 BCE (on the Julian calendar). Nowadays the Julian date is approaching two and half million, 2,457,576 today.

java.time

The java.time framework built into Java 8 and later provides an easy facility to get the day-of-year.

The LocalDate class represents a date-only value without time-of-day and without time zone. You can interrogate for the day-of-year.

LocalDate localDate = LocalDate.of ( 2010 , Month.JUNE , 8 );
int dayOfYear = localDate.getDayOfYear ();

Dump to console. Results show that June 8, 2010 is indeed day # 159.

System.out.println ( "localDate: " + localDate + " | dayOfYear: " + dayOfYear );

localDate: 2010-06-08 | dayOfYear: 159

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
int dayOfYear = today.getDayOfYear ();

Going the other direction, from a number to a date.

LocalDate ld = Year.of( 2017 ).atDay( 159 ) ;

org.threeten.extra.DayOfYear

The ThreeTen-Extra library adds functionality to the java.time classes built into Java.

This library offers a class to represent explicitly the ordinal day of any year: DayOfYear. Using this class rather than a mere integer number makes your code more self-documenting, provides type-safety, and ensures valid values.

DayOfYear dayOfYear = DayOfYear.from( LocalDate.of ( 2010 , Month.JUNE , 8 ) ) ;

Get a date for a DayOfYear with specific year.

LocalDate localDate = dayOfYear.atYear( 2023 ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

樱花落人离去 2024-09-12 02:55:00

如果您想要的只是一年中的某一天,为什么不使用 GregorianCalendars DAY_OF_YEAR 字段呢?

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 天:

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

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));
}

}

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());
一刻暧昧 2024-09-12 02:55:00
import java.util.Calendar;
// ...
final int julianDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);

请注意,这没有考虑您引用的那个奇怪网站声称的“中午开始”交易。当然,只需检查时间就可以解决这个问题。

import java.util.Calendar;
// ...
final int julianDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);

Note that this doesn't take into account the "starts at noon" deal claimed by that weird site you referenced. That could be fixed by just checking the time of course.

淡看悲欢离合 2024-09-12 02:55:00

我已阅读所有帖子,但我认为有些内容不太清楚。

user912567提到了Jean Meeus,他是绝对正确的。

我发现的最准确的定义是Jean Meeus在其“天文算法”书中给出的(必须有,真的......)。

Julian Date 是一个日期,按照通常的方式表示,包含年、月和日。

儒略日是一个数字(实数),从 -4712 年开始计算,是“...连续的天数...”(以及天的分数)。
用于精确天文计算的有用时间尺度。

Jean Meeus:“儒略日与儒略历无关”(《天文算法》,第 2 版,第 59 页)

I've read all the posts and something's not very clear I think.

user912567 mentionned Jean Meeus, and he's absolutely right

The most accurate definition I've found is given by Jean Meeus in its "Astronomical Algorithms" book (a must have, really...).

Julian Date is a date, expressed as usual, with a year, a month and a day.

Julian Day is a number (a real number), counted from year -4712 and is "...a continuous count of days..." (and fraction of day).
A usefull time scale used for accurate astronomical calculations.

Jean Meeus : "The Julian Day has nothing to do with the Julian calendar" ("Astronomical Algorithms", 2nd Edition, p.59)

携君以终年 2024-09-12 02:55:00

如果我们得到双朱利安日期,例如弦决策管理器

http://java.ittoolbox.com/groups/technical-function/java-l/java-function-to-convert-julian-date-to-calendar- date-1947446

以下内容正常工作,但第二个内容没有得到处理
如何在 Java 日期和 Julian 之间进行转换天数?

public static String julianDate(String julianDateStr) {

    try{
        // Calcul date calendrier Gr?gorien ? partir du jour Julien ?ph?m?ride 
        // Tous les calculs sont issus du livre de Jean MEEUS "Calcul astronomique" 
        // Chapitre 3 de la soci?t? astronomique de France 3 rue Beethoven 75016 Paris 
        // Tel 01 42 24 13 74 
        // Valable pour les ann?es n?gatives et positives mais pas pour les jours Juliens n?gatifs
        double jd=Double.parseDouble(julianDateStr);
          double z, f, a, b, c, d, e, m, aux;
            Date date = new Date();
            jd += 0.5;
            z = Math.floor(jd);
            f = jd - z;

            if (z >= 2299161.0) {
              a = Math.floor((z - 1867216.25) / 36524.25);
              a = z + 1 + a - Math.floor(a / 4);
            } else {
              a = z;
            }

            b = a + 1524;
            c = Math.floor((b - 122.1) / 365.25);
            d = Math.floor(365.25 * c);
            e = Math.floor((b - d) / 30.6001);
            aux = b - d - Math.floor(30.6001 * e) + f;
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, (int) aux);

            double hhd= aux-calendar.get(Calendar.DAY_OF_MONTH);                
            aux = ((aux - calendar.get(Calendar.DAY_OF_MONTH)) * 24);




            calendar.set(Calendar.HOUR_OF_DAY, (int) aux);

            calendar.set(Calendar.MINUTE, (int) ((aux - calendar.get(Calendar.HOUR_OF_DAY)) * 60));



         // Calcul secondes 
            double mnd = (24 * hhd) - calendar.get(Calendar.HOUR_OF_DAY);
            double ssd = (60 * mnd) - calendar.get(Calendar.MINUTE); 
            int ss = (int)(60 * ssd);
            calendar.set(Calendar.SECOND,ss);



            if (e < 13.5) {
              m = e - 1;
            } else {
              m = e - 13;
            }
            // Se le resta uno al mes por el manejo de JAVA, donde los meses empiezan en 0.
            calendar.set(Calendar.MONTH, (int) m - 1);
            if (m > 2.5) {
              calendar.set(Calendar.YEAR, (int) (c - 4716));
            } else {
              calendar.set(Calendar.YEAR, (int) (c - 4715));
            }


        SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        //System.out.println("Appnumber= "+appNumber+" TimeStamp="+timeStamp+" Julian Date="+julianDateStr+" Converted Date="+sdf.format(calendar.getTime()));
        return sdf.format(calendar.getTime());

}catch(Exception e){
    e.printStackTrace();
}
return null;

}  

if we get a double julian date such as chordiant decision manager

http://java.ittoolbox.com/groups/technical-functional/java-l/java-function-to-convert-julian-date-to-calendar-date-1947446

The following is working but second is not taken care of
How can I convert between a Java Date and Julian day number?

public static String julianDate(String julianDateStr) {

    try{
        // Calcul date calendrier Gr?gorien ? partir du jour Julien ?ph?m?ride 
        // Tous les calculs sont issus du livre de Jean MEEUS "Calcul astronomique" 
        // Chapitre 3 de la soci?t? astronomique de France 3 rue Beethoven 75016 Paris 
        // Tel 01 42 24 13 74 
        // Valable pour les ann?es n?gatives et positives mais pas pour les jours Juliens n?gatifs
        double jd=Double.parseDouble(julianDateStr);
          double z, f, a, b, c, d, e, m, aux;
            Date date = new Date();
            jd += 0.5;
            z = Math.floor(jd);
            f = jd - z;

            if (z >= 2299161.0) {
              a = Math.floor((z - 1867216.25) / 36524.25);
              a = z + 1 + a - Math.floor(a / 4);
            } else {
              a = z;
            }

            b = a + 1524;
            c = Math.floor((b - 122.1) / 365.25);
            d = Math.floor(365.25 * c);
            e = Math.floor((b - d) / 30.6001);
            aux = b - d - Math.floor(30.6001 * e) + f;
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, (int) aux);

            double hhd= aux-calendar.get(Calendar.DAY_OF_MONTH);                
            aux = ((aux - calendar.get(Calendar.DAY_OF_MONTH)) * 24);




            calendar.set(Calendar.HOUR_OF_DAY, (int) aux);

            calendar.set(Calendar.MINUTE, (int) ((aux - calendar.get(Calendar.HOUR_OF_DAY)) * 60));



         // Calcul secondes 
            double mnd = (24 * hhd) - calendar.get(Calendar.HOUR_OF_DAY);
            double ssd = (60 * mnd) - calendar.get(Calendar.MINUTE); 
            int ss = (int)(60 * ssd);
            calendar.set(Calendar.SECOND,ss);



            if (e < 13.5) {
              m = e - 1;
            } else {
              m = e - 13;
            }
            // Se le resta uno al mes por el manejo de JAVA, donde los meses empiezan en 0.
            calendar.set(Calendar.MONTH, (int) m - 1);
            if (m > 2.5) {
              calendar.set(Calendar.YEAR, (int) (c - 4716));
            } else {
              calendar.set(Calendar.YEAR, (int) (c - 4715));
            }


        SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        //System.out.println("Appnumber= "+appNumber+" TimeStamp="+timeStamp+" Julian Date="+julianDateStr+" Converted Date="+sdf.format(calendar.getTime()));
        return sdf.format(calendar.getTime());

}catch(Exception e){
    e.printStackTrace();
}
return null;

}  
小矜持 2024-09-12 02:55:00

如果您要查找自公元前 4713 年以来的天数中的儒略日,那么您可以请使用以下代码:

private static int daysSince1900(Date date) {
    Calendar c = new GregorianCalendar();
    c.setTime(date);

    int year = c.get(Calendar.YEAR);
    if (year < 1900 || year > 2099) {
        throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");
    }
    year -= 1900;
    int month = c.get(Calendar.MONTH) + 1;
    int days = c.get(Calendar.DAY_OF_MONTH);

    if (month < 3) {
        month += 12;
        year--;
    }
    int yearDays = (int) (year * 365.25);
    int monthDays = (int) ((month + 1) * 30.61);

    return (yearDays + monthDays + days - 63);
}

/**
 * Get day count since Monday, January 1, 4713 BC
 * https://en.wikipedia.org/wiki/Julian_day
 * @param date
 * @param time_of_day percentage past midnight, i.e. noon is 0.5
 * @param timezone in hours, i.e. IST (+05:30) is 5.5
 * @return
 */
private static double julianDay(Date date, double time_of_day, double timezone) {
    return daysSince1900(date) + 2415018.5 + time_of_day - timezone / 24;
}

上面的代码基于 https://stackoverflow.com/a/9593736,因此受到限制迄今为止 1900 年至 2099 年。

If you're looking for Julian Day as in the day count since 4713 BC, then you can use the following code instead:

private static int daysSince1900(Date date) {
    Calendar c = new GregorianCalendar();
    c.setTime(date);

    int year = c.get(Calendar.YEAR);
    if (year < 1900 || year > 2099) {
        throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");
    }
    year -= 1900;
    int month = c.get(Calendar.MONTH) + 1;
    int days = c.get(Calendar.DAY_OF_MONTH);

    if (month < 3) {
        month += 12;
        year--;
    }
    int yearDays = (int) (year * 365.25);
    int monthDays = (int) ((month + 1) * 30.61);

    return (yearDays + monthDays + days - 63);
}

/**
 * Get day count since Monday, January 1, 4713 BC
 * https://en.wikipedia.org/wiki/Julian_day
 * @param date
 * @param time_of_day percentage past midnight, i.e. noon is 0.5
 * @param timezone in hours, i.e. IST (+05:30) is 5.5
 * @return
 */
private static double julianDay(Date date, double time_of_day, double timezone) {
    return daysSince1900(date) + 2415018.5 + time_of_day - timezone / 24;
}

The above code is based on https://stackoverflow.com/a/9593736, and so is limited to dates between 1900 and 2099.

满地尘埃落定 2024-09-12 02:55:00

根据 @Basil Bourque 的回答,下面是我使用系统默认区域 ID 获取一年中的儒略日的实现。

LocalDate localDate = LocalDate.now(ZoneId.systemDefault());
int julianDay = localDate.getDayOfYear();

Following @Basil Bourque answer, below is my implementation to get the Julian day of the year using system default Zone ID.

LocalDate localDate = LocalDate.now(ZoneId.systemDefault());
int julianDay = localDate.getDayOfYear();
唱一曲作罢 2024-09-12 02:55:00

如果您真的只需要“一年中的某一天”,实际上,这只是公历/标准日历和 wallenborn 的答案是正确的。

但是,如果您关心实际的 Jualian-Day 或任何进一步的天文相关信息,我为此编写了一个小实用程序

https://github.com/wirklich-xyz/wirklich.xyz.astro/ blob/main/astro-time/src/main/java/xyz/wirklich/astro/time/JulianDay.java

也发布在 Maven 中心 xyz.wirklich.astro

随意使用它。这对于所有合理的用例来说都是非常精确的。

If you really just need "day of the year", indeed, this is just Gregorian/standard calendar and the answer of wallenborn is correct.

However, if you care for the actual Jualian-Day or any further astronomically-relevant information, I wrote a small utility for this

https://github.com/wirklich-xyz/wirklich.xyz.astro/blob/main/astro-time/src/main/java/xyz/wirklich/astro/time/JulianDay.java

which is also published on maven central at xyz.wirklich.astro

Feel free to use it. This is very precise for all reasonable use cases.

我只土不豪 2024-09-12 02:55:00

您还可以通过以下方式获取“儒略日期”或“序数日期”:

import java.util.Calendar;
import java.util.Date;

public class MyClass {

  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    LocalDate myObj = LocalDate.now();
    System.out.println(myObj);
    System.out.println("Julian Date:" + cal.get(Calendar.DAY_OF_YEAR));

  }
}

You can also get the "Julian Date" or "Ordinal Date" this way:

import java.util.Calendar;
import java.util.Date;

public class MyClass {

  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    LocalDate myObj = LocalDate.now();
    System.out.println(myObj);
    System.out.println("Julian Date:" + cal.get(Calendar.DAY_OF_YEAR));

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文