Java 爪哇一年中的儒略日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3005577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Julian day of the year in Java
提问by Mark
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.
我在http://www.rgagnon.com/javadetails/java-0506.html看到了“解决方案” ,但它不能正常工作。例如昨天(6 月 8 日)应该是 159,但它说它是 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)?
那么,是否有人在 Java 中有一个解决方案来获取当前日期的三位数儒略日(不是儒略日期 - 今年我需要这一天)?
Thanks! Mark
谢谢!标记
采纳答案by wallenborn
If all you want is the day-of-year, why don'you just use GregorianCalendars DAY_OF_YEAR
field?
如果您想要的只是一年中的哪一天,为什么不使用 GregorianCalendarsDAY_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));
}
}
}
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:
或者,您可以计算今天的儒略日期与今年 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());
回答by Pointy
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.
请注意,这并没有考虑到您引用的那个奇怪的网站声称的“从中午开始”的交易。这可以通过当然检查时间来解决。
回答by user912567
if we get a double julian date such as chordiant decision manager
如果我们得到一个双朱利安约会,比如chordiant decision manager
The following is working but second is not taken care of How can I convert between a Java Date and Julian day number?
以下是有效的,但第二个没有得到照顾 如何在 Java 日期和儒略日数之间进行转换?
public static String julianDate(String julianDateStr) {
公共静态字符串 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;
}
回答by mariami
DateFormat d = new SimpleDateFormat("D");
System.out.println(d.format(date));
回答by bendeg
I've read all the posts and something's not very clear I think.
我已经阅读了所有帖子,但我认为有些不太清楚。
user912567 mentionned Jean Meeus, and he's absolutely right
user912567 提到了 Jean Meeus,他是绝对正确的
The most accurate definition I've found is given by Jean Meeus in its "Astronomical Algorithms" book (a must have, really...).
我发现的最准确的定义是 Jean Meeus 在其“天文算法”一书中给出的(必须有,真的......)。
Julian Date is a date, expressed as usual, with a year, a month and a day.
Julian Date 是一个日期,照常表示,有年、月和日。
Julian Day is a number(a real number), counted from year -4712 and is "...a continuous count of days..." (and fraction of day). A usefulltime scale used for accurate astronomical calculations.
Julian Day 是一个数字(一个实数),从 -4712 年开始计算,是“......连续的天数......”(以及一天的一小部分)。一个有用的用于精确的天文计算时间尺度。
Jean Meeus : "The Julian Day has nothing to do with the Julian calendar" ("Astronomical Algorithms", 2nd Edition, p.59)
Jean Meeus :“儒略日与儒略历无关”(“天文算法”,第 2 版,第 59 页)
回答by Basil Bourque
tl;dr
tl;博士
LocalDate.now( ZoneId.of( “America/Montreal” ) ).getDayOfYear()
“Julian day” terminology
“儒略日”术语
The term “Julian day” is sometimes used loosely to mean the ordinalday 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).
术语“儒略日”有时被松散地用来表示一年中的序数日,或序数日期,意思是从 1 到 365 或 366(闰年)的数字。1 月 1 日是1
,1 月 2 日是2
,12 月 31 日是365
(或366
闰年)。
This loose (incorrect) use of Julian
probably comes from the use in astronomyand 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.
这种松散(不正确)的使用Julian
可能来自于在天文学和其他领域中使用的跟踪日期作为自公元前 4713 年 1 月 1 日世界时间(儒略历)中午以来的连续天数计数。如今,朱利安日期已接近 250 万,2,457,576
今天。
java.time
时间
The java.time framework built into Java 8 and later provides an easy facility to get the day-of-year.
Java 8 及更高版本中内置的 java.time 框架提供了一种简单的工具来获取年份。
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 = LocalDate.of ( 2010 , Month.JUNE , 8 );
int dayOfYear = localDate.getDayOfYear ();
Dump to console. Results show that June 8, 2010 is indeed day # 159.
转储到控制台。结果显示,2010 年 6 月 8 日确实是第 159 天。
System.out.println ( "localDate: " + localDate + " | dayOfYear: " + dayOfYear );
localDate: 2010-06-08 | dayOfYear: 159
本地日期:2010-06-08 | 一年中的第 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 ) ;
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject 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.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by J_A
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));
}
}
回答by Arpit Aggarwal
Following @Basil Bourque answer, below is my implementation to get the Julian day of the year using system default Zone ID.
按照@Basil Bourque 的回答,下面是我使用系统默认区域 ID 获取一年中的儒略日的实现。
LocalDate localDate = LocalDate.now(ZoneId.systemDefault());
int julianDay = localDate.getDayOfYear();
回答by BKC
If you're looking for Julian Day as in the day count since 4713 BC, then you can use the following code instead:
如果您正在寻找 Julian Day 作为自 4713 BC 以来的天数,那么您可以使用以下代码:
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.
上述代码基于https://stackoverflow.com/a/9593736,因此仅限于 1900 年和 2099 年之间的日期。