Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 4 of 12 FirstFirst ... 23456 ... LastLast
Results 76 to 100 of 284

Thread: library or component for Hijra date chooser

  1. #76

    Default Re: library for Hijra date chooser

    i know but how

  2. #77
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Creating a class is the basic part of java programming. I am not a tutor so I can not begin to write a manual on creating a class. There are many on the internet.
    I'd start with these steps:
    Make a list of what data the class will hold
    and what methods it will have to access/update the data.
    Add methods as the need comes.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #78

    Default Re: library for Hijra date chooser

    something like this?
     
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
     
    public class hijridate {
     
        public void hijridate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedhijri = formatter.format(hijrahDate); // 07/03/1439
            System.out.println(formattedhijri);
        }
     
        public static void main(String args[]) {
            hijridate hijridate = new hijridate();
            hijridate.hijridate();
        }
     
        public void tst() {
            hijridate();  // no errors
        }
    }
     
     
    }
    Last edited by cnmeysam; April 27th, 2021 at 02:54 PM.

  4. #79
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    You need to write down the design for the class before coding it.
    The statements in that example class might be useful for the design, I don't know.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #80

    Default Re: library for Hijra date chooser

    you main like last post? it must print hiji date?

    --- Update ---

    or like this?
     
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
     
    public class hijridate {
    int year ;
    int month;
    int day;
     
        public void hijridate() {
            HijrahDate today = HijrahChronology.INSTANCE.date(year, month, day);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedhijri = formatter.format(today); // 07/03/1439
            System.out.println(formattedhijri);
        }
     
        public static void main(String args[]) {
            hijridate hijridate = new hijridate();
            hijridate.hijridate();
        }
     
        public void tst() {
            hijridate();  // no errors
        }
    }

  6. #81
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Can you describe what that class is for? What data does it hold?
    How can the data it holds be accessed? What methods would it need to be useful in your project.

    Notes:
    Java class names should start with an uppercase letter.
    There is no need for a main method in this class. It will never be executed. Its purpose is to contain data and provide methods to access that data.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #82

    Default Re: library for Hijra date chooser

    in The gregory use Calendar and Date libraries , so there is no need to create a class. Why should a new class be created in the Hijrah that uses the HijrahDate library?

  8. #83
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Ok, if everything you need is in an existing class, there would not be any need for you to create one.

    Now having all the classes and methods you need, what problems are you having using the dialog to update the date that is displayed in the text field?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #84

    Default Re: library for Hijra date chooser

    i need year month number and day number and in library i have them but i cant update jbox after click on spinner year or month
              Locale ar = new Locale("ar");
            HijrahDate hijrahDate = HijrahDate.now();
            System.out.println("hijrahDate= " + hijrahDate);// this is show full date
     
            // month
            DateTimeFormatter MoonNum = DateTimeFormatter.ofPattern("M", ar);
            String MoondayNum = hijrahDate.format(MoonNum);
            int monthnumber = Integer.parseInt(MoondayNum);
            System.out.println("Month Number  " + monthnumber);// this is show Month Number
     
            DateTimeFormatter MonthName = DateTimeFormatter.ofPattern("L", ar);
            String MonthNames = hijrahDate.format(MonthName);
            System.out.println("Month Name  " + MonthNames);// this is show Month Names
     
            DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
            String dayMoondayNum = hijrahDate.format(dayOfMoonNum);
            int daymoondayNumb = Integer.parseInt(dayMoondayNum);
            System.out.println("Number day-of-month   " + daymoondayNumb);// this is show days of month number (Today's issue this month)
     
            //week
            DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
            String WeekdayNum = hijrahDate.format(dayOfweekNum);
            int WeekdayNumb = Integer.parseInt(WeekdayNum);
            System.out.println("Number day-of-Week   " + WeekdayNumb);// this is show day of week number
     
            DateTimeFormatter DayName = DateTimeFormatter.ofPattern("E", ar);
            String DayNames = hijrahDate.format(DayName);
            System.out.println("Day Name  " + DayNames);// this is show Month Number
            //year
            DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar);
            String HijriYearNum = hijrahDate.format(YearNum);
            int yearnumber = Integer.parseInt(HijriYearNum);
            System.out.println("Number of year  " + yearnumber);// this is show year number
     
            int moondaysnumber = hijrahDate.lengthOfMonth();
            System.out.println("Number of days of the month   " + moondaysnumber);// this is show month length number

    you can see all hijra Meaning here
         Symbol  Meaning                     Presentation      Examples
         ------  -------                     ------------      -------
         G       era                         text              AD; Anno Domini; A
         u       year                        year              2004; 04
         y       year-of-era                 year              2004; 04
         D       day-of-year                 number            189
         M/L     month-of-year               number/text       7; 07; Jul; July; J
         d       day-of-month                number            10
     
         Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
         Y       week-based-year             year              1996; 96
         w       week-of-week-based-year     number            27
         W       week-of-month               number            4
         E       day-of-week                 text              Tue; Tuesday; T
         e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
         F       week-of-month               number            3
     
         a       am-pm-of-day                text              PM
         h       clock-hour-of-am-pm (1-12)  number            12
         K       hour-of-am-pm (0-11)        number            0
         k       clock-hour-of-am-pm (1-24)  number            0
     
         H       hour-of-day (0-23)          number            0
         m       minute-of-hour              number            30
         s       second-of-minute            number            55
         S       fraction-of-second          fraction          978
         A       milli-of-day                number            1234
         n       nano-of-second              number            987654321
         N       nano-of-day                 number            1234000000
     
         V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
         z       time-zone name              zone-name         Pacific Standard Time; PST
         O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
         X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
         x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
         Z       zone-offset                 offset-Z          +0000; -0800; -08:00;
     
         p       pad next                    pad modifier      1
     
         '       escape for text             delimiter
         ''      single quote                literal           '
         [       optional section start
         ]       optional section end
         #       reserved for future use
         {       reserved for future use
         }       reserved for future use
    and i can set date like this
    HijrahDate today = HijrahChronology.INSTANCE.date(year, month, day);

  10. #85
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    cant update jbox
    Sorry, I don't know what a jbox is. Please explain.
    What value do you want to update?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #86

    Default Re: library for Hijra date chooser

    sorry i mean dialogBox

  12. #87
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    I still not sure what you want to update. The dialogBox has two spinners can be changed by the user.
    Are you asking about how to update the days shown in the calendar so that the first day of the month starts in the correct column?
    What is wrong with the code in the reflushWeekAndDay method that does that now?
    I don't know the hijrah calendar to know where the first day of the current month is.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #88

    Default Re: library for Hijra date chooser

    when i use spinner dialog box not update
    whet first time i open dialog box load current month and days Properly and when i push spinner year or month dialog still show current month dialog box will not be updated at all
    this is my class all code
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
     
        Object hijri(){
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedhijri = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formattedhijri);
            return formattedhijri;
     
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
            return dialog;
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
     
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
    //            String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
                JLabel monthNameLabel = new JLabel(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"بس", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String.valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + MoondayNum);
                System.out.println("Number day-of-Week   " + WeekdayNum);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int actionCommandId = moondayNumb - 2 + WeekdayNumb;
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + moondayNumb);
                System.out.println("Number day-of-Week   " + WeekdayNumb);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int dayNo = 2 - WeekdayNumb;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= moondaysnumber) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Locale ar = new Locale("ar");
                DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar);
                DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar);
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                HijrahChronology crono = hijrahDate.getChronology();
                String HijriYearNum = hijrahDate.format(YearNum);
                System.out.println("YearNum   " + HijriYearNum);
                String MoonOfYear = hijrahDate.format(numOfMoonNum);
                System.out.println("month-of-year   " + MoonOfYear);
                System.out.println("crono   " + crono);
                String dayOfMoonNums = hijrahDate.format(dayOfMoonNum);
                int moondayNumb = Integer.parseInt(dayOfMoonNums);
                System.out.println("crono   " + crono);
                HijrahDate date;
                int HijriMoonOfYearb = Integer.parseInt(MoonOfYear);
     
                if (source.getName().equals("Year")) {
     
                    date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb);               
                    hijrahDate=date;
                    System.out.println("date Year  " + date);
                } else {
                    date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb);
                    hijrahDate=date;
                    System.out.println("date MONTH  " + date);
                }
                   hijrahDate=date;
                    reflushWeekAndDay();
            }
     
        }
     
    }

    https://en.wikipedia.org/wiki/Islamic_calendar
    Last edited by cnmeysam; April 27th, 2021 at 04:25 PM.

  14. #89
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Where is the date being stored? Where is the method that updates that date?
    The date displayed in the text field comes from the testMethod which always gets the current date(HijrahDate.now()) and not the date that should be available for updating.
    So the program is only using the current date. It does not have a date that can be updated. Therefore the date that is displayed is always the same.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #90

    Default Re: library for Hijra date chooser

    That is, define a global variable and record the date of the variable for tax?like this
        int ye = 1442;
        int mo = 10;
        int da = 10;
    String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedd = formatter.format(hijrahDate); // 07/03/1439
     
            ye = Integer.parseInt(formattedd.substring(0, 4));
            mo = Integer.parseInt(formattedd.substring(5, 7));
            da = Integer.parseInt(formattedd.substring(8, 10));
     
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
     
            String formatted = formatter.format(today); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
    Last edited by cnmeysam; April 27th, 2021 at 05:39 PM.

  16. #91
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Does that code solve the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #92

    Default Re: library for Hijra date chooser

    i dont know how i must value of sppiner set to ye and mo
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        int ye = 1442;
        int mo = 10;
        int da = 10;
        JSpinner yearSpin;
        JSpinner monthSpin;
     
     
        Object hijri() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedhijri = formatter.format(hijrahDate); // 07/03/1439
            System.out.println("hijrahDate " + formattedhijri);
            return formattedhijri;
     
        }
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public Hijrah() {
            this(new Date());
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
            return dialog;
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
     
                return new Date();
            }
        }
     
        String testMethod() {                  //  ADDED
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formattedd = formatter.format(hijrahDate); // 07/03/1439
     
            ye = Integer.parseInt(formattedd.substring(0, 4));
            mo = Integer.parseInt(formattedd.substring(5, 7));
            da = Integer.parseInt(formattedd.substring(8, 10));
     
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
     
            String formatted = formatter.format(today); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
    //            String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
                JLabel monthNameLabel = new JLabel(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"بس", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
     
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String.valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
     
                return ((Integer) yearSpin.getValue()).intValue();
     
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + MoondayNum);
                System.out.println("Number day-of-Week   " + WeekdayNum);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int actionCommandId = moondayNumb - 2 + WeekdayNumb;
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + moondayNumb);
                System.out.println("Number day-of-Week   " + WeekdayNumb);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int dayNo = 2 - WeekdayNumb;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= moondaysnumber) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Locale ar = new Locale("ar");
                DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar);
                DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar);
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                HijrahChronology crono = hijrahDate.getChronology();
                String HijriYearNum = hijrahDate.format(YearNum);
                System.out.println("YearNum   " + HijriYearNum);
                String MoonOfYear = hijrahDate.format(numOfMoonNum);
                System.out.println("month-of-year   " + MoonOfYear);
                System.out.println("crono   " + crono);
                String dayOfMoonNums = hijrahDate.format(dayOfMoonNum);
                int moondayNumb = Integer.parseInt(dayOfMoonNums);
                System.out.println("crono   " + crono);
                HijrahDate date;
                int HijriMoonOfYearb = Integer.parseInt(MoonOfYear);
     
                if (source.getName().equals("Year")) {
     
                    date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb);
                    hijrahDate = date;
                    System.out.println("date Year  " + date);
                } else {
                    date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb);
                    hijrahDate = date;
                    System.out.println("date MONTH  " + date);
                }
                hijrahDate = date;
                reflushWeekAndDay();
            }
     
        }
     
    }

  18. #93
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Did the code in the NewClass class correctly get and use values from the spinners for year and month?
    Can the technique used in NewClass be used in your new class?
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 27th, 2021)

  20. #94

    Default Re: library for Hijra date chooser

    in the Gregory works currently but in Hijra i don't know but numbers is correct and spin work

  21. #95
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    but in Hijra i don't know
    That is probably code that you need to write.
    But first you need to write down (design) what you want the code to do. Once you have a definition, then write code.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #96

    Default Re: library for Hijra date chooser

    in spinner year and month load hijra date ( year and month ) load correctly
    but when i change value of spinners in text month and year and in jdialog date not updated
    in define 2 global Variable named ye and mo
    But after add spinner values to them, updates are not done
    I do not know how to put these values in the variables
    i change code like this
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.time.chrono.HijrahChronology;
    import java.util.Locale;
     
    public class Hijrah extends JTextField {
     
        int ye = 1442;
        int mo = 10;
        int da = 10;
        JSpinner yearSpin;
        JSpinner monthSpin;
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 250;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
     
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
     
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
     
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
     
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                ye = (Integer) yearSpin.getValue();
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
     
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                mo = (Integer) monthSpin.getValue();
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
     
                String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
    //            String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
                JLabel monthNameLabel = new JLabel(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
                String colname[] = {"بس", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
    //                if (i == 0 || i == 6) {
                    if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
     
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String.valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
     
                return calendar;
     
            }
     
            private int getSelectedYear() {
     
                return ye;
    //            return ((Integer) yearSpin.getValue()).intValue();
     
            }
     
            private int getSelectedMonth() {
                return mo;
    //            return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + MoondayNum);
                System.out.println("Number day-of-Week   " + WeekdayNum);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int actionCommandId = moondayNumb - 2 + WeekdayNumb;
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
     
            }
     
            private void reflushWeekAndDay() {
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahDate.now();
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + moondayNumb);
                System.out.println("Number day-of-Week   " + WeekdayNumb);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int dayNo = 2 - WeekdayNumb;
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= moondaysnumber) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
     
                JSpinner source = (JSpinner) e.getSource();
                Locale ar = new Locale("ar");
                DateTimeFormatter YearNum = DateTimeFormatter.ofPattern("u", ar);
                DateTimeFormatter numOfMoonNum = DateTimeFormatter.ofPattern("M", ar);
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
    //            HijrahDate hijrahDate = HijrahDate.now();
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
                HijrahChronology crono = hijrahDate.getChronology();
                String HijriYearNum = hijrahDate.format(YearNum);
                System.out.println("YearNum   " + HijriYearNum);
                String MoonOfYear = hijrahDate.format(numOfMoonNum);
                System.out.println("month-of-year   " + MoonOfYear);
                System.out.println("crono   " + crono);
                String dayOfMoonNums = hijrahDate.format(dayOfMoonNum);
                int moondayNumb = Integer.parseInt(dayOfMoonNums);
                System.out.println("crono   " + crono);
                HijrahDate date;
                int HijriMoonOfYearb = Integer.parseInt(MoonOfYear);
     
                if (source.getName().equals("Year")) {
     
                    date = crono.date(getSelectedYear(), HijriMoonOfYearb, moondayNumb);
                    hijrahDate = date;
                    System.out.println("date Year  " + date);
                } else {
                    date = crono.date(getSelectedYear(), getSelectedMonth(), moondayNumb);
                    hijrahDate = date;
                    System.out.println("date MONTH  " + date);
                }
                hijrahDate = date;
                reflushWeekAndDay();
            }
     
        }
    public Hijrah() {
            this(new Date());
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
            Frame owner = (Frame) SwingUtilities.getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
     
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
            return dialog;
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
     
        }
     
        public Date getDate() {
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
     
                return new Date();
            }
        }
     
        String testMethod() {                  //  ADDED
    //        HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    //        String formattedd = formatter.format(hijrahDate); // 07/03/1439
    // 
    //        ye = Integer.parseInt(formattedd.substring(0, 4));
    //        mo = Integer.parseInt(formattedd.substring(5, 7));
    //        da = Integer.parseInt(formattedd.substring(8, 10));
     
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
     
            String formatted = formatter.format(today); // 07/03/1439
            System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
     
        public void setDate(Date date) {
    //        super.setText(getDefaultDateFormat().format(date) + " " + testMethod());   //<<<<< Changed
            super.setText(testMethod());   //<<<<< Changed
     
        }
    }
    Last edited by cnmeysam; April 27th, 2021 at 06:17 PM.

  23. #97
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Where is the ye value set in the stateChanged method that is called when the spinner is used?

    The reflushWeekAndDay method does not use the value in ye, it still uses: HijrahDate hijrahDate = HijrahDate.now();

    Describe how the value selected by the spinner makes it to the reflushWeekAndDay method.

    The setDate method is passed a Date object which is does not use? Why pass a value if the method does not use it?

    getSelectedYear returns ye and not the selected spinner value? Why?

    Too many errors. You need to start at the beginning and work through the code and trace how a value goes from the spinner to what is displayed in the text field.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #98

    Default Re: library for Hijra date chooser

     yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
                ye = (Integer) yearSpin.getValue();
     
    monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
                mo = (Integer) monthSpin.getValue();
    The amount of data get from hijra
    i fix getSelectedYear but don't happened anything

    --- Update ---

    can you fix that and send here?

  25. #99
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: library for Hijra date chooser

    Try debugging the code to see what happens to the year value selected by the spinner.
    Add a print statement at each method where the ye is used that prints the name of the method and the value of ye or the object that contains the value of ye like a String or a date object.
    Start at the stateChanged event and continue through where the text field's setText method is called.

    The print outs should show you where the value of ye is not being used or is missing.

    Make the print outs unique so they are easy to find amongst all the other print outs.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #100

    Default Re: library for Hijra date chooser

    this is my last code
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.chrono.HijrahChronology;
    import java.time.chrono.HijrahDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.LineBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    public class Hijrah extends JTextField {
     
    //    int ye ;
    //    int mo ;
    //    int da ;
        int ye = 1442;
        int mo = 10;
        int da = 16;
     
        String[] moonNames = {"ٱلْمُحَرَّم‎", "صَفَر‎", "رَبِيع ٱلْأَوَّل‎", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب‎", "شَعْبَان‎", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
        //String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
        JLabel monthNameLabel = new JLabel();  // get name from array
     
        private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
        private static final int DIALOG_WIDTH = 270;
        private static final int DIALOG_HEIGHT = 210;
     
        private SimpleDateFormat dateFormat;
        private DatePanel datePanel = null;
        private JDialog dateDialog = null;
     
        public void hdate() {
            HijrahDate hijrahDate = HijrahDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            String formatted = formatter.format(hijrahDate); // 07/03/1439 
            ye = Integer.parseInt(formatted.substring(0, 4));
            mo = Integer.parseInt(formatted.substring(5, 7));
            da = Integer.parseInt(formatted.substring(8, 10));
        }
     
        public Hijrah() {
            this(new Date());
        }
     
        public Hijrah(String dateFormatPattern, Date date) {
            this(date);
            DEFAULT_DATE_FORMAT = dateFormatPattern;
        }
     
        public Hijrah(Date date) {
            setDate(date);
            setEditable(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
            addListeners();
        }
     
        private void addListeners() {
            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    if (datePanel == null) {
                        datePanel = new DatePanel();
                    }
                    Point point = getLocationOnScreen();
                    point.y = point.y + 30;
                    showDateDialog(datePanel, point);
                }
            });
        }
     
        private void showDateDialog(DatePanel dateChooser, Point position) {
     
            Frame owner = (Frame) SwingUtilities
                    .getWindowAncestor(Hijrah.this);
            if (dateDialog == null || dateDialog.getOwner() != owner) {
                dateDialog = createDateDialog(owner, dateChooser);
            }
            dateDialog.setLocation(getAppropriateLocation(owner, position));
            dateDialog.setVisible(true);
     
        }
     
        private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
     
            JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
    //        JDialog dialog = new JDialog(owner, "Date Selected", true);
            dialog.setUndecorated(true);
            dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
     
            dialog.addWindowFocusListener(new WindowFocusListener() {
                public void windowLostFocus(WindowEvent e) {
                    dialog.setVisible(false);
                }
     
                public void windowGainedFocus(WindowEvent e) {
                }
            });
     
            return dialog;
     
        }
     
        private Point getAppropriateLocation(Frame owner, Point position) {
            Point result = new Point(position);
            Point p = owner.getLocation();
            int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
            int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
     
            if (offsetX > 0) {
                result.x -= offsetX;
            }
     
            if (offsetY > 0) {
                result.y -= offsetY;
            }
     
            return result;
        }
     
        private SimpleDateFormat getDefaultDateFormat() {
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            }
            return dateFormat;
        }
     
        public void setText(Date date) {
            setDate(date);
        }
     
        public void setDate(Date date) {
            super.setText(testMethod());   //<<<<< Changed
        }
     
        public Date getDate() {
     
            try {
                return getDefaultDateFormat().parse(getText());
            } catch (ParseException e) {
                return new Date();
            }
        }
     
        private class DatePanel extends JPanel implements ChangeListener {
     
            int startYear = 1380;
            int lastYear = 3050;
     
            Color backGroundColor = Color.gray;
            Color palletTableColor = Color.white;
            Color todayBackColor = Color.orange;
            Color weekFontColor = Color.blue;
            Color dateFontColor = Color.black;
            Color weekendFontColor = Color.red;
     
            Color controlLineColor = Color.GREEN;
            Color controlTextColor = Color.white;
            Color controlMoonnameTextColor = Color.BLUE;
     
            JSpinner yearSpin;
            JSpinner monthSpin;
            JButton[][] daysButton = new JButton[6][7];
     
            DatePanel() {
     
                setLayout(new BorderLayout());
                setBorder(new LineBorder(backGroundColor, 2));
                setBackground(backGroundColor);
     
                JPanel topYearAndMonth = createYearAndMonthPanal();
                add(topYearAndMonth, BorderLayout.NORTH);
                JPanel centerWeekAndDay = createWeekAndDayPanal();
                add(centerWeekAndDay, BorderLayout.CENTER);
     
                reflushWeekAndDay();
            }
     
            private JPanel createYearAndMonthPanal() {
     
                Calendar cal = getCalendar();
                int currentYear = cal.get(Calendar.YEAR);
                int currentMonth = cal.get(Calendar.MONTH) + 1;
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout());
                panel.setBackground(controlLineColor);
                JLabel yearLabel = new JLabel("Year");
                yearLabel.setForeground(controlTextColor);
                panel.add(yearLabel);
                yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
                        startYear, lastYear, 1));
     
                yearSpin.setPreferredSize(new Dimension(60, 30));
                yearSpin.setName("Year");
                yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
                yearSpin.addChangeListener(this);
                panel.add(yearSpin);
                monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
                        12, 1));
     
                monthSpin.setPreferredSize(new Dimension(45, 30));
                monthSpin.setName("Month");
                monthSpin.addChangeListener(this);
                monthNameLabel.setPreferredSize(new Dimension(60, 20));
                monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
                monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
                monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]);  // get name from array
                panel.add(monthNameLabel);
     
                panel.add(monthSpin);
     
                JLabel monthLabel = new JLabel("Month");
                monthLabel.setForeground(controlTextColor);
                panel.add(monthLabel);
     
                return panel;
            }
     
            private JPanel createWeekAndDayPanal() {
     
                String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
    //            String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
                JPanel panel = new JPanel();
                panel.setFont(new Font("Arial", Font.PLAIN, 10));
                panel.setLayout(new GridLayout(7, 7));
                panel.setBackground(Color.white);
     
                for (int i = 0; i < 7; i++) {
                    JLabel cell = new JLabel(colname[i]);
                    cell.setHorizontalAlignment(JLabel.CENTER);
                    if (i == 6) {
                        cell.setForeground(weekendFontColor);
                    } else {
                        cell.setForeground(weekFontColor);
                    }
                    panel.add(cell);
                }
     
                int actionCommandId = 0;
                for (int i = 0; i < 6; i++) {
     
                    for (int j = 0; j < 7; j++) {
                        JButton numBtn = new JButton();
                        numBtn.setBorder(null);
                        numBtn.setHorizontalAlignment(SwingConstants.CENTER);
                        numBtn.setActionCommand(String.valueOf(actionCommandId));
                        numBtn.setBackground(palletTableColor);
                        numBtn.setForeground(dateFontColor);
                        numBtn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event) {
                                JButton source = (JButton) event.getSource();
                                if (source.getText().length() == 0) {
                                    return;
                                }
                                dayColorUpdate(true);
                                source.setForeground(todayBackColor);
                                int newDay = Integer.parseInt(source.getText());
                                Calendar cal = getCalendar();
                                cal.set(Calendar.DAY_OF_MONTH, newDay);
                                setDate(cal.getTime());
     
                                dateDialog.setVisible(false);
                            }
                        });
                        if (j == 6) {
                            numBtn.setForeground(weekendFontColor);
                        } else {
                            numBtn.setForeground(dateFontColor);
                        }
                        daysButton[i][j] = numBtn;
                        panel.add(numBtn);
                        actionCommandId++;
                    }
                }
     
                return panel;
            }
     
            private Calendar getCalendar() {
     
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(getDate());
                return calendar;
            }
     
            private int getSelectedYear() {
                return ((Integer) yearSpin.getValue()).intValue();
            }
     
            private int getSelectedMonth() {
                return ((Integer) monthSpin.getValue()).intValue();
            }
     
            private void dayColorUpdate(boolean isOldDay) {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
                System.out.println("spinner ye " + ye);
                System.out.println("spinner mo " + mo);
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + MoondayNum);
                System.out.println("Number day-of-Week   " + WeekdayNum);
                System.out.println("Number of days of the month   " + moondaysnumber);
     
                int actionCommandId = moondayNumb - 3 + WeekdayNumb;
                int i = actionCommandId / 7;
                int j = actionCommandId % 7;
                if (isOldDay) {
                    daysButton[i][j].setForeground(dateFontColor);
                } else {
                    daysButton[i][j].setForeground(todayBackColor);
                }
            }
     
            private void reflushWeekAndDay() {
     
                ye = (Integer) yearSpin.getValue();
                mo = (Integer) monthSpin.getValue();
                System.out.println("spinner ye " + ye);
                System.out.println("spinner mo " + mo);
     
                Locale ar = new Locale("ar");
                DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
                DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
                HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
                HijrahChronology crono = hijrahDate.getChronology();
                HijrahDate date = crono.date(ye, mo, da);
                System.out.println("crono date= " + date);
                String MoondayNum = hijrahDate.format(dayOfMoonNum);
                String WeekdayNum = hijrahDate.format(dayOfweekNum);
                int WeekdayNumb = Integer.parseInt(WeekdayNum);
                int moondayNumb = Integer.parseInt(MoondayNum);
                int moondaysnumber = hijrahDate.lengthOfMonth();
                System.out.println("hijrahDate= " + hijrahDate);
                System.out.println("Number day-of-month   " + moondayNumb);
                System.out.println("Number day-of-Week   " + WeekdayNumb);
                System.out.println("Number of days of the month   " + moondaysnumber);
                int maxDayNo = moondaysnumber;
     
                int dayNo = 3 - WeekdayNumb;
                System.out.println("dayNo   " + dayNo);
                for (int i = 0; i < 6; i++) {
                    for (int j = 0; j < 7; j++) {
                        String s = "";
                        if (dayNo >= 1 && dayNo <= maxDayNo) {
                            s = String.valueOf(dayNo);
                        }
                        daysButton[i][j].setText(s);
                        dayNo++;
                    }
     
                }
                dayColorUpdate(false);
            }
     
            public void stateChanged(ChangeEvent e) {
                dayColorUpdate(true);
                JSpinner source = (JSpinner) e.getSource();
                Calendar cal = getCalendar();
                if (source.getName().equals("Year")) {
                    cal.set(Calendar.YEAR, getSelectedYear());
                } else {
                    cal.set(Calendar.MONTH, getSelectedMonth() - 1);
                }
                setDate(cal.getTime());
                reflushWeekAndDay();
                mothnam();
            }
     
        }
     
        public void mothnam() {
            if (mo == 1) {
                monthNameLabel.setText("ٱلْمُحَرَّم‎");
            } else if (mo == 2) {
                monthNameLabel.setText("صَفَر‎");
            } else if (mo == 3) {
                monthNameLabel.setText("رَبِيع ٱلْأَوَّل‎");
            } else if (mo == 4) {
                monthNameLabel.setText("رَبِيع ٱلثَّانِي");
            } else if (mo == 5) {
                monthNameLabel.setText("جُمَادَىٰ ٱلْأُولَىٰ");
            } else if (mo == 6) {
                monthNameLabel.setText("جُمَادَىٰ ٱلثَّانِيَة");
            } else if (mo == 7) {
                monthNameLabel.setText("رَجَب‎");
            } else if (mo == 8) {
                monthNameLabel.setText("شَعْبَان‎");
            } else if (mo == 9) {
                monthNameLabel.setText("رَمَضَان");
            } else if (mo == 10) {
                monthNameLabel.setText("شَوَّال");
            } else if (mo == 11) {
                monthNameLabel.setText("ذُو ٱلْقَعْدَة");
            } else if (mo == 12) {
                monthNameLabel.setText("ذُو ٱلْحِجَّة");
            }
        }
     
        String testMethod() {                  //  ADDED
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
            String formatted = formatter.format(today); // 07/03/1439
    //        System.out.println("hijrahDate " + formatted);
            return formatted.toString(); //<<<<<< ADDED
        }
     
    }
    now update text and dialogbox after push spinners

    The second case is how to run this part before anything else hdate() i need this part for set year monyh and day in ye mo da for load current date before app opened
    Last edited by cnmeysam; April 28th, 2021 at 11:56 AM.

Page 4 of 12 FirstFirst ... 23456 ... LastLast

Similar Threads

  1. Splitting date string by date and time and assigning it to 2 variables
    By KaranSaxena in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2014, 05:58 AM
  2. Start Date should be exactly 1 week before to End Date
    By bhanuchandar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 26th, 2013, 08:07 AM
  3. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  4. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM