7 Replies - 390 Views - Last Post: 12 June 2018 - 11:32 AM

#1 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 12:34 AM

Hi i have a question about the Calendar and LocalDate.

I use LocalDate and have made a dataTable in JSF primefaces that has the function as a "Calendar".
You will see every date of one month on the screen.

So i want to switch between months so that the dataTable gets all the date of the previous month or the next month, according of witch button left or right you click.

The functionality works fine, except that i use the systemdate that gets only the date of today. So When i switch the months he starts always from the system date.

How can i get the date, of a date from the Calendar itself? So i can switch to next or previous month properly?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How does the Calendar work when it has to switch the month?

#2 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 04:36 AM

View Postwim81, on 12 June 2018 - 09:34 AM, said:

The functionality works fine, except that i use the systemdate that gets only the date of today. So When i switch the months he starts always from the system date.


Could you clarify this?

But anyway, you can add months to a LocalDate with the LocalDate.plusMonths method.

LocalDate datePlusOneMonth = originalDate.plusMonths(1);



If you want a specific date, you can enter a string as an argument to the method LocalDate.parse.

LocalDate parsedDate = LocalDate.parse("2018-07-20");



I hope I answered your question.

This post has been edited by mkkekkonen: 12 June 2018 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 04:59 AM

Hi, thanks for your answer.

So what i have as code is this:

 public void previousMonth() {
        
       LocalDate date = LocalDate.now().withDayOfMonth(1);

        LocalDate previousMonth = date.minusMonths(1);
        LocalDate end = previousMonth.plusMonths(1);
        month = previousMonth.getMonth();
        dayDataList = new ArrayList<>();
        while (previousMonth.isBefore(end)) {
            data = dayDataTaskService.findAllDataForUser("Jan", Date.valueOf(previousMonth));
            if (data == null) {
                DayData dat = new DayData();
                dat.setRamDate(Date.valueOf(previousMonth));
                dayDataList.add(dat);
                previousMonth = previousMonth.plusDays(1);
                System.out.println(dat.getRamDate());
            } else {
                dayDataList.add(data);
                previousMonth = previousMonth.plusDays(1);

            }
        }
        projects = projectTaskService.findAllProjects();

    }


You notice the line

LocalDate date = LocalDate.now().withDayOfMonth(1);


Wich means that i use the systemdate as starting point. But if you use a calendar widget, say in a datepicker or so, you can switch between the months. I don't know how to implement it as it is in a Calendar ui?

So in short i don't want to use the system date, but a date from the current month that is displaying.

This post has been edited by ndc85430: 12 June 2018 - 11:31 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 07:16 AM

I'm not entirely sure I understand, but you likely need to have some kind of a state (a class variable etc.) regarding the current day/month the user is viewing. Then call plusMonths and minusMonths when the user switches the month.

This post has been edited by mkkekkonen: 12 June 2018 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 07:24 AM

I've tried to use the ArrayList that comes from the dataTable and get the date from the first element/Object.
And then do with plusMonths. But that didn't help.

This post has been edited by ndc85430: 12 June 2018 - 11:31 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#6 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 07:39 AM

Do you handle changing the month with AJAX/fetch or do you reload the entire page? You could pass the current day/month either as a form value or in the AJAX body and then handle it server-side and return it to the UI.
Was This Post Helpful? 0
  • +
  • -

#7 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 07:45 AM

This is the code from the xhtml file.
                            <p:commandButton icon="fa fa-angle-left" action="#{insertProjectHoursBean.previousMonth}" update="form"/> | <h:outputText value="#{insertProjectHoursBean.month}" /> |
                            <p:commandButton icon="fa fa-angle-right" action="#{insertProjectHoursBean.nextMonth}" update="form"/>
                            <p:commandButton id="submit_insertHours" value="Opslaan" action="#{insertProjectHoursBean.saveHours}" style="width:100px; float:right;" />

                        



I update only the form.

This post has been edited by ndc85430: 12 June 2018 - 11:31 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,827
  • Joined: 13-June 14

Re: How does the Calendar work when it has to switch the month?

Posted 12 June 2018 - 11:32 AM

wim81, there's no need to quote the previous post in its entirety. Please just press the "Reply" button in future.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1