问题
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
回答1:
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());
回答2:
DateFormat d = new SimpleDateFormat("D");
System.out.println(d.format(date));
回答3:
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.
回答4:
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;
}
回答5:
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)
回答6:
tl;dr
LocalDate.now( ZoneId.of( “America/Montreal” ) ).getDayOfYear()
“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 ) ;
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.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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.
Where to obtain the java.time classes?
- 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 6 and 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 ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
回答7:
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();
回答8:
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));
}
}
来源:https://stackoverflow.com/questions/3005577/julian-day-of-the-year-in-java