Java 8 – HijrahDate, How to calculate the Ramadan date

Ramadan is the 9th month of the Islamic calendar, the entire month.

1. HijrahDate -> Ramadan 2016

Full example to calculate the start and end of the Ramadan 2016

TestHijrahDate.java

package com.mkyong.date;

import java.time.LocalDate;
import java.time.chrono.HijrahDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;

public class TestDate {

    public static void main(String[] args) {

        //first day of Ramadan, 9th month
        HijrahDate ramadan = HijrahDate.now()
                .with(ChronoField.DAY_OF_MONTH, 1).with(ChronoField.MONTH_OF_YEAR, 9);
        System.out.println("HijrahDate : " + ramadan);

        //HijrahDate -> LocalDate
        System.out.println("\n--- Ramandan 2016 ---");
        System.out.println("Start : " + LocalDate.from(ramadan));

        //until the end of the month
        System.out.println("End : " + LocalDate.from(ramadan.with(TemporalAdjusters.lastDayOfMonth())));

    }

}

Output


HijrahDate : Hijrah-umalqura AH 1437-09-01

--- Ramandan 2016 ---
Start : 2016-06-06
End : 2016-07-05

References

  1. Wikipedia – Ramadan (calendar month)
  2. HijrahDate JavaDoc

6 comments on “Java 8 – HijrahDate, How to calculate the Ramadan date

  1. import java.time.LocalDate;
    import java.time.chrono.HijrahDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAdjusters

    not importing. can you please tell me where to find these classes.?
    your cooperation will be highly appreciated.
    Thanks.

    Reply
      1. import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JFrame;
        import javax.swing.JTextField;
        import javax.swing.JPasswordField;
        import javax.swing.JLabel;
        import javax.swing.JButton;
        import javax.swing.JInternalFrame;

        public class sample extends JFrame{
        private JTextField textField;
        private JPasswordField passwordField;
        JInternalFrame internalFrame;
        public sample() {
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(299, 43, 86, 20);
        getContentPane().add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(299, 88, 86, 20);
        getContentPane().add(passwordField);

        JLabel lblNewLabel = new JLabel(“u”);
        lblNewLabel.setBounds(233, 46, 46, 14);
        getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel(“p”);
        lblNewLabel_1.setBounds(233, 91, 46, 14);
        getContentPane().add(lblNewLabel_1);

        JButton btnNewButton = new JButton(“c”);
        btnNewButton.addActionListener(new sample2());
        btnNewButton.setBounds(299, 132, 89, 23);
        getContentPane().add(btnNewButton);

        internalFrame = new JInternalFrame(“New JInternalFrame”);
        internalFrame.setBounds(249, 251, 136, 142);
        getContentPane().add(internalFrame);
        internalFrame.setVisible(false);
        internalFrame.getContentPane().setLayout(null);

        JButton btnNewButton_1 = new JButton(“makilchi”);
        btnNewButton_1.setBounds(10, 34, 89, 23);
        internalFrame.getContentPane().add(btnNewButton_1);
        this.setVisible(true);
        this.setSize(500, 500);
        }
        class sample2 implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
        String user,password;
        user = textField.getText();
        password = passwordField.getText();
        if(user.equals(“Vikram”) && password.equals(“Info”)){
        internalFrame.setVisible(true);
        }else{
        System.out.println(“wrong”);
        }
        }

        }
        public static void main(String arg[]){
        sample object = new sample();
        }
        }

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *