android - Android 中带有字符串参数的 shamsi/Jalali 日历

标签 android algorithm date date-conversion persian

我正在使用此算法将日历日期更改为波斯日历。 here 它工作正常但是: 当我想创建一个新的构造函数来获取参数并将字符串更改为波斯语日期时,比如将 "2012/3/6" 更改为波斯语(而不是使用内部日期)我有一个非常奇怪的问题和安装不匹配点。 所以当期望 1392/9/4 时,算法显示 1392/8/4。

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.net.ParseException;
import android.util.Log;

public class ShamsiCalleder {


        private class SolarCalendar {

            public String strWeekDay = "";
            public String strMonth = "";

            int date;
            int month;
            int year;

            public SolarCalendar(String str)
            {

                //SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

                //String newDateStr = postFormater.format(dateObj); 



                // for converting input date
                Date MiladiDate = null;
                SimpleDateFormat  format = new SimpleDateFormat("yyyy/MM/dd");  
                try {  
                    MiladiDate = format.parse(str);  
                    System.out.println(date);  

                } catch (java.text.ParseException e) {

                    e.printStackTrace();
                }


                calcSolarCalendar(MiladiDate);
            }



            public SolarCalendar()
            {
                Date MiladiDate = new Date();
                Log.i("PRS", "from solarcalender="+MiladiDate);
                calcSolarCalendar(MiladiDate);
            }

            public SolarCalendar(Date MiladiDate)
            {
                calcSolarCalendar(MiladiDate);
            }

            private void calcSolarCalendar(Date MiladiDate) {

                int ld;

                int miladiYear = MiladiDate.getYear() + 1900;
                int miladiMonth = MiladiDate.getMonth() + 1;
                int miladiDate = MiladiDate.getDate();
                int WeekDay = MiladiDate.getDay();

                int[] buf1 = new int[12];
                int[] buf2 = new int[12];

                buf1[0] = 0;
                buf1[1] = 31;
                buf1[2] = 59;
                buf1[3] = 90;
                buf1[4] = 120;
                buf1[5] = 151;
                buf1[6] = 181;
                buf1[7] = 212;
                buf1[8] = 243;
                buf1[9] = 273;
                buf1[10] = 304;
                buf1[11] = 334;

                buf2[0] = 0;
                buf2[1] = 31;
                buf2[2] = 60;
                buf2[3] = 91;
                buf2[4] = 121;
                buf2[5] = 152;
                buf2[6] = 182;
                buf2[7] = 213;
                buf2[8] = 244;
                buf2[9] = 274;
                buf2[10] = 305;
                buf2[11] = 335;

                if ((miladiYear % 4) != 0) {
                    date = buf1[miladiMonth - 1] + miladiDate;

                    if (date > 79) {
                        date = date - 79;
                        if (date <= 186) {
                            switch (date % 31) {
                            case 0:
                                month = date / 31;
                                date = 31;
                                break;
                            default:
                                month = (date / 31) + 1;
                                date = (date % 31);
                                break;
                            }
                            year = miladiYear - 621;
                        } else {
                            date = date - 186;

                            switch (date % 30) {
                            case 0:
                                month = (date / 30) + 6;
                                date = 30;
                                break;
                            default:
                                month = (date / 30) + 7;
                                date = (date % 30);
                                break;
                            }
                            year = miladiYear - 621;
                        }
                    } else {
                        if ((miladiYear > 1996) && (miladiYear % 4) == 1) {
                            ld = 11;
                        } else {
                            ld = 10;
                        }
                        date = date + ld;

                        switch (date % 30) {
                        case 0:
                            month = (date / 30) + 9;
                            date = 30;
                            break;
                        default:
                            month = (date / 30) + 10;
                            date = (date % 30);
                            break;
                        }
                        year = miladiYear - 622;
                    }
                } else {
                    date = buf2[miladiMonth - 1] + miladiDate;

                    if (miladiYear >= 1996) {
                        ld = 79;
                    } else {
                        ld = 80;
                    }
                    if (date > ld) {
                        date = date - ld;

                        if (date <= 186) {
                            switch (date % 31) {
                            case 0:
                                month = (date / 31);
                                date = 31;
                                break;
                            default:
                                month = (date / 31) + 1;
                                date = (date % 31);
                                break;
                            }
                            year = miladiYear - 621;
                        } else {
                            date = date - 186;

                            switch (date % 30) {
                            case 0:
                                month = (date / 30) + 6;
                                date = 30;
                                break;
                            default:
                                month = (date / 30) + 7;
                                date = (date % 30);
                                break;
                            }
                            year = miladiYear - 621;
                        }
                    }

                    else {
                        date = date + 10;

                        switch (date % 30) {
                        case 0:
                            month = (date / 30) + 9;
                            date = 30;
                            break;
                        default:
                            month = (date / 30) + 10;
                            date = (date % 30);
                            break;
                        }
                        year = miladiYear - 622;
                    }

                }

                switch (month) {
                case 1:
                    strMonth = "�روردين";
                    break;
                case 2:
                    strMonth = "ارديبهشت";
                    break;
                case 3:
                    strMonth = "خرداد";
                    break;
                case 4:
                    strMonth = "تير";
                    break;
                case 5:
                    strMonth = "مرداد";
                    break;
                case 6:
                    strMonth = "شهريور";
                    break;
                case 7:
                    strMonth = "مهر";
                    break;
                case 8:
                    strMonth = "آبان";
                    break;
                case 9:
                    strMonth = "آذر";
                    break;
                case 10:
                    strMonth = "دي";
                    break;
                case 11:
                    strMonth = "بهمن";
                    break;
                case 12:
                    strMonth = "اس�ند";
                    break;
                }

                switch (WeekDay) {

                case 0:
                    strWeekDay = "يکشنبه";
                    break;
                case 1:
                    strWeekDay = "دوشنبه";
                    break;
                case 2:
                    strWeekDay = "سه شنبه";
                    break;
                case 3:
                    strWeekDay = "چهارشنبه";
                    break;
                case 4:
                    strWeekDay = "پنج شنبه";
                    break;
                case 5:
                    strWeekDay = "جمعه";
                    break;
                case 6:
                    strWeekDay = "شنبه";
                    break;
                }

            }

        }




        public static String getCurrentShamsidate() {
            Locale loc = new Locale("en_US");
            ShamsiCalleder shamsi = new ShamsiCalleder();
            SolarCalendar sc = shamsi.new SolarCalendar();
            return String.valueOf(sc.year) + "/" + String.format(loc, "%02d",
                    sc.month) + "/" + String.format(loc, "%02d", sc.date);
        }
        public static String getCurrentShamsidate(String str) {
            Locale loc = new Locale("en_US");
            ShamsiCalleder shamsi = new ShamsiCalleder();
            SolarCalendar sc = shamsi.new SolarCalendar(str);
            Log.i("PRS", "mointh is="+sc.month);
            return String.valueOf(sc.year) + "/" + String.format(loc, "%02d",
                    sc.month) + "/" + String.format(loc, "%02d", sc.date);
        }

所以当我调用:ShamsiCalleder.getCurrentShamsidate()

它返回 1392/10/4(今天正好!)

当我打电话时(今天):ShamsiCalleder.getCurrentShamsidate("2013/12/25")

返回 1392/9/4

我希望有人能帮助我解决这个问题。 关于。

最佳答案

好吧,我终于找到了这个问题的根源。

我曾经使用此代码获取当前日期:

Calendar c = Calendar.getInstance(); 
int day = c.get(Calendar.DATE);
int mounth = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);

但我发现月份是从 0 开始索引的。 所以当它返回 11 时,它意味着 Dec.

所以我把代码改成这样:

Calendar c = Calendar.getInstance(); 
int day = c.get(Calendar.DATE);
int mounth = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.YEAR);

感谢我所有的 friend 。

关于android - Android 中带有字符串参数的 shamsi/Jalali 日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20762404/

相关文章:

javascript - 正则表达式是否有可能知道日期是否是周末?

android - 当 Google Play 服务添加为依赖项时无法使用 gradle 构建

android - 独立工具栏项目上的背景波纹效果消失了

javascript - 算法:我怎么不能使用滑动窗口方法来解决这个问题?

c++ - Advanced SelectionSort - 在一次迭代中搜索两个元素

android - Android 中的 System.currentTimeMillis()?

android - InputListener.touchDragged 中的鼠标坐标不一致(闪烁)

java - 如何在自定义适配器中使用数组?

c++ - 使用带有 max_element 的 lambda 函数编译错误

scala - 如何在scala中获取没有时间的当前日期