Solved Checking time and day

Discussion in 'Plugin Development' started by sgavster, Mar 29, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    sgavster

    Hello!


    I was wondering if it is possible to check if it is a certain time and day, for example say I wanted it to be wednesday at 3PM, and at that time send all the players "HUMP DAY!" (just an example; would never do this)

    Is this possible?


    Thanks!
     
  2. Offline

    Ambamore2000

    Maybe make it so every minute, it checks the systems date, and yeah.

    EDIT: Use the date class.
     
  3. Offline

    sgavster

    @Ambamore2000 I did this:

    PHP:
        public static int getDay() {
             
            
    Calendar cal Calendar.getInstance();
            
    int day cal.get(Calendar.DAY_OF_MONTH);
            return 
    day;
        }
       
        public static 
    int getHour() {
             
            
    Calendar cal Calendar.getInstance();
            
    int hour cal.get(Calendar.HOUR_OF_DAY);
            return 
    hour;
        }
       
        public static 
    int getMinute() {
             
            
    Calendar cal Calendar.getInstance();
            
    int min cal.get(Calendar.MINUTE);
            return 
    min;
        }
        public static 
    boolean getPM() {
           
            
    Calendar cal Calendar.getInstance();
            
    String am cal.get(Calendar.AM_PM) == "AM" "PM";
           
            return 
    am == "PM" true false;
        }
    but I can't tell if its wednesday, and it uses military time.. Is there a better way?
     
  4. Offline

    Ambamore2000

    You shouldn't create a new Calendar object every time a method using it is called; Create Calendar cal, and in the onEnable, set cal = Calendar.getInstance();

    Now, to answer your question, you should use the SimpleDateFormat class (for both telling if it is wednesday and changing from military time to am/pm).

    Example:
    DON'T CLICK IF YOU WANT TO TRY YOURSELF (open)

    Code:
    Date dateObject = new Date();
    
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM dd, yyyy");
                SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
    
                String todayDate = dateFormat.format(dateObject);
                String time = timeFormat.format(dateObject);
    
    todayDate would be "Sunday March 29, 2015"
    time = "07:07 PM"
     
    sgavster likes this.
  5. Offline

    sgavster

    @Ambamore2000 Solved. Thank you!

    Code: (for ANYONE who wants it :))

    PHP:
        static Calendar cal Calendar.getInstance();
       
        public static 
    int getDay() {

            
    int day cal.get(Calendar.DAY_OF_MONTH);
            return 
    day;
        }
       
        public static 
    String getDayName() {
           
            
    Date dat = new Date();
            
    SimpleDateFormat day = new SimpleDateFormat("EEEE");
            return 
    day.format(dat);
        }
       
        public static 
    int getHour() {
            
            
    int hour cal.get(Calendar.HOUR_OF_DAY);
            
    int th getPM() ? hour 12 hour;
            return 
    th;
           
        }
       
        public static 
    int getMinute() {
            

            
    int min cal.get(Calendar.MINUTE);
            
    int tm min 10 min min;
            return 
    tm;
        }
        public static 
    boolean getPM() {
           

            
    String am cal.get(Calendar.AM_PM) == "AM" "PM";
           
            return 
    am == "PM" true false;
        }
     
Thread Status:
Not open for further replies.

Share This Page