Is there a 'Search period' or 'Date search' interface?

pic

If you look at the picture, you can search for time as above.

Is there an interface library or source code like that?

help me.

Is this for Android, Processing or Html/js? Please provide these details when adding your question. If this is html, I strongly suggest you also explore Stackoverflow or do a generic Google search as well.

Kf

1 Like

@kfrajer

  • Tool: Processing JAVA Mode
  • I wonder if there is a library or example source code available in JAVA mode.

Look at website libraries

Controlp5 or g4p

Could have it

Dear Chrisir, @Chrisir

No matter how much you search, there is no source code to find the date. Do you know anything?

Please help me.

please download

1 Like

@Chrisir

thank you for the reply. very thanks.

By the way, this is not what I want.
What i want

  1. Click the Date button.
  2. The calendar appears.
  3. You should be able to edit the year, month, and day.

I wanted an interface that could perform 1)~3).

Here’s a couple of links I’ve found regarding date and time in Java (and thus Processing):


https://docs.oracle.com/javase/tutorial/datetime/iso/instant.html

Here’s an example from the first link. I’m no Java expert, all I know is Processing is based on Java, and is essentially the same. It seems you don’t need to include the package com.journaldev.java8.time in the sketch / program for Processing.

// Tests for Processing
// https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;

/**
 * LocalDate Examples
 * @author pankaj
 *
 */

//Current Date
LocalDate today = LocalDate.now();
System.out.println("Current Date="+today);

//Creating LocalDate by providing input arguments
LocalDate firstDay_2014 = LocalDate.of(2014, Month.JANUARY, 1);
System.out.println("Specific Date="+firstDay_2014);


//Try creating date by providing invalid inputs
//LocalDate feb29_2014 = LocalDate.of(2014, Month.FEBRUARY, 29);
//Exception in thread "main" java.time.DateTimeException: 
//Invalid date 'February 29' as '2014' is not a leap year

//Current date in "Asia/Kolkata", you can get it from ZoneId javadoc
LocalDate todayKolkata = LocalDate.now(ZoneId.of("Asia/Kolkata"));
System.out.println("Current Date in IST="+todayKolkata);

//java.time.zone.ZoneRulesException: Unknown time-zone ID: IST
//LocalDate todayIST = LocalDate.now(ZoneId.of("IST"));

//Getting date from the base date i.e 01/01/1970
LocalDate dateFromBase = LocalDate.ofEpochDay(365);
System.out.println("365th day from base date= "+dateFromBase);

LocalDate hundredDay2014 = LocalDate.ofYearDay(2014, 100);
System.out.println("100th day of 2014="+hundredDay2014);


exit();

EDIT: As for ready-made solution I don’t know.

1 Like

@raron

Thank you very much for the answer.

Thanks for the solution on how to calculate, etc.

However, I am curious if there is an interface like setting in the web (in Java program).

So this is what I asked. Thanks for the answer.

like here

you can split into single tabs

(you could add the option to make the calendar on or off if you like)

// ********************************************************************************
//         joined pde-file of folder CalenderGrid3
// ********************************************************************************


// ********************************************************************************
// tab: CalenderGrid3.pde main file (file name is folder name)


// for calendar
import java.util.*;
import java.util.Calendar;
import java.text.*;
import java.text.DateFormat;

final int normal = 0;
final int help   = 1;
int state = normal;

ArrayList <Appointment> appointments = new ArrayList();

// a list of the days 
final String[] daysWeek = { 
  "MO", "TU", "WE", "TH", "FR", "SA", "SU"
};

// and months
final String[] monthsNames = { 
  "January", "February", "March", "April", "May", "June", "July", 
  "August", "September", "October", "November", "December"
};

// the grid 
Cell[][] cells = new Cell[7][6];

// a font 
PFont font1;

// month and year (not now but as seen in the grid) 
int currMonth = month();
int currYear = year();

// ---------------------------------------------------------

void setup() {
  size(800, 800);

  init1();

  font1  = createFont("Arial", 20);
  textFont(font1);

  //
  makeAnAppointment( 2014, 10, 13, 
    12, 12, 
    14, 12, 
    "Meeting 17", 
    "John and Jack"  ) ;

  makeAnAppointment( year(), month(), day(), 
    12, 12, 
    14, 12, 
    "Meeting 18", 
    "Mary and Sue"  ) ;

  // --------------------------------------------------
  for (Appointment app1 : appointments) {
    app1.displayPrintln();
  } // for

  println ("----------------------------- ");
  println ("Use mouse click. ");
  println ("Use keyboard: cursor, Pg Up and Pg Down. ");
  println ("Plus in a day means appointment (click with mouse). ");
  println ("The red day is today. ");
  println ("----------------------------- ");

  background(0);
} // func setup()

void draw() {

  // the states: 
  switch (state) {

  case normal: 
    manageStateNormal();
    break;

  case help:
    background(0);
    text ("works with direct window in processing IDE---------------- ", 30, 100);
    text ("Use mouse: click into a day. ", 30, 120);
    text ("Use keyboard: F1, cursor, - and + (month), Pg Up and Pg Down (year). ", 30, 140);
    text ("The Plus + sign in a day means appointment (click with mouse). ", 30, 160);
    text ("The red day is today. ", 30, 180);
    text ("Click mouse ----------------------------- ", 30, 200);
    break;

  default:
    println ("Error 149");
    exit(); 
    break;
  } // switch
} // func draw()
//

// ********************************************************************************
// tab: classAppointment.pde


class Appointment {

  // this represents one appointment
  //
  // see http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html
  // see http://docs.oracle.com/javase/6/docs/api/java/util/Date.html 
  // see http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

  Date from;
  Date to;
  String whatWhere;
  String who;

  SimpleDateFormat sdf = new SimpleDateFormat();
  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
  // DateFormat dfHour = DateFormat.getDateInstance(java.text.DateFormat.HOUR0_FIELD);

  Appointment (Date from_, Date to_, 
    String whatWhere_, 
    String who_)
  {
    //
    from        = from_;
    to          = to_;
    whatWhere   = whatWhere_;
    who         = who_;
  } // constr

  void displayPrintln()
  {
    // simple print
    print("From " + sdf.format(from) );
    println(" to " + sdf.format(to));
    //    println (from);
    //    println (to);
    print (whatWhere);
    println (" with " + who);
    //   print("From " + (from) );
  } // method

  int getFromYearFromApp() {  

    Calendar cal = Calendar.getInstance();

    cal.setTime (from);

    // Calendar.

    return cal.get(Calendar.YEAR);
  }

  int getFromDayFromApp() {  

    Calendar cal = Calendar.getInstance();

    cal.setTime (from);

    // Calendar.

    return cal.get(Calendar.DAY_OF_MONTH );
  }

  //
} // class

// ********************************************************************************
// tab: classCell.pde


class Cell {

  float x;
  float y;
  String letter; 

  Cell(float x_, float y_, String  k ) {
    x = x_;
    y = y_;
    letter = k;
  } // constr

  void show() {
    rectMode(CENTER);
    stroke(39, 20, 1, 150);
    noFill();
    fill(222);
    rect(x, y, 55, 55, 7);
    // is it today? 
    if (letter.equals (str(day())) && 
      currMonth == month() && 
      currYear == year()  ) {
      // yes
      fill(255, 2, 2); // red
    } else {
      // no 
      fill(0);   // black
    } 
    text (letter, x-8, y+8);
    if (hasAppointment()) {
      //
      text ("+", x+15, y+24);
    }
  } // method

  boolean hasAppointment() {
    for (Appointment app1 : appointments) {
      //app1.displayPrintln();
      //println (app1.getFromDayFromApp()+", "+app1.from.getMonth() +", "+app1.getFromYearFromApp() );
      if (letter.equals (trim(str(app1.getFromDayFromApp()))) && 
        currMonth == app1.from.getMonth()+1 && 
        currYear == app1.getFromYearFromApp()  ) {
        // yes
        // println("yes");
        fill(1, 255, 2); // color
        return true;
      } else {
        // no
      }
    } // for
    fill(0);   // black
    return false;
  }

  Appointment getAppointment() {
    for (Appointment app1 : appointments) {
      //app1.displayPrintln();
      //println (app1.getFromDayFromApp()+", "+app1.from.getMonth() +", "+app1.getFromYearFromApp() );
      if (letter.equals (trim(str(app1.getFromDayFromApp()))) && 
        currMonth == app1.from.getMonth()+1 && 
        currYear == app1.getFromYearFromApp()  ) {
        // yes
        // println("yes");
        fill(1, 255, 2); // color
        return app1;
      } else {
        // no
      }
    } // for
    fill(0);   // black
    return null;
  }

  boolean nearMouse () {
    // Is the mouse close ?
    // Can return true or false.
    float distToMouse = dist (x, y, mouseX, mouseY) ; 
    if  ( distToMouse < 55/2 ) {
      return true;
    } else {
      return false;
    }
  } // method
} // class 

// ********************************************************************************
// tab: InputsKeyboard.pde


void keyPressed() {
  if (state==normal) {
    keyPressedForStateNormal ();
  } // if
  else if (state==help) {
    state=normal; 
    key=0;
  } // else if
  else {  
    println ("Error 1149");
    exit();
  }//else 
  //
} // func 

void keyPressedForStateNormal () {
  //
  if (!(key==CODED)) {
    // not CODED -----------------------------------

    // change month 
    if (key=='-')
      currMonth--;
    else if (key=='+')
      currMonth++;
    else if (key>='1'&&key<='9') {
      // month (Jan to Sept)
      currMonth=key-48;
    } else {
      // do nothing
    }
  } else 

  {

    // if (key==CODED) { --------------------------------------------
    //
    switch (keyCode) {

    case java.awt.event.KeyEvent.VK_F1:
      // help
      state = help;
      break;

    case java.awt.event.KeyEvent.VK_PAGE_UP:
      currYear ++;
      break;

    case java.awt.event.KeyEvent.VK_PAGE_DOWN:
      currYear --;
      break;

    case UP:
      currMonth++;
      break;

    case DOWN:
      currMonth--;
      break;

    case LEFT:
      //
      break;

    case RIGHT:
      //
      break;

    default:
      // do nothing 
      break;
    } // switch
  }

  // ---------------

  // prev year? 
  if (currMonth<=0) {
    currMonth=12;
    currYear--;
  }
  // next year?
  else if (currMonth>12) {
    currMonth=1;
    currYear++;
  }

  init1() ;
  //
}

// ********************************************************************************
// tab: InputsMouse.pde


void mousePressed() {
  // 
  if (state==normal) {
    // Normal 
    for (int i = 0; i < cells.length; i++) {
      for (int j = 0; j < cells[i].length; j++) {
        if (cells[i][j].nearMouse()) {
          // show text  
          if (cells[i][j].letter.equals(""))
            println ("no valid day");
          else 
          println (currYear  
            + "/" 
            +  currMonth + "/" 
            + cells[i][j].letter);
          if (cells[i][j].hasAppointment()) { 
            Appointment currApp=cells[i][j].getAppointment();
            currApp.displayPrintln();
          }
          // quit the function 
          return;
        }
      }
    }
  } // if 
  else { 
    state = normal;
  } // else
  //
} /// func 

// ********************************************************************************
// tab: Tools.pde


void init1() {

  // make grid

  int dayAsInt =  dow (1, currMonth, currYear  ) ; 

  dayAsInt--;
  if (dayAsInt<0) 
    dayAsInt=6;  

  // define the grid (k is an increment and shows the day)
  int k = - dayAsInt + 1;

  int daysOfMonth = daysOfMonth(); 

  String textFromK;

  for (int j = 0; j < cells[1].length; j++) {
    for (int i = 0; i < cells.length; i++) {

      textFromK = str(k);
      if (k<1 || k>daysOfMonth)
        textFromK="";

      cells[i][ j] = new Cell( 158 + i*(width/10), 
        158 + j*(height/10), 
        textFromK);

      k++;
    }
  }
} // func 

// Wikipedia.org/wiki/Zeller's_congruence
// by Gotoloop
int dow(int d, int o, int y) {
  // day of the week - int for a list like 
  // Sun, Mon etc. (we use here Mon, Tue...)
  if (o < 3) {
    o += 12;
    y--;
  }

  return ( d + (int) ((o + 1)*2.6) + y + (y>>2)
    + (y/100 | 0)*6 + ~~(y/400) + 6 ) % 7;
}

int daysOfMonth() {

  // Create a calendar object and set year and moth
  Calendar mycal = new GregorianCalendar(currYear, currMonth-1, 1);

  // Get the number of days in that month
  int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

  // Try month in a leap year
  // mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
  // daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);     // 29

  return daysInMonth;
}

void makeAnAppointment(int year, int month, int day, 
  int hourFrom, int minuteFrom, 
  int hourTo, int minuteTo, 
  String what, 
  String who ) {

  // this Appointment can only be within one day:

  // 2013, 3, 4, 12, 12, 14, 12 "Meeting 17",  "John and Jack"

  // and can not last from day A to day B


  Calendar cal = Calendar.getInstance();

  cal.set(year, month-1, day, hourFrom, minuteFrom);
  Date from1 = cal.getTime(); 

  cal.set(year, month-1, day, hourTo, minuteTo);
  Date to1   = cal.getTime();

  Appointment appointmentTest = new Appointment(from1, to1, 
    what, 
    who );
  appointments.add(appointmentTest);
} // func

// ********************************************************************************
// tab: ToolsStates.pde


void manageStateNormal() {

  background(0);

  // headline one 
  fill(0, 255, 0);
  text ( monthsNames [currMonth-1] + " " + currYear, 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y - 80 ); 

  int dayAsInt; 

  //  dayAsInt =  dow (1, currMonth, currYear  ) ; 
  //  dayAsInt--;
  //  if (dayAsInt<0) 
  //    dayAsInt=6;  
  //  text ("1st day in month "
  //    + currMonth 
  //    + " ("
  //    + monthsNames [currMonth-1] 
  //    + ") in year " 
  //    + currYear 
  //    + " is "
  //    + daysWeek [ dayAsInt ], 
  //  cells[0][ 0 ].x, 
  //  cells[0][ 0 ].y + 580 ); 

  // big rect behind the grid
  fill(111);
  noStroke();
  rectMode(CORNER);
  rect(cells[0][ 0 ].x-40, cells[0][ 0 ].y-70, 
    cells[ 6][ 5 ].x-cells[0][ 0 ].x+80, cells[ 6][ 5 ].y - cells[0][ 0 ].y +120, 
    7);

  // draw the week days (headline two)
  for (int i = 0; i < cells.length; i++) {
    fill(0, 255, 0);
    text (daysWeek[i], cells[i][ 0 ].x-13, cells[i][ 0 ].y-33);
  }  

  // draw the grid 
  for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
      cells[i][ j].show();
    }
  }
  // show date today and time now
  fill(0, 255, 0);
  dayAsInt =  dow ( day(), month(), year() ) ; 
  dayAsInt--;
  if (dayAsInt<0) 
    dayAsInt=6;  

  text(  nf( month(), 2 ) 
    +"/" +  nf( day(), 2 ) 
    +"/" +  year()
    +" - " +  nf(hour(), 2) 
    +":" + nf( minute (), 2)  
    +" - " + daysWeek [ dayAsInt ], 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y + 480 ); 

  // help line
  text( "Use F1 - Click mouse on a day or click - and + on the keyboard.", 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y + 620 );
}

// End of joined file. ********************************************************************************
2 Likes

I don’t know, sorry.

But it’s “just” a matter of making a “select date” function, using those Java functions (Java Date Time API)?

Chrisir got a couple of good suggestions and a working example even! Did you look into controlP5 or g4p? They may have a ready-made calendar control. I’ve never used them so I’m not familiar with them.

2 Likes

I assume you want to select a date (09/03/20) from the calendar?

1 Like

@Chrisir, @raron

Like’Dear raron’ says
it’s “just” a matter of making a “select date” function, using those Java functions (Java Date Time API)?
This is the purpose I want.

If there isn’t an easy way, I’ll have to process the source code for’Dear Chrisir’.

Thank you for your reply.

I worked further with this

I can post tomorrow

1 Like

Here we go…

This program let’s you display a screen where you can select one day.

States

This program has different states / screens.

They dictate which screen the program is in:

  • normal state = your program as you have it now. E.g. a Word Processor.
  • Calendar state = show Calendar (think of a dialog like in MS Word)
  • help = show help for the Calendar (not for the program)

States are relevant in draw(), keyPressed() and mousePressed() and have a tab.

Usage

Usage of the Calendar:

  • There is a screen button to open the calendar (use mouse)
  • Esc closes the calendar without result
  • Select a day to pick that date. This closes the calendar as well.
  • Further keys are indicated: + and -, PgUp and PgDn, F1

The result is stored in resultCal. When you leave the Calendar with Esc it stays unchanged.

Tabs

Again you might split the code into different tabs where indicated in the code.

I hope this helps.

Warm regards,

Chrisir

  • Keywords: Select Date, Pick Date, DatePicker, Date Pick, DateSelector, Calendar Tool, Date Tool, Day Picker, Day Pick, DayPicker

// ********************************************************************************
//         joined pde-file of folder CalenderPick1
// ********************************************************************************


// ********************************************************************************
// tab: CalenderPick1.pde main file (file name is folder name)


// for calendar
import java.util.*;
import java.util.Calendar;
import java.text.*;
import java.text.DateFormat;

// states: consts
final int normal = 0; // unique numbers 
final int help   = 1;
final int stateCalendar   = 2;
int state = normal; // current 

// a list of the days 
final String[] daysWeek = { 
  "MO", "TU", "WE", "TH", "FR", "SA", "SU"
};

// and months
final String[] monthsNames = { 
  "January", "February", "March", "April", "May", "June", "July", 
  "August", "September", "October", "November", "December"
};

// the grid 
Cell[][] cells = new Cell[7][6];

// a font 
PFont font1;

// month and year (not now but as seen in the grid) 
int currMonth = month();
int currYear = year();

// result after closing the form
String resultCal=""; 

// ---------------------------------------------------------

void setup() {
  size(800, 800);

  init1();

  font1  = createFont("Arial", 20);
  textFont(font1);

  println ("----------------------------- ");
  println ("Use mouse. ");
  println ("Use keyboard: cursor, Pg Up and Pg Down. And + and -.");
  println ("The red day is today. ");
  println ("----------------------------- ");

  background(0);
} // func setup()

void draw() {

  // the states: 
  switch (state) {

  case normal: 
    //normal
    background(0);
   showButtonCalendar();
    text ("Your program", width/2, height/2-88);
    text (resultCal, width/2, height/2);
    break;

  case stateCalendar:
    manageStateCalendar();
    break; 

  case help:
    background(0);
    text ("Help\n\n", 30, 30);
    text ("Use mouse: click into a day. ", 30, 120);
    text ("Use keyboard: F1, cursor, - and + (month), Pg Up and Pg Down (year). ", 30, 140);
    text ("Esc to leave. ", 30, 160);
    text ("The red day is today. ", 30, 190);
    text ("Click mouse or hit ESC ", 30, 210);
    break;

  default:
    println ("Error 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++");
    exit(); 
    break;
  } // switch
} // func draw()
//

// ********************************************************************************
// tab: classCell.pde



class Cell {

  float x;
  float y;
  String letter; 

  Cell(float x_, float y_, String  k ) {
    x = x_;
    y = y_;
    letter = k;
  } // constr

  void show() {
    rectMode(CENTER);
    stroke(39, 20, 1, 150);
    noFill();
    fill(222);
    rect(x, y, 55, 55, 7);
    // is it today? 
    if (letter.equals (str(day())) && 
      currMonth == month() && 
      currYear == year()  ) {
      // yes
      fill(255, 2, 2); // red
    } else {
      // no 
      fill(0);   // black
    } 
    text (letter, x-8, y+8);
  } // method

  boolean nearMouse () {
    // Is the mouse close ?
    // Can return true or false.
    float distToMouse = dist (x, y, mouseX, mouseY) ; 
    if  ( distToMouse < 55/2 ) {
      return true;
    } else {
      return false;
    }
  } // method
} // class 

// ********************************************************************************
// tab: InputsKeyboard.pde



void keyPressed() {
  if (state==normal) {
    // no keys yet
  } // if
  else if (state==help) {
    // go back with Esc for example
    state=stateCalendar; 
    key=0;
  } else if (state==stateCalendar) {
    keyPressedForStateCalendar();
  } else {  
    println ("Error 1149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    exit();
  }//else 
  //
} // func 

void keyPressedForStateCalendar() {
  //
  if (!(key==CODED)) {
    // not CODED -----------------------------------

    // change month 
    if (key=='-')
      currMonth--;
    else if (key=='+')
      currMonth++;
    else if (key>='1'&&key<='9') {
      // month (Jan to Sept)
      currMonth=key-48;
    } else if (key==ESC) {
      state=normal;
      key=0;
    }
  } else {

    // if (key==CODED) { --------------------------------------------
    //
    switch (keyCode) {

    case java.awt.event.KeyEvent.VK_F1:
      // help
      state = help;
      break;

    case java.awt.event.KeyEvent.VK_PAGE_UP:
      currYear ++;
      break;

    case java.awt.event.KeyEvent.VK_PAGE_DOWN:
      currYear --;
      break;

    case UP:
      currMonth++;
      break;

    case DOWN:
      currMonth--;
      break;

    case LEFT:
      //
      break;

    case RIGHT:
      //
      break;

    default:
      // do nothing 
      break;
    } // switch
  }

  // ---------------

  // prev year? 
  if (currMonth<=0) {
    currMonth=12;
    currYear--;
  }
  // next year?
  else if (currMonth>12) {
    currMonth=1;
    currYear++;
  }

  init1() ;
  //
}

// ********************************************************************************
// tab: InputsMouse.pde


//

void mousePressed() {
  // 
  if (state==stateCalendar) {
    // Calendar 
    for (int i = 0; i < cells.length; i++) {
      for (int j = 0; j < cells[i].length; j++) {
        if (cells[i][j].nearMouse()) {
          // show text  
          if (cells[i][j].letter.equals("")) {
            println ("no valid day");
          } else {
            resultCal = currYear  
              + "/" 
              +  nf(currMonth, 2) + "/" 
              + nfString(cells[i][j].letter);

            // close calender 
            state = normal; 
            // quit the function 
            return;
          }
        }
      }
    }
    //
  } else if (state==normal) {
    // open calendar when clicked on text
    if (mouseX<280&&mouseY<39) {
      state = stateCalendar;
    }
  }  // if 
  else if (state==help) { 
    state = stateCalendar;
  } // else
  //
} /// func 
//

// ********************************************************************************
// tab: Tools.pde


//

void init1() {

  // make grid

  int dayAsInt =  dow (1, currMonth, currYear  ) ; 

  dayAsInt--;
  if (dayAsInt<0) 
    dayAsInt=6;  

  // define the grid (k is an increment and shows the day)
  int k = - dayAsInt + 1;

  int daysOfMonth = daysOfMonth(); 

  String textFromK;

  for (int j = 0; j < cells[1].length; j++) {
    for (int i = 0; i < cells.length; i++) {

      textFromK = str(k);
      if (k<1 || k>daysOfMonth)
        textFromK="";

      cells[i][ j] = new Cell( 158 + i*(width/10), 
        158 + j*(height/10), 
        textFromK);

      k++;
    }
  }
} // func 

// Wikipedia.org/wiki/Zeller's_congruence
// by Gotoloop
int dow(int d, int o, int y) {
  // day of the week - int for a list like 
  // Sun, Mon etc. (we use here Mon, Tue...)
  if (o < 3) {
    o += 12;
    y--;
  }

  return ( d + (int) ((o + 1)*2.6) + y + (y>>2)
    + (y/100 | 0)*6 + ~~(y/400) + 6 ) % 7;
}

int daysOfMonth() {

  // Create a calendar object and set year and moth
  Calendar mycal = new GregorianCalendar(currYear, currMonth-1, 1);

  // Get the number of days in that month
  int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

  // Try month in a leap year
  // mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
  // daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);     // 29

  return daysInMonth;
}

String nfString(String str1) {
  // Leading zero for a String 
  if (str1.length()==1) 
    return "0"+str1;

  if (str1.length()==2) 
    return str1;

  return str1;
}

void showButtonCalendar() {
  fill(0);
  stroke(222);
  rectMode(CORNER);  // Default rectMode is CORNER
  rect(4, 4, 178, 32);
  fill(222); 
  text ("Open Calender", 23, 23);
}
//

// ********************************************************************************
// tab: ToolsStates.pde


//

void manageStateCalendar() {

  background(0);

  // headline one 
  fill(0, 255, 0);
  text ( monthsNames [currMonth-1] + " " + currYear, 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y - 80 ); 

  int dayAsInt; 

  // big rect behind the grid
  fill(111);
  noStroke();
  rectMode(CORNER);
  rect(cells[0][ 0 ].x-40, cells[0][ 0 ].y-70, 
    cells[ 6][ 5 ].x-cells[0][ 0 ].x+80, cells[ 6][ 5 ].y - cells[0][ 0 ].y +120, 
    7);

  // draw the week days (headline two)
  for (int i = 0; i < cells.length; i++) {
    fill(0, 255, 0);
    text (daysWeek[i], cells[i][ 0 ].x-13, cells[i][ 0 ].y-33);
  }  

  // draw the grid 
  for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
      cells[i][ j].show();
    }
  }
  // show date today and time now
  fill(0, 255, 0);
  dayAsInt =  dow ( day(), month(), year() ) ; 
  dayAsInt--;
  if (dayAsInt<0) 
    dayAsInt=6;  

  text(  nf( month(), 2 ) 
    +"/" +  nf( day(), 2 ) 
    +"/" +  year()
    +" - " +  nf(hour(), 2) 
    +":" + nf( minute (), 2)  
    +" - " + daysWeek [ dayAsInt ], 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y + 480 ); 

  // help line
  text( "Use F1 - Click mouse on a day or \nclick - and + on the keyboard. \nEsc to leave.", 
    cells[0][ 0 ].x, 
    cells[0][ 0 ].y + 520 );
  //
}
//

