java - 如何使用波斯 JDF 日期类?

标签 java android datetime format jalali-calendar

我找到了一个将公历日期转换为贾拉利日期的类

JDF.java

public class JDF {

    /**
     * Main: The default constructor uses the current Gregorian date to
     * initialize the other private memebers of the class (Iranian and Julian
     * dates).
     */
    public JDF() {
        Calendar calendar = new GregorianCalendar();
        setGregorianDate(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
    }

    public JDF(GregorianCalendar calendar) {
        setGregorianDate(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
    }

    public JDF(Calendar calendar) {
        setGregorianDate(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * Main: This constructor receives a Gregorian date and initializes the
     * other private members of the class accordingly.
     *
     * @param year  int
     * @param month int
     * @param day   int
     * @return
     */
    public JDF(int year, int month, int day) {
        setGregorianDate(year, month, day);
    }

    /**
     * getIranianYear: Returns the 'year' part of the Iranian date.
     *
     * @return int
     */
    public int getIranianYear() {
        return irYear;
    }

    /**
     * getIranianMonth: Returns the 'month' part of the Iranian date.
     *
     * @return int
     */
    public int getIranianMonth() {
        return irMonth;
    }

    /**
     * getGregorianCalendar: gets Iranian date and returns Gregorian calendar
     *
     * @return calendar
     */
    public GregorianCalendar getGregorianCalendar(int year, int month, int day)
            throws ParseException {

        setIranianDate(year, month, day);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/M/d",
                Locale.US);
        Date myDate = dateFormat.parse(getGregorianDate());
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(myDate);
        calendar.add(Calendar.MONTH, 1);

        return calendar;
    }

    /**
     * getIranianDayName: Returns the number of Iranian day of week
     *
     * @return int
     */
    public int getIranianDay(int year, int month, int day)
            throws ParseException {

        setIranianDate(year, month, day);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/M/d",
                Locale.US);
        Date myDate = dateFormat.parse(getGregorianDate());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(myDate);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if (Calendar.SATURDAY == dayOfWeek) {
            dayOfWeek = 0;
        } else if (Calendar.SUNDAY == dayOfWeek) {
            dayOfWeek = 1;
        } else if (Calendar.MONDAY == dayOfWeek) {
            dayOfWeek = 2;
        } else if (Calendar.TUESDAY == dayOfWeek) {
            dayOfWeek = 3;
        } else if (Calendar.WEDNESDAY == dayOfWeek) {
            dayOfWeek = 4;
        } else if (Calendar.THURSDAY == dayOfWeek) {
            dayOfWeek = 5;
        } else if (Calendar.FRIDAY == dayOfWeek) {
            dayOfWeek = 6;
        }

        return dayOfWeek;
    }

    /**
     * getIranianDay: Returns the 'day' part of the Iranian date.
     *
     * @return int
     */
    public int getIranianDay() {
        return irDay;
    }

    /**
     * getGregorianYear: Returns the 'year' part of the Gregorian date.
     *
     * @return int
     */
    public int getGregorianYear() {
        return gYear;
    }

    /**
     * getGregorianMonth: Returns the 'month' part of the Gregorian date.
     *
     * @return int
     */
    public int getGregorianMonth() {
        return gMonth;
    }

    /**
     * getGregorianDay: Returns the 'day' part of the Gregorian date.
     *
     * @return int
     */
    public int getGregorianDay() {
        return gDay;
    }

    /**
     * getJulianYear: Returns the 'year' part of the Julian date.
     *
     * @return int
     */
    public int getJulianYear() {
        return juYear;
    }

    /**
     * getJulianMonth: Returns the 'month' part of the Julian date.
     *
     * @return int
     */
    public int getJulianMonth() {
        return juMonth;
    }

    /**
     * getJulianDay() Returns the 'day' part of the Julian date.
     *
     * @return int
     */
    public int getJulianDay() {
        return juDay;
    }

    /**
     * getIranianDate: Returns a string version of Iranian date
     *
     * @return String
     */
    public String getIranianDate() {
        return (irYear + "/" + irMonth + "/" + irDay);
    }

    /**
     * getGregorianDate: Returns a string version of Gregorian date
     *
     * @return String
     */
    public String getGregorianDate() {
        return (gYear + "/" + gMonth + "/" + gDay);
    }

    /**
     * getJulianDate: Returns a string version of Julian date
     *
     * @return String
     */
    public String getJulianDate() {
        return (juYear + "/" + juMonth + "/" + juDay);
    }

    /**
     * getWeekDayStr: Returns the week day name.
     *
     * @return String
     */
    public String getWeekDayStr() {
        String weekDayStr[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
                "Friday", "Saturday", "Sunday"};
        return (weekDayStr[getDayOfWeek()]);
    }

    /**
     * toString: Overrides the default toString() method to return all dates.
     *
     * @return String
     */
    @Override
    public String toString() {
        return (getWeekDayStr() + ", Gregorian:[" + getGregorianDate()
                + "], Julian:[" + getJulianDate() + "], Iranian:["
                + getIranianDate() + "]");
    }

    /**
     * getDayOfWeek: Returns the week day number. Monday=0..Sunday=6;
     *
     * @return int
     */
    public int getDayOfWeek() {
        return (JDN % 7);
    }

    /**
     * nextDay: Go to next julian day number (JDN) and adjusts the other dates.
     */
    public void nextDay() {
        JDN++;
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * nextDay: Overload the nextDay() method to accept the number of days to go
     * ahead and adjusts the other dates accordingly.
     *
     * @param days int
     */
    public void nextDay(int days) {
        JDN += days;
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * previousDay: Go to previous julian day number (JDN) and adjusts the otehr
     * dates.
     */
    public void previousDay() {
        JDN--;
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * previousDay: Overload the previousDay() method to accept the number of
     * days to go backward and adjusts the other dates accordingly.
     *
     * @param days int
     */
    public void previousDay(int days) {
        JDN -= days;
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * setIranianDate: Sets the date according to the Iranian calendar and
     * adjusts the other dates.
     *
     * @param year  int
     * @param month int
     * @param day   int
     */
    public void setIranianDate(int year, int month, int day) {
        irYear = year;
        irMonth = month;
        irDay = day;
        JDN = IranianDateToJDN();
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * setGregorianDate: Sets the date according to the Gregorian calendar and
     * adjusts the other dates.
     *
     * @param year  int
     * @param month int
     * @param day   int
     */
    public void setGregorianDate(int year, int month, int day) {
        gYear = year;
        gMonth = month;
        gDay = day;
        JDN = gregorianDateToJDN(year, month, day);
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * setJulianDate: Sets the date according to the Julian calendar and adjusts
     * the other dates.
     *
     * @param year  int
     * @param month int
     * @param day   int
     */
    public void setJulianDate(int year, int month, int day) {
        juYear = year;
        juMonth = month;
        juDay = day;
        JDN = julianDateToJDN(year, month, day);
        JDNToIranian();
        JDNToJulian();
        JDNToGregorian();
    }

    /**
     * IranianCalendar: This method determines if the Iranian (Jalali) year is
     * leap (366-day long) or is the common year (365 days), and finds the day
     * in March (Gregorian Calendar)of the first day of the Iranian year
     * ('irYear').Iranian year (irYear) ranges from (-61 to 3177).This method
     * will set the following private data members as follows: leap: Number of
     * years since the last leap year (0 to 4) Gy: Gregorian year of the
     * begining of Iranian year march: The March day of Farvardin the 1st (first
     * day of jaYear)
     */
    private void IranianCalendar() {
        // Iranian years starting the 33-year rule
        int Breaks[] = {-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210,
                1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178};
        int jm, N, leapJ, leapG, jp, j, jump;
        gYear = irYear + 621;
        leapJ = -14;
        jp = Breaks[0];
        // Find the limiting years for the Iranian year 'irYear'
        j = 1;
        do {
            jm = Breaks[j];
            jump = jm - jp;
            if (irYear >= jm) {
                leapJ += (jump / 33 * 8 + (jump % 33) / 4);
                jp = jm;
            }
            j++;
        } while ((j < 20) && (irYear >= jm));
        N = irYear - jp;
        // Find the number of leap years from AD 621 to the begining of the
        // current
        // Iranian year in the Iranian (Jalali) calendar
        leapJ += (N / 33 * 8 + ((N % 33) + 3) / 4);
        if (((jump % 33) == 4) && ((jump - N) == 4))
            leapJ++;
        // And the same in the Gregorian date of Farvardin the first
        leapG = gYear / 4 - ((gYear / 100 + 1) * 3 / 4) - 150;
        march = 20 + leapJ - leapG;
        // Find how many years have passed since the last leap year
        if ((jump - N) < 6)
            N = N - jump + ((jump + 4) / 33 * 33);
        leap = (((N + 1) % 33) - 1) % 4;
        if (leap == -1)
            leap = 4;
    }

    /**
     * IranianDateToJDN: Converts a date of the Iranian calendar to the Julian
     * Day Number. It first invokes the 'IranianCalender' private method to
     * convert the Iranian date to Gregorian date and then returns the Julian
     * Day Number based on the Gregorian date. The Iranian date is obtained from
     * 'irYear'(1-3100),'irMonth'(1-12) and 'irDay'(1-29/31).
     *
     * @return long (Julian Day Number)
     */
    private int IranianDateToJDN() {
        IranianCalendar();
        return (gregorianDateToJDN(gYear, 3, march) + (irMonth - 1) * 31
                - irMonth / 7 * (irMonth - 7) + irDay - 1);
    }

    /**
     * JDNToIranian: Converts the current value of 'JDN' Julian Day Number to a
     * date in the Iranian calendar. The caller should make sure that the
     * current value of 'JDN' is set correctly. This method first converts the
     * JDN to Gregorian calendar and then to Iranian calendar.
     */
    private void JDNToIranian() {
        JDNToGregorian();
        irYear = gYear - 621;
        IranianCalendar(); // This invocation will update 'leap' and 'march'
        int JDN1F = gregorianDateToJDN(gYear, 3, march);
        int k = JDN - JDN1F;
        if (k >= 0) {
            if (k <= 185) {
                irMonth = 1 + k / 31;
                irDay = (k % 31) + 1;
                return;
            } else
                k -= 186;
        } else {
            irYear--;
            k += 179;
            if (leap == 1)
                k++;
        }
        irMonth = 7 + k / 30;
        irDay = (k % 30) + 1;
    }

    /**
     * julianDateToJDN: Calculates the julian day number (JDN) from Julian
     * calendar dates. This integer number corresponds to the noon of the date
     * (i.e. 12 hours of Universal Time). This method was tested to be good
     * (valid) since 1 March, -100100 (of both calendars) up to a few millions
     * (10^6) years into the future. The algorithm is based on D.A.Hatcher,
     * Q.Jl.R.Astron.Soc. 25(1984), 53-55 slightly modified by K.M. Borkowski,
     * Post.Astron. 25(1987), 275-279.
     *
     * @param year  int
     * @param month int
     * @param day   int
     * @return int
     */
    private int julianDateToJDN(int year, int month, int day) {
        return (year + (month - 8) / 6 + 100100) * 1461 / 4
                + (153 * ((month + 9) % 12) + 2) / 5 + day - 34840408;
    }

    /**
     * JDNToJulian: Calculates Julian calendar dates from the julian day number
     * (JDN) for the period since JDN=-34839655 (i.e. the year -100100 of both
     * calendars) to some millions (10^6) years ahead of the present. The
     * algorithm is based on D.A. Hatcher, Q.Jl.R.Astron.Soc. 25(1984), 53-55
     * slightly modified by K.M. Borkowski, Post.Astron. 25(1987), 275-279).
     */
    private void JDNToJulian() {
        int j = 4 * JDN + 139361631;
        int i = ((j % 1461) / 4) * 5 + 308;
        juDay = (i % 153) / 5 + 1;
        juMonth = ((i / 153) % 12) + 1;
        juYear = j / 1461 - 100100 + (8 - juMonth) / 6;
    }

    /**
     * gergorianDateToJDN: Calculates the julian day number (JDN) from Gregorian
     * calendar dates. This integer number corresponds to the noon of the date
     * (i.e. 12 hours of Universal Time). This method was tested to be good
     * (valid) since 1 March, -100100 (of both calendars) up to a few millions
     * (10^6) years into the future. The algorithm is based on D.A.Hatcher,
     * Q.Jl.R.Astron.Soc. 25(1984), 53-55 slightly modified by K.M. Borkowski,
     * Post.Astron. 25(1987), 275-279.
     *
     * @param year  int
     * @param month int
     * @param day   int
     * @return int
     */
    private int gregorianDateToJDN(int year, int month, int day) {
        int jdn = (year + (month - 8) / 6 + 100100) * 1461 / 4
                + (153 * ((month + 9) % 12) + 2) / 5 + day - 34840408;
        jdn = jdn - (year + 100100 + (month - 8) / 6) / 100 * 3 / 4 + 752;
        return (jdn);
    }

    public static boolean isLeapYear(int year) {
        double a = 0.025;
        int b = 266;
        double leapDays0;
        double leapDays1;
        if (year > 0) {
            leapDays0 = ((year + 38) % 2820) * 0.24219 + a;
            leapDays1 = ((year + 39) % 2820) * 0.24219 + a;
        } else if (year < 0) {
            leapDays0 = ((year + 39) % 2820) * 0.24219 + a;
            leapDays1 = ((year + 40) % 2820) * 0.24219 + a;
        } else
            return false;

        int frac0 = (int) ((leapDays0 - (int) (leapDays0)) * 1000);
        int frac1 = (int) ((leapDays1 - (int) (leapDays1)) * 1000);

        return frac0 <= b && frac1 > b;
    }

    /**
     * JDNToGregorian: Calculates Gregorian calendar dates from the julian day
     * number (JDN) for the period since JDN=-34839655 (i.e. the year -100100 of
     * both calendars) to some millions (10^6) years ahead of the present. The
     * algorithm is based on D.A. Hatcher, Q.Jl.R.Astron.Soc. 25(1984), 53-55
     * slightly modified by K.M. Borkowski, Post.Astron. 25(1987), 275-279).
     */
    private void JDNToGregorian() {
        int j = 4 * JDN + 139361631;
        j = j + (((((4 * JDN + 183187720) / 146097) * 3) / 4) * 4 - 3908);
        int i = ((j % 1461) / 4) * 5 + 308;
        gDay = (i % 153) / 5 + 1;
        gMonth = ((i / 153) % 12) + 1;
        gYear = j / 1461 - 100100 + (8 - gMonth) / 6;
    }

    private int irYear; // Year part of a Iranian date
    private int irMonth; // Month part of a Iranian date
    private int irDay; // Day part of a Iranian date
    private int gYear; // Year part of a Gregorian date
    private int gMonth; // Month part of a Gregorian date
    private int gDay; // Day part of a Gregorian date
    private int juYear; // Year part of a Julian date
    private int juMonth; // Month part of a Julian date
    private int juDay; // Day part of a Julian date
    private int leap; // Number of years since the last leap year (0 to 4)
    private int JDN; // Julian Day Number
    private int march; // The march day of Farvardin the first (First day of
    // jaYear)
}

但是我不能使用这个类,例如我想显示 jalali String date 像:پنain

不幸的是,源站点还没有完全解释,如果有人在波斯语站点上得到了关于这个类的指导,请提交站点链接

最佳答案

你可以使用这个库:

com.mohamadamin.persianmaterialdatetimepicker.utils

    /**
 * Persian Calendar see: http://code.google.com/p/persian-calendar/
   Copyright (C) 2012  Mortezaadi@gmail.com
   PersianCalendar.java

   Persian Calendar is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.mohamadamin.persianmaterialdatetimepicker.utils;

import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 
 * <strong> Persian(Shamsi) calendar </strong>
 * <p>
 * </p>
 * <p>
 * The calendar consists of 12 months, the first six of which are 31 days, the
 * next five 30 days, and the final month 29 days in a normal year and 30 days
 * in a leap year.
 * </p>
 * <p>
 * As one of the few calendars designed in the era of accurate positional
 * astronomy, the Persian calendar uses a very complex leap year structure which
 * makes it the most accurate solar calendar in use today. Years are grouped
 * into cycles which begin with four normal years after which every fourth
 * subsequent year in the cycle is a leap year. Cycles are grouped into grand
 * cycles of either 128 years (composed of cycles of 29, 33, 33, and 33 years)
 * or 132 years, containing cycles of of 29, 33, 33, and 37 years. A great grand
 * cycle is composed of 21 consecutive 128 year grand cycles and a final 132
 * grand cycle, for a total of 2820 years. The pattern of normal and leap years
 * which began in 1925 will not repeat until the year 4745!
 * </p>
 * </p> Each 2820 year great grand cycle contains 2137 normal years of 365 days
 * and 683 leap years of 366 days, with the average year length over the great
 * grand cycle of 365.24219852. So close is this to the actual solar tropical
 * year of 365.24219878 days that the Persian calendar accumulates an error of
 * one day only every 3.8 million years. As a purely solar calendar, months are
 * not synchronized with the phases of the Moon. </p>
 * <p>
 * </p>
 * 
 * <p>
 * <strong>PersianCalendar</strong> by extending Default GregorianCalendar
 * provides capabilities such as:
 * </p>
 * <p>
 * </p>
 * 
 * <li>you can set the date in Persian by setPersianDate(persianYear,
 * persianMonth, persianDay) and get the Gregorian date or vice versa</li>
 * <p>
 * </p>
 * <li>determine is the current date is Leap year in persian calendar or not by
 * IsPersianLeapYear()</li>
 * <p>
 * </p>
 * <li>getPersian short and long Date String getPersianShortDate() and
 * getPersianLongDate you also can set delimiter to assign delimiter of returned
 * dateString</li>
 * <p>
 * </p>
 * <li>Parse string based on assigned delimiter</li>
 * <p>
 * </p>
 * <p>
 * </p>
 * <p>
 * </p>
 * <p>
 * <strong> Example </strong>
 * </p>
 * <p>
 * </p>
 * 
 * <pre>
 * {@code
 *       PersianCalendar persianCal = new PersianCalendar();
 *       System.out.println(persianCal.getPersianShortDate());
 *       
 *       persianCal.set(1982, Calendar.MAY, 22);
 *       System.out.println(persianCal.getPersianShortDate());
 *       
 *       persianCal.setDelimiter(" , ");
 *       persianCal.parse("1361 , 03 , 01");
 *       System.out.println(persianCal.getPersianShortDate());
 *       
 *       persianCal.setPersianDate(1361, 3, 1);
 *       System.out.println(persianCal.getPersianLongDate());
 *       System.out.println(persianCal.getTime());
 *       
 *       persianCal.addPersianDate(Calendar.MONTH, 33);
 *       persianCal.addPersianDate(Calendar.YEAR, 5);
 *       persianCal.addPersianDate(Calendar.DATE, 50);
 * 
 * }
 * 
 * <pre>
 * @author Morteza  contact: <a href="mailto:Mortezaadi@gmail.com">Mortezaadi@gmail.com</a>
 * @version 1.1
 */
public class PersianCalendar extends GregorianCalendar {

    private static final long serialVersionUID = 5541422440580682494L;

    private int persianYear;
    private int persianMonth;
    private int persianDay;
    // use to seperate PersianDate's field and also Parse the DateString based
    // on this delimiter
    private String delimiter = "/";

    private long convertToMilis(long julianDate) {
        return PersianCalendarConstants.MILLIS_JULIAN_EPOCH + julianDate * PersianCalendarConstants.MILLIS_OF_A_DAY
                + PersianCalendarUtils.ceil(getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH, PersianCalendarConstants.MILLIS_OF_A_DAY);
    }

    /**
     * default constructor
     * 
     * most of the time we don't care about TimeZone when we persisting Date or
     * doing some calculation on date. <strong> Default TimeZone was set to
     * "GMT" </strong> in order to make developer to work more convenient with
     * the library; however you can change the TimeZone as you do in
     * GregorianCalendar by calling setTimeZone()
     */
    public PersianCalendar(long millis) {
        setTimeInMillis(millis);
    }

    /**
     * default constructor
     * 
     * most of the time we don't care about TimeZone when we persisting Date or
     * doing some calculation on date. <strong> Default TimeZone was set to
     * "GMT" </strong> in order to make developer to work more convenient with
     * the library; however you can change the TimeZone as you do in
     * GregorianCalendar by calling setTimeZone()
     */
    public PersianCalendar() {
        setTimeZone(TimeZone.getTimeZone("GMT"));
    }

    /**
     * Calculate persian date from current Date and populates the corresponding
     * fields(persianYear, persianMonth, persianDay)
     */
    protected void calculatePersianDate() {
        long julianDate = ((long) Math.floor((getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH)) / PersianCalendarConstants.MILLIS_OF_A_DAY);
        long PersianRowDate = PersianCalendarUtils.julianToPersian(julianDate);
        long year = PersianRowDate >> 16;
        int month = (int) (PersianRowDate & 0xff00) >> 8;
        int day = (int) (PersianRowDate & 0xff);
        this.persianYear = (int) (year > 0 ? year : year - 1);
        this.persianMonth = month;
        this.persianDay = day;
    }

    /**
     * 
     * Determines if the given year is a leap year in persian calendar. Returns
     * true if the given year is a leap year.
     * 
     * @return boolean
     */
    public boolean isPersianLeapYear() {
        // calculatePersianDate();
        return PersianCalendarUtils.isPersianLeapYear(this.persianYear);
    }

    /**
     * set the persian date it converts PersianDate to the Julian and assigned
     * equivalent milliseconds to the instance
     * 
     * @param persianYear
     * @param persianMonth
     * @param persianDay
     */
    public void setPersianDate(int persianYear, int persianMonth, int persianDay) {
        persianMonth += 1; // TODO
        this.persianYear = persianYear;
        this.persianMonth = persianMonth;
        this.persianDay = persianDay;
        setTimeInMillis(convertToMilis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay)));
    }

    public int getPersianYear() {
        // calculatePersianDate();
        return this.persianYear;
    }

    /**
     * 
     * @return int persian month number
     */
    public int getPersianMonth() {
        // calculatePersianDate();
        return this.persianMonth;
    }

    /**
     * 
     * @return String persian month name
     */
    public String getPersianMonthName() {
        // calculatePersianDate();
        return PersianCalendarConstants.persianMonthNames[this.persianMonth];
    }

    /**
     * 
     * @return int Persian day in month
     */
    public int getPersianDay() {
        // calculatePersianDate();
        return this.persianDay;
    }

    /**
     * 
     * @return String Name of the day in week
     */
    public String getPersianWeekDayName() {
        switch (get(DAY_OF_WEEK)) {
        case SATURDAY:
            return PersianCalendarConstants.persianWeekDays[0];
        case SUNDAY:
            return PersianCalendarConstants.persianWeekDays[1];
        case MONDAY:
            return PersianCalendarConstants.persianWeekDays[2];
        case TUESDAY:
            return PersianCalendarConstants.persianWeekDays[3];
        case WEDNESDAY:
            return PersianCalendarConstants.persianWeekDays[4];
        case THURSDAY:
            return PersianCalendarConstants.persianWeekDays[5];
        default:
            return PersianCalendarConstants.persianWeekDays[6];
        }

    }

    /**
     * 
     * @return String of Persian Date ex: شنبه 01 خرداد 1361
     */
    public String getPersianLongDate() {
        return getPersianWeekDayName() + "  " + this.persianDay + "  " + getPersianMonthName() + "  " + this.persianYear;
    }

    public String getPersianLongDateAndTime() {
        return getPersianLongDate() + " ساعت " + get(HOUR_OF_DAY) + ":" + get(MINUTE) + ":" + get(SECOND);
    }

    /**
     * 
     * @return String of persian date formatted by
     *         'YYYY[delimiter]mm[delimiter]dd' default delimiter is '/'
     */
    public String getPersianShortDate() {
        // calculatePersianDate();
        return "" + formatToMilitary(this.persianYear) + delimiter + formatToMilitary(getPersianMonth()+1) + delimiter + formatToMilitary(this.persianDay);
    }

    public String getPersianShortDateTime() {
        return "" + formatToMilitary(this.persianYear) + delimiter + formatToMilitary(getPersianMonth()+1) + delimiter + formatToMilitary(this.persianDay) + " " + formatToMilitary(this.get(HOUR_OF_DAY)) + ":" + formatToMilitary(get(MINUTE))
                + ":" + formatToMilitary(get(SECOND));
    }

    private String formatToMilitary(int i) {
        return (i < 9) ? "0" + i : String.valueOf(i);
    }

    /**
     * add specific amout of fields to the current date for now doesnt handle
     * before 1 farvardin hejri (before epoch)
     * 
     * @param field
     * @param amount
     *            <pre>
     *  Usage:
     *  {@code
     *  addPersianDate(Calendar.YEAR, 2);
     *  addPersianDate(Calendar.MONTH, 3);
     *  }
     * </pre>
     * 
     *            u can also use Calendar.HOUR_OF_DAY,Calendar.MINUTE,
     *            Calendar.SECOND, Calendar.MILLISECOND etc
     */
    //
    public void addPersianDate(int field, int amount) {
        if (amount == 0) {
            return; // Do nothing!
        }

        if (field < 0 || field >= ZONE_OFFSET) {
            throw new IllegalArgumentException();
        }

        if (field == YEAR) {
            setPersianDate(this.persianYear + amount, getPersianMonth()+1, this.persianDay);
            return;
        } else if (field == MONTH) {
            setPersianDate(this.persianYear + ((getPersianMonth()+1 + amount) / 12), (getPersianMonth()+1 + amount) % 12, this.persianDay);
            return;
        }
        add(field, amount);
        calculatePersianDate();
    }

    /**
     * <pre>
     *    use <code>{@link PersianDateParser}</code> to parse string 
     *    and get the Persian Date.
     * </pre>
     * 
     * @see PersianDateParser
     * @param dateString
     */
    public void parse(String dateString) {
        PersianCalendar p = new PersianDateParser(dateString, delimiter).getPersianDate();
        setPersianDate(p.getPersianYear(), p.getPersianMonth(), p.getPersianDay());
    }

    public String getDelimiter() {
        return delimiter;
    }

    /**
     * assign delimiter to use as a separator of date fields.
     * 
     * @param delimiter
     */
    public void setDelimiter(String delimiter) {
        this.delimiter = delimiter;
    }

    @Override
    public String toString() {
        String str = super.toString();
        return str.substring(0, str.length() - 1) + ",PersianDate=" + getPersianShortDate() + "]";
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);

    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public void set(int field, int value) {
        super.set(field, value);
        calculatePersianDate();
    }

    @Override
    public void setTimeInMillis(long millis) {
        super.setTimeInMillis(millis);
        calculatePersianDate();
    }

    @Override
    public void setTimeZone(TimeZone zone) {
        super.setTimeZone(zone);
        calculatePersianDate();
    }

}

如果你想要精确的格式,你说你可以使用这样的东西:

        PersianCalendar pers = new PersianCalendar();
        String.format(
                    Locale.getDefault(),
                    "%s %d %d:%d",
                    pers.getPersianMonthName(),
                    pers.getPersianDay(),
                    pers.get(Calendar.HOUR_OF_DAY),
                    pers.get(Calendar.MINUTE)
            )

关于java - 如何使用波斯 JDF 日期类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51195640/

相关文章:

android - 在google map static api中绘制圆形路径

android - 如何在android中顺时针和逆时针旋转图像?

java - 创建一个 Student 将其传递给 SQL 对象并在线程 "AWT-EventQueue-0"lang.ArrayIndexOutOfBoundsException : 4 中获取异常

java - 从 jar 加载文件夹

java - 使用 HTTP Post、Java、Json、PHP 的 Android 注册表单

android - android中媒体播放器问题的多个实例

.net - 在 .net 中的两个时间戳之间查找完整日历年、月、日和小时的最简单方法

python - 如何在 Odoo 日期时间字段中显示不同的日期格式?

python - 按时间间隔分组并查找具有相似最小值(进入时间值)的唯一 ID

java - 逻辑问题 A 或 C(C 和 B)