Algorithma Julianday dengan Java

Teringat dengan tahun 2020 adalah termasuk tahun kabisat, penulis mencoba membuka-buka kembali library Java untuk menghitung Julianday yang pernah penulis buat sebelumnya.

Library tersebut adalah kode utama untuk membangun beberapa aplikasi penulis, antara lain Julianday Converter, Predicted Date, dan Date Calculator di Play Store.

Berikut ini adalah kode library Julianday yang penulis maksud di atas:

import java.util.Calendar; 
 
/*  version 2 
*   Last Update 25 Mei 2015 
* 
*   This Lib was made by Smart Calc Community for Smart Calc Lib of 
*   Departement of Physics, Faculty of Mathematics and Natural Sciences, 
*   University Brawijaya, Malang 
* 
* Referensi: 
* Anugraha, Rinto. 2012. Mekanika Benda Langit. Jurusan Fisika Fakultas MIPA Universitas Gajah Mada. 
* 
*   Contact person: 
*   uboiz@yahoo.com 
*/ 
 
public class JulianDate 
{ 
    private int day = 0, month = 0, year = 0,hours=0,minute=0; 
    private double jd = 0; 
    private double peryear = 0; 
    private double permonth = 0; 
    private double penambah = 10; 
    private int AwalTahun = -4712; 
    private int jmlmenit=60*24; 
 
    private int[] numday = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    private String[] nameday = new String[]{"Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"}; 
    private String[] namemonth = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 
 
    public JulianDate() 
    { 
        for (int i = 0; i < numday.length; i++) { 
            peryear += numday[i]; 
        } 
        permonth = peryear / numday.length; 
    } 
 
    public final double trace(int day, int month, int year) 
    { 
        if (month < 1 || month > 12) { 
            if (month < 1) { 
                month = 1; 
            } 
            if (month > 12) { 
                month = 12; 
            } 
        } 
        if (day < 1) { 
            day = 1; 
        } 

        if(day>numday[month-1]){ 
            day=numday[month-1]; 
        } 

        if(basitoh(year) && month==2){ 
            day=numday[month-1]+1; 
        } 

        if (year == 1582 && month == 10) { 
            if (day >= 5 && day < 15) { 
                day = 15; 
            } 
        } 

        this.day = day; 
        this.month = month; 
        this.year = year; 
 
        double hasil = 0; 
        for (int th = AwalTahun; th < year; th++) { 
            if (th % 4 == 0) { 
                if (!basitoh(th)) { 
                    hasil += 1; 
                } else { 
                    if (th < 1582) { 
                        hasil += 1; 
                    } 
                } 
            } 
            hasil += peryear; 
        } 
        int tam=0; 
        if (year % 4 == 0) { 
            if (!basitoh(year)) { 
                tam++; 
            } else { 
                if (year < 1582) { 
                    tam++; 
                } 
            } 
        } 
        for (int i = 0; i < (month - 1); i++) { 
            hasil += numday[i]; 
            if(i==1)hasil+=tam; 
        } 
        hasil += day; 
        if (year > 1582) { 
            hasil -= penambah; 
        } else if (year == 1582) { 
            if (month > 10) { 
                hasil -= penambah; 
            } else if (month == 10) { 
                if (day>=15) { 
                    hasil -= penambah; 
                } 
            } 
        } 
        jd = hasil-1; 
        return jd; 
    } 

    public final double trace(int minute,int hours,int day, int month, int year) 
    { 
        this.hours=hours; 
        this.minute=minute; 
        return trace(day,month,year)+1.0*(hours*60+minute)/jmlmenit; 
    } 
 
    private boolean basitoh(int th) 
    { 
        return ((th >= 1582) && (th % 100 == 0) && (th % 400) != 0); 
    } 
 
    public final void trace(double jd) 
    { 
        this.jd = jd; 
        jd+=1; 
        double jum = 0; 
        int th = AwalTahun; 
        while ((jum+peryear)<jd) { 
            if (th % 4 == 0) { 
                if (!basitoh(th)) { 
                    jum++; 
                } else { 
                    if (th < 1582) { 
                        jum++; 
                    } 
                } 
            } 
            jum += peryear; 
            th++; 
        } 
        year = th; 
        jd -= jum; 
        jum = 0; 
        th = 0; 
        int tam=0; 
        if (year % 4 == 0) { 
            if (!basitoh(year)) { 
                tam++; 
            } else { 
                if (year < 1582) { 
                    tam++; 
                } 
            } 
        } 
        while ((jum+permonth)<jd) { 
            jum +=numday[th]; 
            if(th==1)jum+=tam; 
            th++; 
        } 
        month = th+1; 
        day = (int) (jd - jum); 

        if (year > 1582) { 
            day += penambah; 
        } else if (year == 1582) { 
            if (month > 10) { 
                day += penambah; 
            } else if (month == 10) { 
                if (day>4) { 
                    day += penambah; 
                    if(day>numday[9]){ 
                       day-=numday[9]; 
                       month++; 
                    } 
                } 
            } 
        } 
 
        if(day>numday[th]){ 
            day-=numday[th]; 
            th++; 
            month = th+1; 
            if(month>12){ 
                month-=12; 
                year++; 
            } 
        } 
        double sisa=this.jd-(int)this.jd; 
        double mnt=sisa*jmlmenit; 
        this.hours=(int)(mnt/60); 
        this.minute=((int)mnt)%60; 
    } 
 
    public final void islamic(double nd) 
    { 
        jd = nd + 1947004; 
        trace(jd); 
    } 
 
    public final int getDay() 
    { 
        return day; 
    } 
 
    public final int getMonth() 
    { 
        return month; 
    } 

    public final int getHours() 
    { 
        return hours; 
    } 

    public final int getMinute() 
    { 
        return minute; 
    } 
 
    public final String getMonthName() 
    { 
        return namemonth[month - 1]; 
    } 

    public final String getMonthName(int id) 
    { 
        return namemonth[id - 1]; 
    } 
 
    public final int getYear() 
    { 
        return year; 
    } 
 
    public final double getJD() 
    { 
        return jd; 
    } 
 
    public final int getDayOfWeek() 
    { 
        return (((int) jd) % 7); 
    } 
 
    public final String getNameDayOfWeek() 
    { 
        return nameday[(((int) jd) % 7)]; 
    } 

    public final int getNumDayOfMonth() 
    { 
        int bts=0; 
        if(year%4==0 && month==2){ 
            if(!basitoh(year)){ 
                bts++; 
            }else{ 
                if(year<1582){ 
                    bts++; 
                } 
            } 
        } 
        return numday[month-1]+bts; 
    } 

    public final double getJDThisDay() 
    { 
        Calendar cal = Calendar.getInstance(); 
        return trace(cal.get(Calendar.DAY_OF_MONTH),(cal.get(Calendar.MONTH)+1),cal.get(Calendar.YEAR)); 
    } 
} 


Referensi



Komentar



Postingan populer dari blog ini

Apps Script untuk Cetak Sertifikat

Kumpulan Source Code Greenfoot

Menyembunyikan Failed Load Images di Blogger

Kode Greenfoot Game Flappy Bird

Algorithma Bilangan Prima dengan Javascript

Checking Data yang Belum Dimasukkan dalam Daftar Menggunakan Query Google Sheet

Kebodohan Karyawan Menyalahkan Sistem

Algorithma Coretan Abstrak dengan HTML5 Canvas

Mencoba Submit Theme di Wordpress.org

Generate Karakter Acak dan Menempatkannya di Sel Google Sheets dengan Apps Script