// End of joined file. ********************************************************************************
2 Likes

I found this: https://www.roseindia.net/tutorial/java/swing/datePicker.html

I have basically no idea how to actually use it though. Hopefully others here do.

I did this test that runs (in Processing). The returned date is just a date formatted text here. Depending on what you want with it, getting the date directly is probably better (Change setPickedDate() function to return cal.getTime() instead I think.) Then you could use that to convert to other formats as you see fit. If this can be used in Processing that is.

/** Java Swing DatePicker  - test for Processing

https://www.roseindia.net/tutorial/java/swing/datePicker.html
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

JLabel label = new JLabel("Selected Date:");
final JTextField text = new JTextField(20);
JButton b = new JButton("popup");
JPanel p = new JPanel();
final JFrame f = new JFrame();


void setup(){
  
  size(400, 400);

  p.add(label);
  p.add(text);
  p.add(b);

  f.getContentPane().add(p);
  f.pack();
  f.setVisible(true);
  b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
      text.setText(new DatePicker(f).setPickedDate());
    }
  });

  println("TEST");
  
  //println(text.getText());

}


void draw() {
  background(32);
  
  text("TEST", 20, 20);
  text(text.getText(), 20, 40);

}


class DatePicker {
  int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
  int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
  JLabel l = new JLabel("", JLabel.CENTER);
  String day = "";
  JDialog d;
  JButton[] button = new JButton[49];

  public DatePicker(JFrame parent) {
    d = new JDialog();
    d.setModal(true);
    String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
    JPanel p1 = new JPanel(new GridLayout(7, 7));
    p1.setPreferredSize(new Dimension(430, 120));

    for (int x = 0; x < button.length; x++) {
      final int selection = x;
      button[x] = new JButton();
      button[x].setFocusPainted(false);
      button[x].setBackground(Color.white);
      if (x > 6)
        button[x].addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            day = button[selection].getActionCommand();
            d.dispose();
          }
        });
      if (x < 7) {
        button[x].setText(header[x]);
        button[x].setForeground(Color.red);
      }
      p1.add(button[x]);
    }
    JPanel p2 = new JPanel(new GridLayout(1, 3));
    JButton previous = new JButton("<< Previous");
    previous.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        month--;
        displayDate();
      }
    });
    p2.add(previous);
    p2.add(l);
    JButton next = new JButton("Next >>");
    next.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        month++;
        displayDate();
      }
    });
    p2.add(next);
    d.add(p1, BorderLayout.CENTER);
    d.add(p2, BorderLayout.SOUTH);
    d.pack();
    d.setLocationRelativeTo(parent);
    displayDate();
    d.setVisible(true);
  }

  public void displayDate() {
    for (int x = 7; x < button.length; x++)
      button[x].setText("");
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
        "MMMM yyyy");
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.set(year, month, 1);
    int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
    int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
    for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
      button[x].setText("" + day);
    l.setText(sdf.format(cal.getTime()));
    d.setTitle("Date Picker");
  }

  public String setPickedDate() {
    if (day.equals(""))
      return day;
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
        "dd-MM-yyyy");
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.set(year, month, Integer.parseInt(day));
    return sdf.format(cal.getTime());
  }
}

2 Likes

@Chrisir

I applaud your passion.
Thank you very much.

@raron

This is what I want. Thank you.
Thank you very much.

@Chrisir, @raron

Thanks to you, it was resolved well. Thank you.

For reference, an error occurs when used as follows.

import g4p_controls.;
import java.awt.event.
;

So, you need to set it like this:

import g4p_controls.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

This is project. Thank you.
zz2

3 Likes

@GWAK

Nice! Thanks also for posting your solution.

2 Likes