Julian day of the year in Java

后端 未结 9 1028
挽巷
挽巷 2020-11-30 12:12

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

9条回答
  •  粉色の甜心
    2020-11-30 12:45

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

提交回复
热议问题